FFT Audio Spectrum Analysis

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
FFT Audio Spectrum Analysis
One more of my crazy projects involving some basic FFT (DFT) convolution filters.
To begin with, what we want to do is to build a device that will graphically display the spectral output of an audio signal in near realtime. It is "near" realtime because the display always lags realtime by the amount of time required to convolve the signal through the FFT filter.
Since this is an audio device, we can expect the frequency range to go from 20Hz to 20kHz. According to the ShannonNyquist sampling theorem, our sampling frequency is going to have to be 40ksps and above. We will also need to include a lowpass filter to remove frequency components above half our sampling rate.
Secondly, the size of the sampled array will determine the minimum frequency resolution, so for a frequency of 1Hz, you will need at least 20,000 samples at a 20ksps rate to fully represent the 1Hz waveform. This translates to a lot of memory requirement  40kB to be precise, if we are using a 12bit ADC. Since none of the dsPIC's we have can handle such an array (not to mentioned the memory required to hold the filter's frequency response output array), we will certainly have to limit our maximum frequency to something less than 20kHz or accept a lower frequency resolution. For example, a 10Hz resolution would require only 2kB to hold.
Another consideration is the response time. A large array will take a longer time to convolve so we are going to have to pick something reasonable. At this point, most of the FFT algorithms are optimized for around 256points which is good since our VFD display has exactly that horizontal resolution. Assuming that we go with a 256point filter and a 20kHz fmax, we will end up with a resolution of 78.125Hz. This seems reasonable enough. Even so, there is a question of whether an LPF is needed either at the input or as a digitalfilter (depending on whether there is enough CPU power to do the FIR/IIR and the FFT in a timely manner).
*update: In retrospect, you can't use a digital filter to get around the Nyquist limit. The FIR/IIR will have no way of distinguishing the aliased signal from a real one. What this means is that we will need a real LPF at the input of the ADC to do this right.
Daniel
Reference: http://www.dspguide.com/
To begin with, what we want to do is to build a device that will graphically display the spectral output of an audio signal in near realtime. It is "near" realtime because the display always lags realtime by the amount of time required to convolve the signal through the FFT filter.
Since this is an audio device, we can expect the frequency range to go from 20Hz to 20kHz. According to the ShannonNyquist sampling theorem, our sampling frequency is going to have to be 40ksps and above. We will also need to include a lowpass filter to remove frequency components above half our sampling rate.
Secondly, the size of the sampled array will determine the minimum frequency resolution, so for a frequency of 1Hz, you will need at least 20,000 samples at a 20ksps rate to fully represent the 1Hz waveform. This translates to a lot of memory requirement  40kB to be precise, if we are using a 12bit ADC. Since none of the dsPIC's we have can handle such an array (not to mentioned the memory required to hold the filter's frequency response output array), we will certainly have to limit our maximum frequency to something less than 20kHz or accept a lower frequency resolution. For example, a 10Hz resolution would require only 2kB to hold.
Another consideration is the response time. A large array will take a longer time to convolve so we are going to have to pick something reasonable. At this point, most of the FFT algorithms are optimized for around 256points which is good since our VFD display has exactly that horizontal resolution. Assuming that we go with a 256point filter and a 20kHz fmax, we will end up with a resolution of 78.125Hz. This seems reasonable enough. Even so, there is a question of whether an LPF is needed either at the input or as a digitalfilter (depending on whether there is enough CPU power to do the FIR/IIR and the FFT in a timely manner).
*update: In retrospect, you can't use a digital filter to get around the Nyquist limit. The FIR/IIR will have no way of distinguishing the aliased signal from a real one. What this means is that we will need a real LPF at the input of the ADC to do this right.
Daniel
Reference: http://www.dspguide.com/

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
Okay, so the basic idea behind this project is to transform the input signal from the time domain into the frequency domain using some form of DFT. This can be done thanks to Fourier's discovery of signal decomposition into sinusoids of fixed periods but varying amplitudes. To put it in another way, the spectrum output is simply a graphical representation of the output of the DFT.
Also a slight correction to the previous post about FFT points, technically what we meant there were bins. The relationship between the sample points N and the output bins for the FFT is that there will be N/2 output bins. Consequently, for a 256point sample input of x[], you will end up with N/2 output bins of X[] (frequency domain). So a 40ksps input rate will yield a 20,000 bin output or a 2Hz bin size. This is probably overkill but we have to remember the NyquistShannon limit. A 256point input will result in 128 bins of about 156.25Hz or so each  assuming the bins are linearly distributed which is probably not the case. In fact it is linear except that it is represented as a fraction of the sampling frequency. It is the limits of these fractions (from 0 onwards to 0.5) that determine the bin sizes. As such, the bin size has a direct correlation to the sampling rate and the highest bin is half of the sampling frequency. The number of bins has to do with the number of points within the sample (which is also related to the rate). So a bigger sample size will yield smaller bins.
In summary, what determines the DFT output that is of interest to us are the sampling rate and the sample size. The bins represent the basis functions and must be unique in such a way that none can contain frequency components of another bin  ie. basis function orthogonality.
binsize = samplerate / N
where N is the FFT size.
Daniel
Also a slight correction to the previous post about FFT points, technically what we meant there were bins. The relationship between the sample points N and the output bins for the FFT is that there will be N/2 output bins. Consequently, for a 256point sample input of x[], you will end up with N/2 output bins of X[] (frequency domain). So a 40ksps input rate will yield a 20,000 bin output or a 2Hz bin size. This is probably overkill but we have to remember the NyquistShannon limit. A 256point input will result in 128 bins of about 156.25Hz or so each  assuming the bins are linearly distributed which is probably not the case. In fact it is linear except that it is represented as a fraction of the sampling frequency. It is the limits of these fractions (from 0 onwards to 0.5) that determine the bin sizes. As such, the bin size has a direct correlation to the sampling rate and the highest bin is half of the sampling frequency. The number of bins has to do with the number of points within the sample (which is also related to the rate). So a bigger sample size will yield smaller bins.
In summary, what determines the DFT output that is of interest to us are the sampling rate and the sample size. The bins represent the basis functions and must be unique in such a way that none can contain frequency components of another bin  ie. basis function orthogonality.
binsize = samplerate / N
where N is the FFT size.
Daniel

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
AntiAliasing Filters
I've mentioned the need for antialiasing filters, basically a lowpass filter to cut out frequency components above the Nyquist frequency so as not to corrupt the sampled data. The thing here, though, is that if we oversample at a high enough frequency, you gain the benefit of the decimation of the samples which has the effect of an LPF, as well as raising the image frequency so high that it is unlikely to find such spurious signal in the source signal.
So, for example our signal is F1 and our sampling frequency is F2 and the unwanted frequency is F3:
F1+F3 sampled at F2 gives F2+F3, F2F3 as sideband products. When F2 is very high, and F3 relatively low  these sideband products do not show up in the F1 frequency band. So in theory, we may not really need an antialiasing filter if the F2 can be made significantly higher and then decimated down.
Daniel
I've mentioned the need for antialiasing filters, basically a lowpass filter to cut out frequency components above the Nyquist frequency so as not to corrupt the sampled data. The thing here, though, is that if we oversample at a high enough frequency, you gain the benefit of the decimation of the samples which has the effect of an LPF, as well as raising the image frequency so high that it is unlikely to find such spurious signal in the source signal.
So, for example our signal is F1 and our sampling frequency is F2 and the unwanted frequency is F3:
F1+F3 sampled at F2 gives F2+F3, F2F3 as sideband products. When F2 is very high, and F3 relatively low  these sideband products do not show up in the F1 frequency band. So in theory, we may not really need an antialiasing filter if the F2 can be made significantly higher and then decimated down.
Daniel
 Attachments

 EEIOL_2009JAN22_INTD_TA_01.pdf
 (120.52 KiB) Downloaded 537 times

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
Given that most of the audio signals we normally find are AC coupled, we will need a levelshifter circuit (simple summing opamp with unity gain) at the input. This gives us the opportunity to control the gain with some of the spare opamps as well as to implement some lowpass filtering if we so choose. I don't think signal inversion should be an issue so we can choose to use the simpler inverting configuration rather than the slightly more involved noninverting configuration. Alternatively we can double invert if the filter uses an inverting configuration after we have a inverting gain control stage.
Going back to the filter algorithm, I've noticed that the Hannwindowing scheme appears to be more popular than other windowing schemes, such as Hamming or Blackman. Is there a reason for this choice? In any case, along with the input gain control, it is important to scale the sampled data to prevent overflow or saturation of the filter.
*Note: The output may need to be scaled logarithmically to make it look better.
*Note2: Due to the linearity of FFT functions, and the fact that our output will not display more than 32levels (or is it 64levels), it is pointless to use the full 12bit resolution for the audio samples. 8bits could be more than sufficient.
Daniel
Going back to the filter algorithm, I've noticed that the Hannwindowing scheme appears to be more popular than other windowing schemes, such as Hamming or Blackman. Is there a reason for this choice? In any case, along with the input gain control, it is important to scale the sampled data to prevent overflow or saturation of the filter.
*Note: The output may need to be scaled logarithmically to make it look better.
*Note2: Due to the linearity of FFT functions, and the fact that our output will not display more than 32levels (or is it 64levels), it is pointless to use the full 12bit resolution for the audio samples. 8bits could be more than sufficient.
Daniel

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
I've been tinkering with the input amplifier and level shifter. I'm thinking that in our application, phase inversion should not be a problem so I've used an inverting amplifier with a level shifter to Vcc/2. The SPICE simulation suggests that our circuit will work as expected.
Given that we are sampling at 40ksps, the minimum slew rate required at 3.3V is given as 2*PI*f*V giving us 0.83V/usec. Since our opamp has a 1.6V/usec SR, we should be good here.
Reference: http://hyperphysics.phyastr.gsu.edu/HB ... 741p3.html
We may want to consider adding another opamp to implement a twopole Butterworth filter for antialiasing with a cutoff frequency of around 16kHz since we don't really need anything higher than that for our purposes. To get a cutoff frequency of 16kHz, R1=R2=4.7k, C2=1.5nF, C1=3nF.
Reference: http://www.radioelectronics.com/info/c ... filter.php
Daniel
Note that older opamps such as the venerable 741 and such just won't cut it for our application due to the supply requirements. Our circuit has a modest 1.5x gain as a test of the shifting and all seems to work. We can change the gain later if we so wish.Given that we are sampling at 40ksps, the minimum slew rate required at 3.3V is given as 2*PI*f*V giving us 0.83V/usec. Since our opamp has a 1.6V/usec SR, we should be good here.
Reference: http://hyperphysics.phyastr.gsu.edu/HB ... 741p3.html
We may want to consider adding another opamp to implement a twopole Butterworth filter for antialiasing with a cutoff frequency of around 16kHz since we don't really need anything higher than that for our purposes. To get a cutoff frequency of 16kHz, R1=R2=4.7k, C2=1.5nF, C1=3nF.
Reference: http://www.radioelectronics.com/info/c ... filter.php
Daniel
 Attachments

 TITLV2461.pdf
 (1.88 MiB) Downloaded 518 times

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
I thought I had better post the shifter schematics used in my simulation before I forget the details:
Daniel
Note that the gain here is 1.5x just because I wanted to test the neutrality of the offset point. It can easily be changed to unity gain or something more or less.Daniel

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
This is the shifter cascaded with a twopole Butterworth filter:
Daniel
The frequency response is as follows:
As you can see, the rolloff isn't exactly steep, which is what you would expect for a loworder filter, but it does work. BTW, I reverted the gain to 1x for this chart.Daniel

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
The opamps have arrived so I can probably built the levelshifter and filter for testing, once I get some time to do it.
Daniel
Daniel

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
I finally got down to building the FFT board. It's about 99% done here I think, but not tested yet:
Daniel
That last one is the VFD that I will be using. Actually I do have a wider display but we will try to go with this one first and see how it goes from there.Daniel

 Site Admin
 Posts: 2188
 Joined: Wed 25 Feb 25 2009 8:00 pm
Re: FFT Audio Spectrum Analysis
I've got the skeletal code going and I2C communications with the VFD is working. Unfortunately, it is starting to look like the render rate is rather slower than I like it to be. This is going to require more exploration:
Daniel