I have been given an audio file (sine wave) 1000Hz as an input to my FFT algorithm. I have got 8192 power spectrum samples in a array.

What is the best and easiest way to check whether my output is right or wrong?

If I give a silent audio file then the output is zero for all samples. In a sine wav the o/p increases from 20 (0th sample) to 26059811 (743rd sample) and decreases gradually to 40.

If I get an idea of the output range then I can prove technically whether the FFT is working.

Any ideas would be helpful.

Refer to this link for any technical doubts.

  • 1
    $\begingroup$ can you plot your array? (in a spreadsheet software perhaps?) $\endgroup$ – J.P.Wack Oct 28 '10 at 12:59
  • $\begingroup$ There are 8192 values in the array.You want me to plot manually or spreadsheet will handle that.I am working on MAC $\endgroup$ – Warrior Oct 28 '10 at 13:05
  • 2
    $\begingroup$ I have plotted tens of thousands samples in MS Excel; Gnumeric or another is suitable too. Or gnuplot too. $\endgroup$ – J.P.Wack Oct 28 '10 at 14:04
  • $\begingroup$ Don't forget open office $\endgroup$ – Fake Name Oct 29 '10 at 5:05
  • $\begingroup$ @Fake Name: i am not able to plot the graph.. $\endgroup$ – Warrior Oct 29 '10 at 5:11

It seems you're calculating the spectrum by averaging 10 windows (non-overlapping?) to get the magnitude squared at 8192 or 8193 frequencies (from 0 to Nyquist, but some algorithms might drop the Nyquist frequency at bin 8192).

The first thing to check is that the peak is in the right bin. You didn't say what the sampling rate is, but bin 743 would be 743/16384 times the sampling rate. If the signal really is at 800 Hz, that puts Fs at approximately 17640 samples/second. That seems wrong. Your test signal would probably be at a standard rate such as 8000, 16000, 22050, 32000, 44100, or 48000. For Fs = 22050, the peak would be sharply in bin 800/22050*16384 = 594.

Another criteria to check is that the total energy in the signal is approximately the same in both the time and frequency domains. Here's an example in Python:

In [1]: NFFT = 2048; N = 10*NFFT; n = arange(N); Fs = 22050
In [2]: x = 0.4*cos(2*pi*400/Fs*n) + 0.6*cos(2*pi*800/Fs*n)

In [3]: y,freqs = psd(x, NFFT=NFFT, Fs=Fs, pad_to=16384)  # PSD by Welch's Method

In [4]: sum(x**2)/Fs           # time-domain energy
Out[4]: 0.24149869319296949
In [5]: sum(y) * N/16384       # frequency-domain energy
Out[5]: 0.24148752834391252

The input signal x, which consists of two sinusoids sampled at Fs = 22050 samples/second, is segmented into 10 non-overlapping windows of size NFFT = 2048 samples. The call to psd (power spectral density) computes the spectrum y as the average of the magnitude squared of ten 16384-point DFTs (actually it's 8193 points since x is real-valued).

The computed frequency-domain energy has a scaling factor of N/16384 because the psd function scaled y to the DFT size instead of to the total signal length. Whether or not this is an issue depends on how your system handles normalizing the PSD. Another optional normalization is scaling by 1/Fs. This matches the energy to the original analog signal. The default normalizations should be well documented in the library.

  • $\begingroup$ I tested my sine wave signal it is 1000 Hz. Mine FFT gives right answer.Thanks for your help. $\endgroup$ – Warrior Oct 29 '10 at 6:40

You need to plot the magnitude of the output of the FFT. I'm not familiar with your programming language, but in Python you would use something like plot(abs(fft(a))). For a silent input, the output should be all zeros. For a sine wave input, you should see two spikes:

alt text

For a real signal, the spikes will be symmetrical from left to right. If you're doing a real FFT, though (which is more computationally efficient) you'll only get the left half of the plot as your output, since it ignores the redundant mirror image.

If the frequency is higher, the spikes will be closer to the center. If the frequency is perfectly in sync with the chunk size, the spike will only be one point wide and everything else will be exactly 0. Otherwise it will have a tapering "skirt" like above.

  • $\begingroup$ Do you mean power spectrum value as the amplitude? $\endgroup$ – Warrior Oct 28 '10 at 14:02
  • $\begingroup$ t--> with reference to the time? $\endgroup$ – Warrior Oct 28 '10 at 14:03
  • $\begingroup$ Don't worry about the time. If you're just checking whether the FFT is working, all you need to do is check that the shape of the magnitude is similar to this. $\endgroup$ – endolith Oct 28 '10 at 14:06
  • $\begingroup$ I am not able to plot anyway my FFT is working right. I will try to implement the graph in my free time definitely.Thanks a lot. $\endgroup$ – Warrior Oct 29 '10 at 6:41
  • 1
    $\begingroup$ @clabacchio: Oh. The FFT produces an output with the f = 0 axis at the beginning and end of the plot. The midpoint of the plot is the f = fs/2 axis. There is often a fftfreq or fftshift function to rearrange the plot so that 0 frequency is in the center. flic.kr/p/arVeZT $\endgroup$ – endolith Feb 9 '12 at 16:12

I have used the Fourier analysis tool within the Excel Analysis Toolpak to do a quick check on the data and results.

  • $\begingroup$ I am working On MAC. $\endgroup$ – Warrior Oct 28 '10 at 13:32
  • $\begingroup$ @Warrior - Then use MacPorts or Fink to install Gnumeric (also see this page for some info on a Platypus wrapper if you want that) $\endgroup$ – Kevin Vermeer Oct 28 '10 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy