Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
learn:courses:unit-7-lab7a:start [2017/03/30 10:45] – [5.3. An IIR Digital Oscillator] Marthalearn:courses:unit-7-lab7a:start [2021/10/13 15:16] (current) Arthur Brown
Line 1: Line 1:
 ====== Lab 7a: Digital Signal Generator ====== ====== Lab 7a: Digital Signal Generator ======
 +[[{}/learn/courses/unit-7/start|Back to Unit 7]]
  
 === Download This Document === === Download This Document ===
-  * {{ :learn:courses:unit-7-lab7a:lab_7a.pdf |Lab 7a PDF}}+{{ :learn:courses:unit-7-lab7a:lab_7a.pdf |Lab 7a PDF}}
  
  
 ===== 1. Objectives ===== ===== 1. Objectives =====
-  Use a programmable IIR filter to generate a single frequency sine wave. +  Use a programmable IIR filter to generate a single frequency sine wave. 
-  Use PWM to generate an audible output tone over the Basys MX3 speaker. +  Use PWM to generate an audible output tone over the Basys MX3 speaker. 
-  Display the frequency of the synthesized sine wave on the LCD and 7-segment display.+  Display the frequency of the synthesized sine wave on the LCD and 7-segment display.
  
  
Line 14: Line 15:
  
 ===== 2. Basic Knowledge ===== ===== 2. Basic Knowledge =====
-  [[http://en.wikibooks.org/wiki/C_Programming/Compiling|Knowledge of C or C++ programming]] +  [[http://en.wikibooks.org/wiki/C_Programming/Compiling|Knowledge of C or C++ programming]] 
-  [[http://ww1.microchip.com/downloads/en/DeviceDoc/52027B.pdf|Working knowledge of MPLAB® X IDE]] +  [[http://ww1.microchip.com/downloads/en/DeviceDoc/52027B.pdf|Working knowledge of MPLAB® X IDE]] 
-  How to display text on a character LCD +  How to display text on a character LCD 
-  How to display numbers on the 4-digit 7-segment display +  How to display numbers on the 4-digit 7-segment display 
-  [[http://en.wikipedia.org/wiki/Finite_impulse_response|Understanding of Finite Impulse Response Digital Filters]]+  [[http://en.wikipedia.org/wiki/Finite_impulse_response|Understanding of Finite Impulse Response Digital Filters]]
  
  
Line 25: Line 26:
 ===== 3. Equipment List ===== ===== 3. Equipment List =====
 ==== 3.1. Hardware ==== ==== 3.1. Hardware ====
-  [[http://store.digilentinc.com/basys-mx3-pic32mx-trainer-board-recommended-for-embedded-systems-courses/|Basys MX3 trainer board]] +  [[https://digilent.com/shop/basys-mx3-pic32mx-trainer-board-for-embedded-systems-courses/|Basys MX3 trainer board]] 
-  Workstation computer running Windows 10 or higher, MAC OS, or Linux  +  Workstation computer running Windows 10 or higher, MAC OS, or Linux  
-  [[http://store.digilentinc.com/usb-a-to-micro-b-cable/|Standard USB A to micro-B cables]]+  [[https://digilent.com/shop/usb-a-to-micro-b-cable/|Standard USB A to micro-B cables]]
  
 In addition, we suggest the following instruments: In addition, we suggest the following instruments:
-  [[http://store.digilentinc.com/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply/|Analog Discovery 2]] +  [[https://digilent.com/shop/analog-discovery-2-100ms-s-usb-oscilloscope-logic-analyzer-and-variable-power-supply/|Analog Discovery 2]] 
  
  
 ==== 3.2. Software ==== ==== 3.2. Software ====
 The following programs must be installed on your development work station: The following programs must be installed on your development work station:
-  [[http://www.microchip.com/mplab/mplab-x-ide|Microchip MPLAB X® v3.35 or higher]] +  [[http://www.microchip.com/mplab/mplab-x-ide|Microchip MPLAB X® v3.35 or higher]] 
-  [[http://www.microchip.com/SWLibraryWeb/product.aspx?product=PIC32%20Peripheral%20Library|PLIB Peripheral Library]] +  [[http://www.microchip.com/SWLibraryWeb/product.aspx?product=PIC32%20Peripheral%20Library|PLIB Peripheral Library]] 
-  [[http://www.microchip.com/xcdemo/xcpluspromo.aspx|XC32 Cross Compiler]] +  [[http://www.microchip.com/xcdemo/xcpluspromo.aspx|XC32 Cross Compiler]] 
-  [[http://store.digilentinc.com/waveforms-2015-download-only/|WaveForms 2015]] (if using the Analog Discovery 2) +  [[https://digilent.com/shop/software/digilent-waveforms/|WaveForms]] (if using the Analog Discovery 2) 
-  [[http://iowahills.com/|Iowa Hills Software for IIR and FIR Filters]]+  [[http://iowahills.com/|Iowa Hills Software for IIR and FIR Filters]]
  
  
Line 45: Line 46:
  
 ===== 4. Project Takeaways ===== ===== 4. Project Takeaways =====
-  How to implement digital filters in C using a PIC32 microprocessor. +  How to implement digital filters in C using a PIC32 microprocessor. 
-  How to use the PIC32 processor to make a signal generator. +  How to use the PIC32 processor to make a signal generator. 
-  How to create analog output using pulse-width modulation. +  How to create analog output using pulse-width modulation. 
-  How to use change frequency of synthesized signals.+  How to use change frequency of synthesized signals.
  
  
Line 82: Line 83:
 From the previous discussion, we can generate an oscillator of any frequency between zero and half the sampling frequency by using Eq. 5.2, or by placement of the poles in Eq. 5.3. In either case, the desired oscillator transfer function is computed using Eq. 5.4. Since the input to the digital filter is a unit impulse, it only exists for the computation of the y[1] output, which can be set as the digital filter initial condition without the need for an input signal at all. From the previous discussion, we can generate an oscillator of any frequency between zero and half the sampling frequency by using Eq. 5.2, or by placement of the poles in Eq. 5.3. In either case, the desired oscillator transfer function is computed using Eq. 5.4. Since the input to the digital filter is a unit impulse, it only exists for the computation of the y[1] output, which can be set as the digital filter initial condition without the need for an input signal at all.
  
-$$y[ n ] = -a_1 \cdot y [ n-1 ] - a_2 \cdot y [ n-2 ], \text{with} a_1 = -\cos \bigg( \frac{2\pi F_0}{Fs} \bigg), a_2 = 1, y [ 1 ] = \sin \bigg( \frac{2\pi F_0}{Fs} \bigg) \quad \text{and} \quad y [ 0 ] = 0 \text{for}+$$y[ n ] = -a_1 \cdot y [ n-1 ] - a_2 \cdot y [ n-2 ], \quad \text{with} \quad a_1 = -\cos \bigg( \frac{2\pi F_0}{Fs} \bigg), a_2 = 1, y [ 1 ] = \sin \bigg( \frac{2\pi F_0}{Fs} \bigg) \quad \text{and} \quad y [ 0 ] = 0, \quad \text{for}
 \quad n > 1 \qquad (\text{Eq. 5.4})$$ \quad n > 1 \qquad (\text{Eq. 5.4})$$
  
Line 120: Line 121:
   - Record the 8 sets of filter constants for the frequencies listed in Table 8.1.   - Record the 8 sets of filter constants for the frequencies listed in Table 8.1.
  
-{{ :learn:courses:unit-7-lab7a:lab7a-fig-8-1.png?nolink |Figure 8.1. Block diagram for Lab 7a.}}+{{ :learn:courses:unit-7-lab7a:lab7a-fig-8-1.png?nolink&700 |Figure 8.1. Block diagram for Lab 7a.}}
 //Figure 8.1. Block diagram for Lab 7a.// //Figure 8.1. Block diagram for Lab 7a.//
  
Line 134: Line 135:
 | SW6        | 6500Hz          |                          |                                                              | | SW6        | 6500Hz          |                          |                                                              |
 | SW7        | 7500Hz          |                          |                                                              | | SW7        | 7500Hz          |                          |                                                              |
-{{tag>reference learn microprocessor courses basys-mx3}}+ 
 +{{ :learn:courses:unit-7-lab7a:unit_7_-_photo_1.jpg?nolink&500 |Figure 8.2. Tone Generator display when not generating tone.}} 
 +//Figure 8.2. Tone Generator display when not generating tone.// 
 + 
 +{{ :learn:courses:unit-7-lab7a:unit_7_-_photo_2.jpg?nolink&500 |Figure 8.3. Tone Generator display when generating tone.}} 
 +//Figure 8.3. Tone Generator display when generating tone.// 
 + 
 +{{ :learn:courses:unit-7-lab7a:lab_7a_fig_8_4.png?nolink |Figure 8.4. Audio signal capture for a 4500 Hz synthesized signal.}} 
 +//Figure 8.4. Audio signal capture for a 4500 Hz synthesized signal.// 
 + 
 + 
 +The control flow diagrams shown in Fig. 8.5 and Fig. 8.6 give an overview of an approach to this design. The code provided in Listings A.1 through A.5 can be used for the sine wave generator portion of Fig. 8.1. The Analog Electronics portion of Fig. 8.1 is provided on the Basys MX3 circuit board as shown in Fig. B.1 of Appendix B. 
 + 
 +{{ :learn:courses:unit-7-lab7a:lab7b-fig-8-5.png?nolink |Figure 8.5. Main function control flow diagram.}} 
 +//Figure 8.5. Main function control flow diagram.// 
 + 
 +{{ :learn:courses:unit-7-lab7a:lab7b-fig-8-6.png?nolink |Figure 8.6. Timer 2 ISR.}} 
 +//Figure 8.6. Timer 2 ISR.// 
 + 
 +==== 8.2. Design, Construction, Testing ==== 
 +In previous labs, steps were laid out to lead the student through effective design, construction, and testing procedures. This is now left up to the student to complete on their own, ensuring the design requirements listed in section 8.1 are met. 
 + 
 + 
 +---- 
 + 
 +===== 9. Questions ===== 
 +  - Since the computed poles for the digital oscillator lie exactly on the unit circle, what affect do you expect truncation and round off errors to have on the oscillator output? 
 +  - How does the statistical integer rounding code used in the //gen_tones// function shown in Listing A.4 help prevent signal fading? (Hint: See what happens when you comment out that portion of that code and run the program.) 
 +  - Why is it preferable to synthesize a sine function rather than a cosine function? 
 +  - What precautions are necessary if a signal of more than one frequency is generated by adding outputs? 
 +  - What conclusion do you draw from the data recorded in Table 8.1? 
 + 
 + 
 +---- 
 + 
 +===== 10. References ===== 
 +  - Basys MX3 Trainer Board Reference Manual. 
 +  - [[http://www.w3schools.in/c-programming-language/intro/|C Programming Reference]]. 
 +  - PIC32 Family Reference Manual, Timers Section 14: [[http://ww1.microchip.com/downloads/en/DeviceDoc/61105E.pdf|http://ww1.microchip.com/downloads/en/DeviceDoc/61105E.pdf]]. 
 +  - Steven W. Smith, __The Scientist and Engineer’s Guide to Digital Signal Processing__, [[http://www.dspguide.com/pdfbook.htm|http://www.dspguide.com/pdfbook.htm]]. 
 +  - “Design of Digital Filters”, [[https://web.eecs.umich.edu/~fessler/course/451/l/pdf/c8.pdf|https://web.eecs.umich.edu/~fessler/course/451/l/pdf/c8.pdf]]. 
 +  - “A Fixed-Point Recursive Digital Oscillator for Additive Synthesis of Audio”, T. Hodes, J. Hauser, J. Wawrzynek, A. Freed, and D. Wessel, [[http://www.jhauser.us/publications/1999_Hodes_DigitalOscillator.pdf|http://www.jhauser.us/publications/1999_Hodes_DigitalOscillator.pdf]]. 
 +  - “An Implementation Method for Low Frequency Digital Oscillator”, L. Yan and H. Yang, [[http://ijssst.info/Vol-17/No-25/paper10.pdf|http://ijssst.info/Vol-17/No-25/paper10.pdf]]. 
 +  - The Synthesis of Sound by Computer, Section 4.2: Additive Synthesis, [[http://cmc.music.columbia.edu/musicandcomputers/chapter4/04_02.php|http://cmc.music.columbia.edu/musicandcomputers/chapter4/04_02.php]].  
 + 
 + 
 +---- 
 + 
 +===== Appendix A: Sine Wave Generator Software ===== 
 +==== Listing A.1. Definitions and Global Variable Declarations ==== 
 +<code> 
 +   #define PWM_CYCLE_RATE  160000                // PWM frequency  
 +   #define PWM_ZERO_OFFSET ((int) (PWM_CYCLE_RATE >> 1)) // 50/50 PWM output 
 +   #define PWM_MAX         ((int) ( GetPeripheralClock() / PWM_CYCLE_RATE )) 
 +   #define Q14             (1 << 14)             // Filter scale factor 
 +   #define PIx2            ((float) (3.14159265359 *2)) 
 +#define TONE   5000 // Test frequency 
 + 
 +   struct coefficients // IIR denominator coefficients 
 +   { 
 +       int a0; 
 +       int a1; 
 +       int a2; 
 +   } ; 
 + 
 +   typedef struct coefficients filter; 
 + 
 +// Public functions 
 + void audio_init(void); 
 + void start_tones(int tone); 
 + 
 +// Global variables 
 +static filter tone_filter; // Current Filter coefficients 
 +static filter filt_f1; // Computed filter coefficients 
 +int tones_active = FALSE; // Generate output control 
 +static int y[3] = {0}; // Filter output array 
 +</code> 
 + 
 +==== Listing A.2. Initialize Audio Output ==== 
 +<code> 
 +void audio_init(void) 
 +
 + // H(z) = z(sin(wo*T)) / (z^2 - 2*z*cos(wo*T) + 1) 
 + 
 +float w; 
 +   w = ((float) (TONE * pi2)) / PWM_CYCLE_RATE; 
 +   coeffs->a0 = (int) (sin(w) * Q14/2); // Limit initial impulse to 1/2 
 +   coeffs->a1 = (int) (2*cos(w) * Q14); 
 +   coeffs->a2 = Q14;  
 + 
 +   tones_active = FALSE;            // Disable Timer ISR oscillator 
 + 
 + // Initialize PWM output following the procedure used for Lab 6a but use RB14 
 + // for Output Compare Channel 1 
 + // Set the PWM period for PWM_CYCLE_RATE 
 + // Initialize PWM output for 50% duty cycle 
 +
 +</code> 
 + 
 +==== Listing A.3. Initialize Tone Filter ==== 
 +<code> 
 +void start_tones(int tone) 
 +
 +   tone_filter.a0 = filt_f1.a0; // Initialize filter coefficients 
 +   tone_filter.a1 = filt_f1.a1; 
 +   tone_filter.a2 = filt_f1.a2; 
 +   y[0] = tone_filter.a0;          // Initialize oscillator with impulse 
 +   y[1] = 0; 
 +   y[2] = 0; 
 +   tones_active = TRUE;            // Enable Timer ISR oscillator 
 +
 +</code> 
 + 
 +==== Listing A.4. Tone Generating IIR Filter ==== 
 +<code> 
 +static void gen_tones(void) 
 +
 +int pwm; 
 + 
 +// Oscillator IIR filter  
 +   y[2] = y[1];        // Shift outputs and compute new output  
 +   y[1] = y[0]; 
 + 
 +// Compute IIR result 
 +   y[0] = (tone_filter.a1 * y[1] - tone_filter.a2 * y[2]); 
 + 
 +// Provide rounding function to avoid truncations due to Fixed Point math because of // division by Q14 that causes oscillations to die out. 
 +   if(y[0] < 0) 
 +   { 
 +       y[0] = y[0] - Q14/2; 
 +   } 
 +   else 
 +   { 
 +       y[0] = y[0] + Q14/2;         
 +   } 
 + 
 +   y[0] = y[0] / Q14;   // Remove Q14 scaling for fixed point math 
 + 
 +// Compute new pwm output  
 +   pwm = (y[0] * PWM_MAX / Q14 ) + (PWM_MAX / 2); 
 + 
 +// Limit output range for 0 the maximum PWM 
 +   if(pwm < 0) 
 +   { 
 +       pwm = 0; 
 +   } 
 +   if(pwm > (PWM_MAX - 1) ) 
 +   { 
 +       pwm = PWM_MAX - 1 ; 
 +   } 
 +   SetDCOC1PWM(pwm); // Set PWM duty period        
 +
 +</code> 
 + 
 +==== Listing A.5. Tone Generating Output Control ==== 
 +<code> 
 +void __ISR(_TIMER_2_VECTOR, IPL2SOFT) Timer2Handler(void) 
 +
 +   if(tones_active == TRUE)    // Call IIR filter only if turned on 
 +       gen_tones(); 
 +   INTClearFlag( INT_T2 );     // Clear the interrupt flag  
 +} // End of Timer2Handler ISR 
 +</code> 
 + 
 + 
 +---- 
 + 
 +==== Appendix B: Audio Output Hardware ===== 
 +{{ :learn:courses:unit-7-lab7a:basys_mx3_schematic_audio.png?nolink |Figure B.1. Basys MX3 low-pass filter and audio amplifier.}} 
 +//Figure B.1. Basys MX3 low-pass filter and audio amplifier.// 
 + 
 + 
 + 
 +---- 
 + 
 +[[{}/learn/courses/unit-7/start|Back to Unit 7]] 
 +[[{}/learn/courses/unit-7-lab7b/start|Go to Lab 7b]] 
 +[[{}/learn/courses/start|Back to Course List]]