Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scipy : fourier transform of a few selected frequencies

Tags:

python

scipy

fft

I am using scipy.fft on a signal, with a moving window to plot the amplitudes of frequencies changing with time (here is an example, time is on X, frequency on Y, and amplitude is the color).

However, only a few frequencies interest me (~3, 4 frequencies only). With FFTs it seems like I can't select only the frequencies I want (cause apparently the range of frequencies is determined by the algorithm), so I calculate a lot of useless stuff, and my program even crashes with a MemoryError if the signal is too long.

What should I do ? Do I have to use a custom Fourier transform - in which case, links of good implementations are welcome - , or is there a scipy way ?


EDIT

After @jfaller answer, I decided to (try to) implement the Goertzel algorithm. I came up with this : https://gist.github.com/4128537 but it doesn't work (frequency 440 doesn't appear, nevermind the peaks, I didn't bother to apply a proper window). Any help !? I am bad with DSP.

like image 305
sebpiq Avatar asked Nov 21 '12 18:11

sebpiq


People also ask

What is the minimum number of samples for fast Fourier transformation?

The fast Fourier transform (FFT) is a computer algorithm developed by James Cooley and John Tukey. The algorithm computes the coefficients for the Fourier series that represents a sequence. The number of samples (N) in the FFT must be an integer power of 2.

How do you extract frequency from FFT?

We can obtain the magnitude of frequency from a set of complex numbers obtained after performing FFT i.e Fast Fourier Transform in Python. The frequency can be obtained by calculating the magnitude of the complex number. So simple ab(x) on each of those complex numbers should return the frequency.

What does Scipy Fftfreq do?

Return the Discrete Fourier Transform sample frequencies. The returned float array f contains the frequency bin centers in cycles per unit of the sample spacing (with zero at the start). For instance, if the sample spacing is in seconds, then the frequency unit is cycles/second.


1 Answers

You're really looking to use the Goertzel Algorithm: http://en.wikipedia.org/wiki/Goertzel_algorithm. Basically, it's an FFT at a single point, and efficient if you only need a limited number of frequencies in a signal. If you have trouble pulling apart the algorithm from Wikipedia, ping back, and I'll help you. Also if you google a few resources, there exist DTMF decoders (touch tone phone decoders) written in python. You could check out how they do it.

like image 130
jfaller Avatar answered Oct 23 '22 15:10

jfaller