Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute frequency of data using FFT?

I want to know the frequency of data. I had a little bit idea that it can be done using FFT, but I am not sure how to do it. Once I passed the entire data to FFT, then it is giving me 2 peaks, but how can I get the frequency?

Thanks a lot in advance.

like image 438
Michael Avatar asked Nov 19 '10 13:11

Michael


People also ask

How do you find the frequency of FFT in Matlab?

n = 2^nextpow2(L); Y = fft(X,n); Compute the single-sided amplitude spectrum of the padded signal. Because the signal length n increased from 65 to 128, the frequency resolution becomes Fs/n , which is 0.625 Hz. f = Fs*(0:(n/2))/n; P2 = abs(Y/L); P1 = P2(1:n/2+1); P1(2:end-1) = 2*P1(2:end-1);

What is the frequency range of FFT?

The output of the generic FFT normally used in programming is 0-22khz for a 44.1 sample and 0-24khz for a 48khz input.

What is sampling frequency in FFT?

The sampling rate or sampling frequency fs of the measuring system (e.g. 48 kHz). This is the average number of samples obtained in one second (samples per second). The selected number of samples; the blocklength BL. This is always an integer power to the base 2 in the FFT (e.g., 2^10 = 1024 samples)


2 Answers

Here's what you're probably looking for:

When you talk about computing the frequency of a signal, you probably aren't so interested in the component sine waves. This is what the FFT gives you. For example, if you sum sin(2*pi*10x)+sin(2*pi*15x)+sin(2*pi*20x)+sin(2*pi*25x), you probably want to detect the "frequency" as 5 (take a look at the graph of this function). However, the FFT of this signal will detect the magnitude of 0 for the frequency 5.

What you are probably more interested in is the periodicity of the signal. That is, the interval at which the signal becomes most like itself. So most likely what you want is the autocorrelation. Look it up. This will essentially give you a measure of how self-similar the signal is to itself after being shifted over by a certain amount. So if you find a peak in the autocorrelation, that would indicate that the signal matches up well with itself when shifted over that amount. There's a lot of cool math behind it, look it up if you are interested, but if you just want it to work, just do this:

  1. Window the signal, using a smooth window (a cosine will do. The window should be at least twice as large as the largest period you want to detect. 3 times as large will give better results). (see http://zone.ni.com/devzone/cda/tut/p/id/4844 if you are confused).

  2. Take the FFT (however, make sure the FFT size is twice as big as the window, with the second half being padded with zeroes. If the FFT size is only the size of the window, you will effectively be taking the circular autocorrelation, which is not what you want. see https://en.wikipedia.org/wiki/Discrete_Fourier_transform#Circular_convolution_theorem_and_cross-correlation_theorem )

  3. Replace all coefficients of the FFT with their square value (real^2+imag^2). This is effectively taking the autocorrelation.

  4. Take the iFFT

  5. Find the largest peak in the iFFT. This is the strongest periodicity of the waveform. You can actually be a little more clever in which peak you pick, but for most purposes this should be enough. To find the frequency, you just take f=1/T.

like image 50
Jeremy Salwen Avatar answered Oct 04 '22 22:10

Jeremy Salwen


Suppose x[n] = cos(2*pi*f0*n/fs) where f0 is the frequency of your sinusoid in Hertz, n=0:N-1, and fs is the sampling rate of x in samples per second.

Let X = fft(x). Both x and X have length N. Suppose X has two peaks at n0 and N-n0.

Then the sinusoid frequency is f0 = fs*n0/N Hertz.

Example: fs = 8000 samples per second, N = 16000 samples. Therefore, x lasts two seconds long.

Suppose X = fft(x) has peaks at 2000 and 14000 (=16000-2000). Therefore, f0 = 8000*2000/16000 = 1000 Hz.

like image 38
Steve Tjoa Avatar answered Oct 04 '22 22:10

Steve Tjoa