How to calculate the frequency of data using FFT?

I want to know the frequency of the data. I thought a bit that this can be done using FFT, but I'm not sure how to do it. As soon as I transmitted all the FFT data, then it gives me 2 peaks, but how can I get the frequency?

Thank you very much in advance.

+6
algorithm fft signal-processing
source share
6 answers

Suppose x[n] = cos(2*pi*f0*n/fs) , where f0 is the frequency of your sine wave in hertz, n=0:N-1 , and fs is the sampling frequency 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 sinusoidal frequency is f0 = fs*n0/N Hertz.

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

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

+8
source share

Here you are probably looking for:

When you talk about calculating the frequency of a signal, you are probably not very interested in the sinusoidal signals of the component. This is what FFT gives you. For example, if you add sin (2 * pi * 10x) + sin (2 * pi * 15x) + sin (2 * pi * 20x) + sin (2 * pi * 25x), you probably want to find the "frequency" "as 5 (look at the graph of this function). However, the FFT of this signal will determine the value 0 for frequency 5.

What interests you more is the frequency of the signal. That is, the interval during which the signal becomes most similar to itself. Therefore, most likely, you need autocorrelation. Look for it. This, in essence, will give you an idea of ​​how self-similar the signal itself is after it has moved a certain amount. Therefore, if you find a peak in autocorrelation, this will mean that the signal coincides well with itself when it moves above this sum. There's a lot of cool math there, look if you're interested, but if you just want this to work, just do the following:

  • Signal window using a smooth window (cosine will do. The window should be at least twice as large as the largest period that 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).

  • Take the FFT (however, make sure that the FFT size is two times larger than the window, while the second half is filled with zeros. If the FFT size is just the window size, taking circular autocorrelation, which you don't want, see https: // en .wikipedia.org / wiki / Discrete_Fourier_transform # Circular_convolution_theorem_and_cross-correlation_theorem )

  • Replace all FFT coefficients with their square value (real ^ 2 + imag ^ 2). It efficiently accepts autocorrelation.

  • Take ift

  • Find the largest peak in iFFT. This is the strongest waveform periodicity. In fact, you can be a little smarter when the peak you choose, but for most purposes this should be enough. To find the frequency, you simply take f = 1 / T.

+6
source share

If you have a signal with one frequency (for example:

 y = sin(2 pi ft) 

FROM

  • y time signal
  • f center frequency
  • t time

Then you get two peaks, one with a frequency corresponding to f, and one with a frequency corresponding to -f.

So, to go to the frequency, you can drop the negative frequency part. It is located after the positive frequency part. Also, the first element in the array is dc-offset, so the frequency is 0. (Beware that this offset is usually much greater than 0, so other frequency components can be overshadowed by it.)

In code: (I wrote it in python, but it should be equally simple in C #):

 import numpy as np from pylab import * x = np.random.rand(100) # create 100 random numbers of which we want the fourier transform x = x - mean(x) # make sure the average is zero, so we don't get a huge DC offset. dt = 0.1 #[s] 1/the sampling rate fftx = np.fft.fft(x) # the frequency transformed part # now discard anything that we do not need.. fftx = fftx[range(int(len(fftx)/2))] # now create the frequency axis: it runs from 0 to the sampling rate /2 freq_fftx = np.linspace(0,2/dt,len(fftx)) # and plot a power spectrum plot(freq_fftx,abs(fftx)**2) show() 

Now the frequency is at its largest peak.

+5
source share

If you look at the results of the amplitude of the FFT type most commonly used, then the strong sinusoidal frequency component of the real data will be displayed in two places, once in the lower half, plus its complex conjugate mirror image in the upper half. These two peaks represent the same spectral peak and the same frequency (for strictly real data). If the FFT result bit numbers begin with 0 (zero), then the frequency of the sinusoidal component represented by the basket in the lower half of the FFT result is most likely.

 Frequency_of_Peak = Data_Sample_Rate * Bin_number_of_Peak / Length_of_FFT ; 

Make sure you design your correct units in the above equation (to get units of cycles per second, for two weeks, per kiloparsec, etc.).

Note that if the data wavelength is not an exact integer sum of the FFT length, the actual peak will be between the bins, thus distributing energy between several adjacent cells of the FFT results. Therefore, you may need to interpolate to better estimate the peak frequency. Common interpolation methods for finding a more accurate estimate of the frequency are 3-point parabolic and Sinc convolution (which is almost the same as using a longer FFT with zero width).

+4
source share

Assuming that you are using a discrete Fourier transform to view frequencies, you should be careful how to interpret normalized frequencies back to physical (i.e. Hz).

According to the FFTW tutorial on how to calculate the signal power spectrum:

 #include <rfftw.h> ... { fftw_real in[N], out[N], power_spectrum[N/2+1]; rfftw_plan p; int k; ... p = rfftw_create_plan(N, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE); ... rfftw_one(p, in, out); power_spectrum[0] = out[0]*out[0]; /* DC component */ for (k = 1; k < (N+1)/2; ++k) /* (k < N/2 rounded up) */ power_spectrum[k] = out[k]*out[k] + out[Nk]*out[Nk]; if (N % 2 == 0) /* N is even */ power_spectrum[N/2] = out[N/2]*out[N/2]; /* Nyquist freq. */ ... rfftw_destroy_plan(p); } 

Note that it processes data lengths that do not match. Please note, especially if the data length is specified, FFTW will give you a β€œbit” corresponding to the Nyquist frequency (sampling frequency divided by 2). Otherwise, you will not get it (i.e. the last bit is just below Nyquist).

A MATLAB example is similar, but they choose a length of 1000 (an even number) for the example:

 N = length(x); xdft = fft(x); xdft = xdft(1:N/2+1); psdx = (1/(Fs*N)).*abs(xdft).^2; psdx(2:end-1) = 2*psdx(2:end-1); freq = 0:Fs/length(x):Fs/2; 

In general, this may be an implementation (from DFT) dependent. You must create a test pure sine wave with a known frequency, and then make sure that the calculation gives the same number.

+1
source share

Frequency = speed / wavelength.

The wavelength is the distance between two peaks.

-6
source share

All Articles