

MSP Analysis Tutorial 3: Using the FFT

Fourier's theorem




Spectrum of a signal: fft~


Actually, the analysis produced by fft~ is two parallel signals. That's because the math involved produces complex numbers, which have a real and imaginary component. The real components are output from the left outlet and the imaginary components from the middle. Both signals are required for spectral processing.



With the plot~ object you can catch the output of fft~ and display the frequency analysis of a signal. (Setting plot~ up to do this is discussed below.) Plot~ is the graph at the bottom of the patch.





• Click on the toggle in the blue part of the patch to hear the resynthesized sound. Click on the toggle again to close the gate~. Now look at the plot at the bottom of the fft~.

The plot~ shows a single spike at 861.3 Hz. If you click the clear message and double cick the capture~ object, you will see some of the numbers fft~ is putting out. Each of the 512 numbers represents a harmonic of the FFT frequency itself, starting at the 0th harmonic (0 Hz). The analysis shows energy in the eleventh number, which represents the 10th harmonic of the FFT, 10/512 the sampling rate -- precisely our test frequency. The analysis also shows energy at the 10th number from the end, (scroll to see) which represents 502/512 the sampling rate. This frequency exceeds the Nyquist rate and is actually equivalent to -10/512 of the sampling rate. (This component is not visible in the plot~ display which is limited to half the sampling rate).



Practical problems of the FFT



The analysis of the 1000 Hz tone does indeed show greater energy at 1000 Hz -- in the 12th and 13th frequency regions if your MSP sampling rate is 44,100 Hz -- but it also shows energy in virtually every other region. That's because the waveform it analyzed is no longer a sinusoid. (An exact number of cycles does not fit precisely into the 512 samples.) All the other energy shown in this FFT is an artifact of the ‘incorrect’ interpretation of those 512 samples as one period of the correct waveform. Also if you close the capture~ object, hit clear and reopen it, you will see that the numbers have all changed, as the phase relationship between 1000Hz and 947.5 (the 11th harmonic of the fft frequency) will be constantly changing.

All of this will be visible in the plot, which has a broad peak with constantly changing skirts.


Overlapping FFTs




One FFT is taken 256 samples later than the other


• Click the toggle labeled Show Windowed Analysis. This will add the windowed fft (in red) to the display.


Displaying spectra with plot~

The plot~ object is designed to show graphs of listed data or audio signals. With the latter, it works a lot like scope~, where the portion of waveform shown is determined by the number of points in the display. In plot~ this is set with a numpoints message. plot~ has a lot of options, especially if you want to add gridlines and titles to the display. Luckily, there are some prototypes available for plot~, accessed via the button at the left edge of the display. Prototypes include several spectral displays complete with gridlines and labels.

There is some assembly required before plot can show the fft spectrum. Open the Display_analysis subpatch to see what is required.

  • The signals from fft~ can reach very high values (as high as the frame size) and must be scaled down to a maximum of 1.0 for plot~. Hence the divide by 512.
  • The power in each bin is represented by a complex number. The magnitude of a complex number is the square root of the sum of the squares of the real and imaginary parts, so each signal from the fft~ is squared before they are added together. If we leave the square root out, we wind up with an exponential scale, appropriate to show as dB.
  • Plot~ has no triggering mechanism as scope~ and spectroscope~ do, so the signal from fft~ needs to be stabilized before it can be displayed. The vectral~ object is ideal for this, as it aligns incoming sames with indices in the left inlets. The right outlet of fft~ produces a ramp of bin numbers from 0 to the window size, perfect of vectral~. Vectoral~ can also average the values over several frames, which makes the display smoother.

Most of the time, we will use spectroscope~ as our spectral display, but if you want custom features or extra high resolution, plot~ and fft~ allow you to roll your own.

Signal processing using the FFT







See Also

Name Description
buffer~ Store audio samples
capture~ Store a signal to view as text
fft~ Fast Fourier transform
ifft~ Inverse fast Fourier transform