circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 

September 1998, Issue 98

PIC'Spectrum
Audio Spectrum Analyzer


by Robert Lacoste

ON THE SOFTWARE FRONT

Of course, when you choose the simplest possible hardware, the software has more to do. Here, the software must:

• do the acquisition of a burst of the analog signal (typically 256 samples at a sampling frequency of 16 kHz)

• calculate the FFT of these 256 samples, giving an amplitude for each of the 128 frequencies

• calculate the power of each frequency and scale it (depending on the position of the log/lin switch)

• manage the run/hold switch

• do the generation of the VGA video signal in parallel

To do all these tasks while keeping real-time requirements, PIC’Spectrum software is divided into two tasks. The main program is in charge of initialization, device control, and numerical algorithms. The interrupt routine, executed each 31.77 µs (corresponding to the VGA horizontal video synchronization period), is in charge of analog signal acquisition and video generation.

These two tasks dialog through three RAM shared structures:

• FFT buffer (256 words, 16 bits each), filled by the interrupt routine with analog samples and used by the main program for FFT calculation

• display buffer (128 bytes), filled by the main program with the length (in pixels) of each of the 128 horizontal frequency bars, and used by the interrupt routine for video generation

• synchronization variables

Figure 5 illustrates the information flow. Since you can get the complete source code from the Circuit Cellar Web site, I’ll focus here under on the more specific codes, like FFT calculation and software video generation.

9805021-Fig 5.gif (17920 bytes)

Figure 5—The main task is in charge of the numerical algorithms, while a timer-driven interrupt routine handles analog signal acquisition and video generation. The tasks communicate through a shared memory.

A standard FFT algorithm works on complex numbers (real plus imaginary parts). It takes as an input an array of n complex samples and gives an array of n complex frequency amplitudes. Here, the input signal is of course only real numbers.

The immediate solution to get its FFT is to add a zero imaginary part to each sample and to use the standard complex numbers algorithm. If you do that, only half of the n frequencies in the result are useful (in fact, each value is found twice).

This is a consequence of the well-known Nyquist rule. If you sample your signal at a period of 1/n, you can only analyze frequencies with periods above 2/n. More problematic, this simple approach uses twice as much memory as is useful and we have only 902 bytes of RAM.

Fortunately, you can use a more sophisticated algorithm, known as real-mode FFT. The idea is to pack two real samples in each complex input value, do a standard complex FFT, and decrypt the resulting complex values to find back the good real figures. You can get the details from Numerical Recipes in C: The Art of Scientific Computing [3].

To implement this FFT algorithm on the PIC, I developed a fixed-point mathematical library (source file is fixed.inc). It implements a virtual fixed-point machine operating on two 16-bit floating-point registers (RA and RB).

Routines are available for addition, subtraction, multiplication, division, sine, and logarithm, as well as access to the banked RAM. The fixed-point format used is S/2/13 (one sign bit, 2 bits for the integer part, and 13 bits for the fractional part). In fact, writing sinus and logarithm calculation routines in assembler is quite an interesting experience.