FFT Audio Spectrum Analysis

Stuff I am working on
Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

FFT Audio Spectrum Analysis

Post by Daniel Wee » Sun 25 Oct 25 2009 9:25 pm

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 real-time. It is "near" real-time because the display always lags real-time 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 Shannon-Nyquist sampling theorem, our sampling frequency is going to have to be 40ksps and above. We will also need to include a low-pass 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 12-bit 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 256-points which is good since our VFD display has exactly that horizontal resolution. Assuming that we go with a 256-point 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 digital-filter (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/

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Mon 26 Oct 26 2009 12:23 am

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 256-point 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 Nyquist-Shannon limit. A 256-point 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

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Mon 26 Oct 26 2009 11:10 am

Anti-Aliasing Filters

I've mentioned the need for anti-aliasing filters, basically a low-pass 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 over-sample 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, F2-F3 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 anti-aliasing 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 464 times

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Mon 26 Oct 26 2009 11:32 am

Given that most of the audio signals we normally find are AC coupled, we will need a level-shifter circuit (simple summing op-amp with unity gain) at the input. This gives us the opportunity to control the gain with some of the spare op-amps as well as to implement some low-pass 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 non-inverting 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 Hann-windowing 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 32-levels (or is it 64-levels), it is pointless to use the full 12-bit resolution for the audio samples. 8-bits could be more than sufficient.

Daniel

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Tue 27 Oct 27 2009 12:53 am

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.
Shifter.jpg
Shifter.jpg (108.67 KiB) Viewed 14013 times
Note that older op-amps 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 op-amp has a 1.6V/usec SR, we should be good here.

Reference: http://hyperphysics.phy-astr.gsu.edu/HB ... 741p3.html

We may want to consider adding another op-amp to implement a two-pole Butterworth filter for anti-aliasing with a cutoff frequency of around 16kHz since we don't really need anything higher than that for our purposes.
8le4ev4a.jpg
8le4ev4a.jpg (17.52 KiB) Viewed 14013 times
1whhr6n5.jpg
1whhr6n5.jpg (10.48 KiB) Viewed 14013 times
To get a cutoff frequency of 16kHz, R1=R2=4.7k, C2=1.5nF, C1=3nF.

Reference: http://www.radio-electronics.com/info/c ... filter.php

Daniel
Attachments
TI-TLV2461.pdf
(1.88 MiB) Downloaded 437 times

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Wed 28 Oct 28 2009 8:50 pm

I thought I had better post the shifter schematics used in my simulation before I forget the details:-
Shifter-sch.jpg
Shifter-sch.jpg (35.91 KiB) Viewed 14008 times
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

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Wed 28 Oct 28 2009 10:54 pm

This is the shifter cascaded with a two-pole Butterworth filter:-
Shifter-Filter.jpg
Shifter-Filter.jpg (61.37 KiB) Viewed 14008 times
The frequency response is as follows:-
Shifter-Filter-Response.png
Shifter-Filter-Response.png (7.12 KiB) Viewed 14008 times
As you can see, the roll-off isn't exactly steep, which is what you would expect for a low-order filter, but it does work. BTW, I reverted the gain to 1x for this chart.

Daniel

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Wed 04 Nov 04 2009 2:12 am

The op-amps have arrived so I can probably built the level-shifter and filter for testing, once I get some time to do it.

Daniel

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Thu 05 Nov 05 2009 2:30 am

I finally got down to building the FFT board. It's about 99% done here I think, but not tested yet:-
IMG_1100a.JPG
IMG_1100a.JPG (56.81 KiB) Viewed 13992 times
IMG_1101a.JPG
IMG_1101a.JPG (93.32 KiB) Viewed 13992 times
IMG_1104a.JPG
IMG_1104a.JPG (43.66 KiB) Viewed 13992 times
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

Daniel Wee
Site Admin
Posts: 2084
Joined: Wed 25 Feb 25 2009 8:00 pm

Re: FFT Audio Spectrum Analysis

Post by Daniel Wee » Thu 05 Nov 05 2009 7:02 pm

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:-
IMG_1110a.JPG
IMG_1110a.JPG (55.88 KiB) Viewed 13991 times
Daniel

Post Reply