Spectrum analyzer based PIC18F4550. Part 2


In the first part Spectrum analyzer based PIC18F4550. Part 1, we met with the scheme, with the appointment of individual nodes and elements, over the principles of operation.
Microcontroller software is written entirely in C language, and it can be conditionally divided into 4 parts.


1. ADC sample data (sampling)

Implementation of sampling analog-digital converter is the channel every 50 ms RA0. This gives us a sampling frequency of 20 kHz (20,000 times per second). For the Fourier transform is very important that the samples were uniform and accurate. With this purpose in the subroutine get data from ADC introduced a short time delay, which was calibrated using an oscilloscope connected to the control point W4 on the board. Square-wave period as measured in this reference point should be exactly 50 microseconds. ADC works with a full 10-bit resolution, and the results are shifted down to 512 to implement a "virtual earth" input signal. This means that the resulting data will be in the range from -512 to 512, as required for the mathematical transformation FFT.

The procedure for obtaining the samples will take a little time to 32 ms (64 × 50 microseconds) for each cycle.

2. 16-bit FFT

Subroutine, which implements the calculation algorithm FFT, was found on the Web so (link to the original code is in the source code at the end of the article). Math FFT is complex and difficult to understand. The code has been slightly simplified and adapted for the PIC18F4550. Since the microcontroller has a hardware multiplier 8 × 8 as part of its arithmetic and logic devices have been optimized computing, allowing the compiler to correctly use the features of the microcontroller.

3. The calculation of the absolute values

The result of calculations by fast Fourier transform algorithm - a complex number, which consist of real and imaginary parts of two arrays. To get the result in a meaningful form, we need to compute the absolute value of complex numbers that we get using the Pythagorean calculation for complex numbers. This means the square root of the numbers, so we implement fast integer computation SQRT (), as any floating point calculations considerably slow down the process.

FFT algorithm and the computation of the absolute values ​​of complex numbers occupy approximately 70 ms for each cycle.

4. Update LCD display

LCD display with a resolution of 128 × 64 pixels should be updated as quickly as possible. For this purpose, a simple algorithm was used drawing diagrams in the form of columns, which requires a minimum number of teams display.

Two switches are installed on the motherboard allow you to change the display scale (1 ×, 8 ×) and display mode (linear, logarithmic). Sub-display service is on time 45 ms.

Approximate speed of the display of a spectrum analyzer 1 frame for 150 ms, ie we obtain as a result 6.5 frames per second (or approximately 10 frames per second without using the LCD). Users can increase performance by reducing the amount needed to analyze the frequency (this leads to a reduction in the time of receipt of samples and computation algorithm FFT) or by using a high-speed display. In addition, users can alter the device for use in conjunction with LEDs.

The limiting frequency for the Fourier transform (the Nyquist frequency, Nyquist frequency) - 10 kHz. The device analyzes the frequency range of 32 (Hz):

1: 312.5 - 625
2: 625 - 937.5
3: 937.5 - 1250
4: 1250 - 1562.5
5: 1562.5 - 1875
6: 1875 - 2187.5
7: 2187.5 - 2500
8: 2500 - 2812.5
9: 2812.5 - 3125
10: 3125 - 3437.5
11: 3437.5 - 3750
12: 3750 - 4062.5
13: 4062.5 - 4375
14: 4375 - 4687.5
15: 4687.5 - 5000
16: 5000 - 5312.5
17: 5312.5 - 5625
18: 5625 - 5937.5
19: 5937.5 - 6250
20: 6250 - 6562.5
21: 6562.5 - 6875
22: 6875 - 7187.5
23: 7187.5 - 7500
24: 7500 - 7812.5
25: 7812.5 - 8125
26: 8125 - 8437.5
27: 8437.5 - 8750
28: 8750 - 9062.5
29: 9062.5 - 9375
30: 9375 - 9687.5
31: 9687.5 - 10000
NOTE: Nyquist frequency - the frequency of the analog signal, it provides a correct conversion to digital form. In the Nyquist sampling theorem was proved that the sampling rate must be at least twice the highest frequency signal being processed, so you can restore the original signal - so, for sampling a voice signal having a bandwidth of 4 kHz, 8 kHz frequency is needed in Otherwise, the inevitable distortions when playing back the signal in digital form.

Downloads

The source code for the microcontroller (compiler HiTech C18) - download

Search here!