Summary of Implementing Discrete Fourier Transform in Atmega32 to make an audio spectrum analyzer
This article details a DIY audio spectrum analyzer built with an ATmega32 microcontroller and a 16x2 LCD. The author implements a 32-point Discrete Fourier Transform (DFT) to visualize audio frequencies from 0 to 10KHz at approximately 100 frames per second. To optimize performance on an 8-bit AVR, the project uses integer-based lookup tables for sine and cosine calculations instead of floating-point math. The hardware setup includes an electret microphone pre-amplified by an LM324 op-amp, ensuring the ADC input stays within safe voltage limits. Custom LCD characters are utilized to display the frequency bars efficiently.
Parts used in the Audio Spectrum Analyzer:
- ATmega32 microcontroller
- 16x2 character LCD
- Electret microphone
- LM324 op-amp
- Sine lookup table
- Cosine lookup table
- Angle lookup table
“All waveforms, no matter what you scribble or observe in the universe, are actually just the sum of simple sinusoids of different frequencies.”
Hi,
I am just refreshing the basics of fourier transform. I am not an expert. Now I did a small audio spectrum analyzer(0 – 10KHz) on a 16×2 character lcd using an atmega32 microcontroller. Since I am refreshing from the basics, so I started with simple DFT. Also, I believe I should learn to walk before running. So I am not straight away going towards the FFT, which is nothing but the fastest and a bit complicated algorithm to find DFT.(I will try it later, as soon as possible)
DFT is too slow compared to FFT. My lcd spectum analyzer doesn’t need a great speed like that of an FFT, now if it is capable of providing a speed of around 30 frame/second, then it is more than enough for visualizing the audio spectrum on an LCD. But any way, in my case I can roughly achieve around 100 frames/second(any way it is too high refresh rate for a 16×2 lcd, not recommended also :-)). My audio sampling rate is 20KHz for 32 point DFT. Since the transform result is symmetric, I need to use only the first half, ie the first 16 results. So, it means, it can display upto 10KHz spectrum. So the resolution is 10KHz/16 = 625Hz.
I have tried to improve the speed of DFT computation. If it is an N point DFT, it needs to find (N^2)/2 sin and cos values. For a 32 point DFT, it needs to find 512 sine and cosine. Before finding the sine and cosine, we need to find the angle(degree) which takes some processor time, so I implemented a lookup table for that. Next two tables are for sine and cosine. I didn’t used any float or double since it takes more processing time in 8 bit avr, instead I implemented the sine and cosine lookups as 16bit intiger, by multiplying the real sine and cosine values by 10000. Then after finding the transform, finally I need to divide each result by 10000. This eliminates the need of using float or double and makes it more faster. Now I can calculate 120 32-point DFT operation/sec which is more than enough for my small spectrum analyzer.
LCD
Now, looking towards the LCD side, I utilized the custom character feature of LCD to make 8 stacked horizontal bars which takes the entire 64bytes of the LCD RAM for custom character bitmap. I ones seen a video is hackaday.com that a person used a 16×2 lcd in the similar manner for his spectrum analyser. So I also adopted the same idea of using the custom character for my spectrometer.
AUDIO INPUT
Now one of the most important part of this stuff is the audio sampling via an eletret microphone. Special care must be given while designing the pre-amp for the mic. We need to set the zero-level of the ADC input to exactly half of the ADC reference voltage ie to 2.5v. Now it can have positive and negative swing on this 2.5v level according to the input audio signal but it should not cross the limit ie the amplifier gain should be properly adjusted to prevent clipping. I am using an LM324 op-amp for the mic pre-amp to meet the above conditions. To continue reading click here.
- Why did the author choose DFT over FFT?
The author believes in learning basics first and notes that DFT is sufficient for their required speed of around 30 frames per second. - What sampling rate was used for the 32-point DFT?
A sampling rate of 20KHz was used to achieve a spectrum analysis range up to 10KHz. - How does the system handle the symmetry of the DFT result?
Since the transform result is symmetric, only the first 16 results out of 32 are used for display. - How were sine and cosine calculations optimized for the 8-bit AVR?
The author implemented 16-bit integer lookups multiplied by 10000 to avoid slow float or double processing. - What technique was used to display the spectrum on the LCD?
Custom characters were created using the entire 64 bytes of LCD RAM to form 8 stacked horizontal bars. - What is the target zero-level for the ADC input voltage?
The zero-level must be set exactly to half of the ADC reference voltage, which is 2.5V. - Which operational amplifier is used for the microphone pre-amp?
An LM324 op-amp is used to design the pre-amp and prevent signal clipping.