So I am working on a project using digital signal processing, which I am not familiar with at all, and a lot of this is all new to me. I am using SciPy\NumPy to play around with code to understand the Fourier Transform. Basically, I have been able to read in a wave file and perform an FFT on the data using the following code:

samplerate, data = wavfile.read(sys.argv[1])
fftres = fft(data)

for i in range(1, len(fftres)):
    print i, ",", fftres[i], "\n"

My results look something like this:


I'm not sure what I'm looking at to be honest. I need to find the frequencies in my file so I can apply a bandpass filter on the audio signal. From my output, how do I actually get the frequencies?


I would recommend doing some proper reading of books/tutorials etc on the Fourier transform and the Discrete Fourier Transform (DFT). You will also want to look at filters and probably convolution for the bandpass filter. But regardless, here's some info that should help with your current state:

Use matplotlib to plot the FT that you've calculated:

fs = 44100 # Or whatever the actual sample rate is (Hz)
f = linspace (fs,len(data), endpoint=False)
mpl.plot (f, abs (fftres))
title ('Magnitude spectrum of the signal')
xlabel ('Frequency (Hz)')

Another function you will want to look at is fftshift, which makes it simple to 'roll' your spectrum around so that DC is in the centre.

The frequencies that are sampled by the DFT are between $-f_s/2 < f < f_s/2$ for sampling frequency $f_s$. And the FFT produces the same number of samples as the number of data points provided to it. The FFT function returns the spectrum so that the DC (constant/average) value is first in the array. Use the function fftshift to adjust the array if you want it ordered $-f_s/2 < f < f_s/2$.

I will now repeat: Fourier transforms and filtering are a huge field, and while it will definitely be interesting and useful to play with the functions for a bit like you are doing, you really need to read up on what is going on if you want to actually do anything useful.

Aside: why are you starting at index 1 in your loop, rather than 0?

  • 1
    $\begingroup$ I would add one thing to what @Ixop said. Note that, when plotting, @Ixop has you using abs (fftres). This is because the result of taking the fft is a complex number $x + y j$, where $j=\sqrt{-1}$. abs is giving you the length (distance from zero) of this complex number, and that is the amplitude (amount) of frequency $f_{(i-len/2)}$ that the fft found in your signal. The angle of the complex number (the rotation from the positive real axis) is the phase shift that the fft found in your signal for frequency $f_{(i-len/2)}$. $\endgroup$ – Wandering Logic May 2 '13 at 1:03
  • $\begingroup$ For some reason I was thinking that NumPy\SciPy arrays were indexed starting at 1. I appreciate the help! I've looked at some tutorials, but I do need to try to have this code done in a timely manner. $\endgroup$ – csnate May 2 '13 at 14:32
  • $\begingroup$ @csreap3r: Matlab arrays are indexed with 1, but NumPy arrays are indexed with the more logical 0. $\endgroup$ – endolith May 2 '13 at 14:34
  • 1
    $\begingroup$ Yes, yes as a computer scientist I should have assumed that. Everything starts at 0 :) $\endgroup$ – csnate May 2 '13 at 14:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.