Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| learn:courses:unit-7-lab7b:start [2017/03/20 20:27] – created Martha | learn:courses:unit-7-lab7b:start [2022/10/04 11:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Lab 7b: Digital Spectrum Analyzer ====== | ====== Lab 7b: Digital Spectrum Analyzer ====== | ||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | |||
| === Download This Document === | === Download This Document === | ||
| - | * {{ : | + | {{ : |
| + | |||
| + | |||
| + | ===== 1. Objectives ===== | ||
| + | - Read analog data from the Basys MX3 microphone. | ||
| + | - Process a block of analog data with a Discrete Fourier Transform algorithm. | ||
| + | - Use the Basys MX3 Character LCD to display graphical data. | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 2. Basic Knowledge ===== | ||
| + | - [[http:// | ||
| + | - [[http:// | ||
| + | - How to display text on a character LCD | ||
| + | - [[http:// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 3. Equipment List ===== | ||
| + | ==== 3.1. Hardware ==== | ||
| + | - [[https:// | ||
| + | - Workstation computer running Windows 10 or higher, MAC OS, or Linux | ||
| + | - 2 [[https:// | ||
| + | |||
| + | In addition, we suggest the following instruments: | ||
| + | - [[https:// | ||
| + | |||
| + | |||
| + | ==== 3.2. Software ==== | ||
| + | The following programs must be installed on your development work station: | ||
| + | - [[http:// | ||
| + | - [[http:// | ||
| + | - [[http:// | ||
| + | - [[https:// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 4. Project Takeaways ===== | ||
| + | - How to implement a fast Fourier transform in C using a PIC32MX microprocessor. | ||
| + | - How to use the PIC32 processor to analyze a signal in real time. | ||
| + | - How to display the spectral energy of a real-time signal. | ||
| + | - How to generate a graphical display on a character LCD. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 5. Fundamental Concepts ===== | ||
| + | This unit focuses on processing signals in the audio frequency range using [[https:// | ||
| + | |||
| + | ==== 5.1. Feedback Control ==== | ||
| + | The [[https:// | ||
| + | |||
| + | $$x(t) = A_m \sin (2\pi f_0 t + \varphi) \qquad (\text{Eq. 5.1})$$ | ||
| + | |||
| + | $$X(f) = \int_{t=-\infty}^\infty x(t) \cdot e^{-j 2 \pi t f_0} df \qquad (\text{Eq. 5.2})$$ | ||
| + | |||
| + | ==== 5.2. Discrete Fourier Transform ==== | ||
| + | When the continuous time domain signal of Eq. 5.1 is sampled, we get a series of discrete data values as a function of the sample number, as shown in Eq. 5.3. Eq. 5.4 is the expression for converting the series of sampled inputs into a set of complex values representing the phase and magnitude of the original signal at discrete frequencies. Since $x(n)$ represents samples of a continuous signal at discrete points in time, $X_k$ represents the same signal as discrete frequencies, | ||
| + | |||
| + | There is a one to one correlation of the range of $n$ to the range of $k$. This means that processing $N$ samples of data will result in the magnitude and phase of $N$ discrete frequency components, starting with the sampling frequency, zero and continuing to $Fs/2$ in steps of $Fs/N$. | ||
| + | |||
| + | $$x(n) = A_m \sin (2 \pi fnT + \varphi) \qquad (\text{Eq. 5.3})$$ | ||
| + | |||
| + | $$X_k = \sum_{n=0}^{N-1} x_n e^{-j 2\pi k \frac{n}{N}} = \sum_{n=0}^{N-1} x_n \cdot \bigg[ \cos \bigg( \frac{2\pi kn}{N} \bigg) - j \sin \bigg( \frac{2\pi k n }{N} \bigg) \bigg] \qquad (\text{Eq. 5.4})$$ | ||
| + | |||
| + | While Eq. 5.4 is the analysis equation, Eq. 5.5 is the synthesis equation. Note that the only differences between Eq. 5.4 and Eq. 5.5 are the (1/N) multiplier and the sign of the complex exponent of $e$. A complex signal can be represented in the form of the summation of a selected set of complex pairs, or magnitude-phase pairs, that can be used to reconstruct a very large original time domain signal, thus constituting a form of data compression. | ||
| + | |||
| + | $$x_n = \frac{1}{N} \sum_{k=0}^{N-1} X_k e^{j2 \pi k \frac{n}{N}} \qquad (\text{Eq. 5.5})$$ | ||
| + | |||
| + | Listing A.1 of Appendix A is the C code for implementing the algorithm of Eq. 5.4 in the form of a non-recursive, | ||
| + | |||
| + | ==== 5.3. Fast Fourier Transform C Code ==== | ||
| + | A [[https:// | ||
| + | |||
| + | As the famous infomercials shout, “Wait! That’s not all!” Using the MIPS DSP library // | ||
| + | |||
| + | Listing A.5 declares the coefficient array in the statement, //#define fftc fft16c32//, which declares an array of 32 16-bit signed constants that are pre-defined FFT coefficients. In Listing A.6, the line of code // | ||
| + | |||
| + | Since all of the DFT algorithms described in Listing A.1 through return arrays that contain complex values, they must be converted to real values to be usable for graphical LCD display. Listing 6.7 provides the source code for converting a pair of complex integer values to a real integer. Tests have shown that the execution time of integer conversion is three time faster than using a floating point square root function. See Reference 6 for additional details regarding this algorithm. | ||
| + | |||
| + | The DFT results cover the span of zero to $2\pi$, corresponding to zero to the sampling frequency, Fs. Each output represents the signal frequency in steps of $Fs/N (\Delta N)$ where N is the number of samples being processed. The DFT output is only valid for the first $n = 0$ to $N/2 – 1$ frequencies, | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 6. Problem Statement ===== | ||
| + | The objective for this lab is to create a graphical spectrum analyzer as modeled in Fig. 8.1. Figure 8.2 shows the LCD and 7-segment display for the result outputs. Figure 8.3 through 8.5 provide an approach to the software organization for this lab. Figure 8.4 shows the tasks to be completed for the system initialization. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 7. Background Information ===== | ||
| + | Since DFT algorithms are essentially FIR digital filters, refer to Unit 7 section 6.3.1 for background information. | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 8. Lab 7b ===== | ||
| + | ==== 8.1. Requirements ==== | ||
| + | - Set Time 2 to sample the microphone input at the rate of 16 kHz. | ||
| + | - Implement a [[https:// | ||
| + | - Compute the 16 FFT outputs using the code provided in Appendix A Listings A.5 through A.7. | ||
| + | - Using custom LCD characters, display the energy at the 15 frequencies corresponding to zero to 7500 Hz with 500 Hz intervals. (See Reference 7 and 8.) | ||
| + | - Display the maximum FFT frequency on the 7-segment display provided the detected energy level is above a minimum set point. | ||
| + | - Update the display every 50ms. | ||
| + | - Adjust the MIC Volume control for maxim display for a reasonable volume tone. (Suggestion: | ||
| + | |||
| + | {{ : | ||
| + | //Figure 8.1. Block diagram of a digital spectrum analyzer.// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 8.2. LCD and 7-segment display for spectrum analyzer.// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 8.3. Control flow diagram for LCD spectrum analyzer.// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 8.4. Control flow diagram for LCD spectrum analyzer initialization.// | ||
| + | |||
| + | {{ : | ||
| + | //Figure 8.5. ADC complete ISR.// | ||
| + | |||
| + | ==== 8.2. Design, Construction, | ||
| + | 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 ===== | ||
| + | - Theoretically, | ||
| + | - Theoretically, | ||
| + | - In reality, what is the maximum rate that the LCD and 7-Segment display can be updated? | ||
| + | - Why would one want to update the displays at a rate slower than the theoretical maximum speed? | ||
| + | - How many times will a buffer be filled during the time needed to display the results for computing the FFT of one block of data? What are the potential problems here? | ||
| + | - How can your answer to question 5 be resolved? | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== 10. References ===== | ||
| + | - Basys MX3 Trainer Board Reference Manual. | ||
| + | - [[http:// | ||
| + | - PIC32 Family Reference Manual, Timers Section 14: [[http:// | ||
| + | - Steven W. Smith, __The Scientist and Engineer’s Guide to Digital Signal Processing__, | ||
| + | - Cooley-Tukey FFT for 16-bit Integer Numbers, Microchip Technology, [[http:// | ||
| + | - Calculate an integer square root, [[http:// | ||
| + | - Printing Custom Characters on a Character LCD, [[http:// | ||
| + | - Getting Graphic; Defining Custom LCD Characters, [[https:// | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix A: Discrete Fourier Transform Software ===== | ||
| + | ==== Listing A.1. Code to Implement a DFT Based on Eq. 5.4 ==== | ||
| + | < | ||
| + | #define FFT_SIZE ### | ||
| + | void DFT(int16_t *x, int16_t *y_re, int16_t *y_im) | ||
| + | { | ||
| + | int k,n; | ||
| + | float w = PIx2/ | ||
| + | |||
| + | | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Listing A.2. Constant Declarations for Block Processing C for FFT Algorithm ==== | ||
| + | < | ||
| + | #define FFT_SIZE ### | ||
| + | const int16_t bitPermutationTable[FFT_SIZE] = | ||
| + | {0, | ||
| + | 4, | ||
| + | 23, | ||
| + | </ | ||
| + | |||
| + | ==== Listing A.3. Block Processing C for FFT Algorithm ===== | ||
| + | < | ||
| + | |||
| + | void FFT(int16_t* bufferRe, int16_t* bufferIm) | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | bl = FFT_SIZE; | ||
| + | | ||
| + | | ||
| + | |||
| + | while (p > 0) | ||
| + | { | ||
| + | for (m=0; m<p; m++) | ||
| + | { | ||
| + | j = m << k; // coefficient offset | ||
| + | wRe = twiddleCoefficients[(j + FFT_SIZE / 4)]; | ||
| + | wIm = -twiddleCoefficients[j]; | ||
| + | |||
| + | for (i=m; i< | ||
| + | { | ||
| + | j = i + p; // i - offset for the first point | ||
| + | // j - offset for the second point | ||
| + | FFT2(bufferRe+i, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | k++; | ||
| + | p >>= 1; | ||
| + | bl >>= 1; | ||
| + | } | ||
| + | |||
| + | | ||
| + | { | ||
| + | j = bitPermutationTable[i]; | ||
| + | |||
| + | if(j <= i) | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | |||
| + | temp = bufferRe[i]; | ||
| + | | ||
| + | | ||
| + | |||
| + | temp = bufferIm[i]; | ||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Listing A.4. Butterfly Computation for Block Processing for FFT Algorithm ==== | ||
| + | < | ||
| + | void FFT2(int16_t *ReA, int16_t *ImA, int16_t *ReB, int16_t *ImB, | ||
| + | | ||
| + | { | ||
| + | int w0, w1, w2, w3, w6; | ||
| + | |||
| + | // Scale for Q1.15 | ||
| + | w0 = *ReA >> 1; | ||
| + | w1 = *ImA >> 1; | ||
| + | w2 = *ReB >> 1; | ||
| + | w3 = *ImB >> 1; | ||
| + | |||
| + | // ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| + | // PLUS PATH | ||
| + | // ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| + | // SAVE REAL RESULT | ||
| + | // w6 = w0 + w2; | ||
| + | *ReA = (int16_t) (w0 + w2); | ||
| + | |||
| + | // SAVE IMAGINARY RESULT | ||
| + | // w7 = w1 + w3; | ||
| + | *ImA = (int16_t) (w1 + w3); | ||
| + | |||
| + | //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| + | // MINUS PATH | ||
| + | //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||
| + | |||
| + | w0 = w0 - w2; | ||
| + | w1 = w1 - w3; | ||
| + | |||
| + | // multiply by coefficient | ||
| + | w2 = w0 * Wr; | ||
| + | w6 = w1 * Wi; | ||
| + | w2 = (w2 - w6) >> 15; | ||
| + | |||
| + | // SAVE RESULT W3 HERE (REAL) | ||
| + | // w2 = w2 >> 15; | ||
| + | *ReB = (int16_t) w2 ; | ||
| + | |||
| + | // multiply by coefficient | ||
| + | w2 = w0 * Wi; | ||
| + | w6 = w1 * Wr; | ||
| + | w2 = (w2 + w6) >> 15; | ||
| + | |||
| + | // SAVE RESULT W3 HERE (IMAGINARY) | ||
| + | *ImB = (int16_t) w2; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Listing A.5. Global Declarations for MIPS DSP Library FFT Algorithm ==== | ||
| + | < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Define FFT coefficients pre calculated in DSP lib | ||
| + | #define fftc fft16c32 | ||
| + | |||
| + | #define LOG2FFTLEN | ||
| + | #define FFTLEN | ||
| + | </ | ||
| + | |||
| + | ==== Listing A.6. MIPS DSP Library FFT Algorithm ==== | ||
| + | < | ||
| + | void FFT(int16c *FOut, int16c *TIn) | ||
| + | { | ||
| + | int16c Scratch[FFTLEN]; | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Listing A.7. Absolute Value of a Complex Variable ==== | ||
| + | < | ||
| + | / | ||
| + | A = SQRT(Re^2 + Im^2) | ||
| + | http:// | ||
| + | *************************************************************************/ | ||
| + | uint16_t FFTAmplitude(int16_t re, int16_t im) | ||
| + | { | ||
| + | uint32_t _bit = ((uint32_t)1) << 30; | ||
| + | uint32_t num; | ||
| + | uint32_t res = 0; | ||
| + | |||
| + | num = ((uint32_t)re)*re + ((uint32_t)im)*im; | ||
| + | |||
| + | // 32 bit integer square root calculation. | ||
| + | while (_bit > num) | ||
| + | _bit >>= 2; | ||
| + | |||
| + | while (_bit != 0) | ||
| + | { | ||
| + | if (num >= res + _bit) | ||
| + | { | ||
| + | num -= res + _bit; | ||
| + | res = (res >> 1) + _bit; | ||
| + | } | ||
| + | | ||
| + | res >>= 1; | ||
| + | _bit >>= 2; | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Appendix B: Audio Output Hardware ===== | ||
| + | {{ : | ||
| + | //Figure B.1. Basys MX3 Microphone Schematic.// | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[{}/ | ||
| + | [[{}/ | ||
| + | [[{}/ | ||

