Confusion in figuring out the relation between act

2019-02-18 11:54发布

问题:

I know that there are a lot of similar questions to this, I am still unable to figure out the answer. Let's say we have time signal in MATLAB:

t=0:1/44100:1

and a cosine signal with frequency 500Hz:

x=cos(2*pi*500*t);

Now, I am trying to plot the magnitude spectrum obtained using the fft command on signal x

FFT=abs(fft(x))
plot(FFT)

According to the theory, we should get two peaks in the plot, one at -500 Hz and the other at 500Hz. What I don't understand is that I do get two peaks but I can't figure out at what frequencies these peaks are. I know there is a way to figure out the frequency using the FFT index, length of the input signal and the sampling frequency but I still can't calculate the frequency.

I know that there are methods to align the FFT plots so that the peaks lie at the index number of the frequency they represent by using the fftshift function, but what I want is to figure out the frequency using the the plot resulting from simply calling this function:

FFT=fft(x)

In this case, I already know that signal contains a cosine of 500Hz, but what if the signal that we want to get the FFT of is not known before time. How can we get the frequency values of the peaks in that sample using the output from the fft function?

回答1:

You need to generate the frequency array yourself and plot your FFT result against it.

Like this:

function [Ycomp, fHz] = getFFT(data, Fs)
     len = length(data);
     NFFT = 2^nextpow2(len);
     Ydouble = fft(data, NFFT)/len; % Double-sided FFT
     Ycomp = Ydouble(1:NFFT/2+1); % Single-sided FFT, complex
     fHz = Fs/2*linspace(0,1,NFFT/2+1); % Frequency array in Hertz.
     semilogx(fHz, abs(Ycomp))
end


回答2:

You will see peaks at 500 Hz and Fs - 500 Hz (i.e. 44100 - 500 = 43600 Hz in your particular case).

This is because the real-to-complex FFT output is complex conjugate symmetric - the top half of the spectrum is a "mirror image" of the bottom half when you are just looking at the magnitude and is therefore redundant.

Note that of plotting power spectra you can usually save yourself a lot of work by using MATLAB's periodogram function rather than dealing directly with all the details of FFT, window functions, plotting, etc.