Confusion in figuring out the relation between actual frequency values and FFT plot indexes in MATLAB - matlab

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?

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

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.

Related

How can i use fft to find the maximum frequency of a periodic signal?

I'm trying to find the maximum frequency of a periodic signal in Matlab and as i know when you convert a periodic signal to the frequency spectrum you get only delta functions however i get a few curves between the produced delta functions. Here is the code :
t=[-0.02:10^-3:0.02];
s=5.*(1+cos(2*pi*10*t)).*cos(2*pi*100*t);
figure, subplot(211), plot(t,s);
y=fft(s);
subplot(212), plot(t,y);
Here is a code-snippet to help you understand how to get the frequency-spectrum using fft in matlab.
Things to remember are:
You need to decide on a sampling frequency, which should be high enough, as per the Nyquist Criterion (You need the number of samples, at least more than twice the highest frequency or else we will have aliasing). That means, fs in this example cannot be below 2 * 110. Better to have it even higher to see a have a better appearance of the signal.
For a real signal, what you want is the power-spectrum obtained as the square of the absolute of the output of the fft() function. The imaginary part, which contains the phase should contain nothing but noise. (I didn't plot the phase here, but you can do this to check for yourself.)
Finally, we need to use fftshift to shift the signal such that we get the mirrored spectrum around the zero-frequency.
The peaks would be at the correct frequencies. Now considering only the positive frequencies, as you can see, we have the largest peak at 100Hz and two further lobs around 100Hz +- 10Hz i.e. 90Hz and 110Hz.
Apparently, 110Hz is the highest frequency, in your example.
The code:
fs = 500; % sampling frequency - Should be high enough! Remember Nyquist!
t=[-.2:1/fs:.2];
s= 5.*(1+cos(2*pi*10*t)).*cos(2*pi*100*t);
figure, subplot(311), plot(t,s);
n = length(s);
y=fft(s);
f = (0:n-1)*(fs/n); % frequency range
power = abs(y).^2/n;
subplot(312), plot(f, power);
Y = fftshift(y);
fshift = (-n/2:n/2-1)*(fs/n); % zero-centered frequency range
powershift = abs(Y).^2/n;
subplot(313), plot(fshift, powershift);
The output plots:
The first plot is the signal in the time domain
The signal in the frequency domain
The shifted fft signal

Computing and plotting the spectrum of a signal using FFT on Matlab

I am missing something in the computation of the spectrum of my signal using FFT on Matlab.
My code:
%% compute the spectrum of the data (data(t))
L = length(time); % length of the sample
NFFT = 2^(nextpow2(L)-1); % Next power of 2 from length of y
Y = fft(data,NFFT);%/NFFT;%L;
Fs = 1/(mean(time(2:end)-time(1:end-1))); % compute the sampling frequency
f = Fs/2*linspace(0,1,NFFT/2+1);
loglog(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of My Data')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Would you be so kind as to tell me where I messed up?
I tried to check if the algorythm works using these two sampling of the same signal (same sampling frequency ; over two different time range 0-10 and 0-100):
fs=1000;
time10 = [0:1/fs:10];
time100 = [0:1/fs:100];
data10 = sin(2*pi*0.23 .*time10)+cos(2*pi*12 .*time10);
data100 = sin(2*pi*0.23 .*time100)+cos(2*pi*12 .*time100);
I guess the two spectrum should supperpose but they don't... As seen here: https://www.dropbox.com/s/wfols9o409pr94u/FFT_spectrum_StackOverflow.png?dl=0
https://www.dropbox.com/s/a8vmzwto6x4130w/FFT_spectrum_StackOverflow.fig?dl=0
Thanks
The good news is that there is nothing wrong with your computation of the spectrum by itself.
The problem is that by looking at samples of different lengths you are effectively looking at two different samples altogether.
In the time-domain, they can be seen as the result of a multiplication of an infinitely long sinusoidal with a rectangular window of different lengths.
In the frequency-domain, the spectrum of the infinitely long continuous-time sinusoidal signal gets convoluted with the spectrum of the rectangular windows. With different window length the corresponding spectrum of those windows have different width (narrower spectrum for longer rectangular windows). As a result, the spikes in the spectrum of the infinitely long sinusoidal signal would get spread over different bandwidths. This is exactly what you are seeing.

Dominating frequencies time series

I have a time series data. I performed fft on it using matlab as:
ft=fft(data);
How do we get the dominating frequencies. I tried taking the maximum value of ft, but it appears to be wrong. How can we get dominating frequencies
You need to take the magnitude of the complex FFT output and find the peaks:
ft=fft(data);
mag_ft = abs(ft); % take magnitude of FFT complex output values
First, frequency axis will be needed to find frequencies of your fft data. Since your sampling rate is just 1, maximum frequency you can check will be 0.5 Hz.
dt=1; % your time step
N=length(data); % data length
df=1/(dt*N); %frequency bin
f_axis=(0:N-1)*df; % frequency axis
And then, plot your fft data with the frequency axis.
ft=fft(data);
plot(f_axis,abs(ft));

How to know sampling frequency of a sin(x) function

Consider the following script that plots a sine wave.
t = 0:pi/100:2*pi;
y = sin(t);
plot(t,y)
grid on % Turn on grid lines for this plot
This gives me a plot of sine wave. I understand the sine wave that appears continuous, should actually be discrete (my PC cannot store infinite no. of samples of continuous signal), and the matlab plot function does some kind of interpolation to connect the dots.
So In fact I also used stem instead of plot to see the sampled values (on time axis) of sine.
Now my question is there must be some sampling frequency used here. How much is that?
The sampling interval is the time interval between two consecutive samples of your signal.
The sampling frequency means how much samples of your signal you have in a fixed time interval, and it is reciprocal to the sampling interval.
You declared:
t = 0:pi/100:2*pi;
So your sampling interval is π/100. This means that your sampling frequency is 100/π.
If you want exact units, you'll have to determine the time units for t. If t is in seconds, then your sampling frequency is 100/π Hz (1Hz = 1sec-1).
By the way, MATLAB's plot connects the sampling with straight lines, there is no additional interpolation involved.

Frequency response using FFT in MATLAB

Here is the scenario: using a spectrum analyzer i have the input values and the output values. the number of samples is 32000 and the sampling rate is 2000 samples/sec, and the input is a sine wave of 50 hz, the input is current and the output is pressure in psi.
How do i calculate the frequency response from this data using MATLAB,
using the FFT function in MATLAB.
i was able to generate a sine wave, that gives out the the magnitude and phase angles, here is the code that i used:
%FFT Analysis to calculate the frequency response for the raw data
%The FFT allows you to efficiently estimate component frequencies in data from a discrete set of values sampled at a fixed rate
% Sampling frequency(Hz)
Fs = 2000;
% Time vector of 16 second
t = 0:1/Fs:16-1;
% Create a sine wave of 50 Hz.
x = sin(2*pi*t*50);
% Use next highest power of 2 greater than or equal to length(x) to calculate FFT.
nfft = pow2(nextpow2(length(x)))
% Take fft, padding with zeros so that length(fftx) is equal to nfft
fftx = fft(x,nfft);
% Calculate the number of unique points
NumUniquePts = ceil((nfft+1)/2);
% FFT is symmetric, throw away second half
fftx = fftx(1:NumUniquePts);
% Take the magnitude of fft of x and scale the fft so that it is not a function of the length of x
mx = abs(fftx)/length(x);
% Take the square of the magnitude of fft of x.
mx = mx.^2;
% Since we dropped half the FFT, we multiply mx by 2 to keep the same energy.
% The DC component and Nyquist component, if it exists, are unique and should not be multiplied by 2.
if rem(nfft, 2) % odd nfft excludes Nyquist point
mx(2:end) = mx(2:end)*2;
else
mx(2:end -1) = mx(2:end -1)*2;
end
% This is an evenly spaced frequency vector with NumUniquePts points.
f = (0:NumUniquePts-1)*Fs/nfft;
% Generate the plot, title and labels.
subplot(211),plot(f,mx);
title('Power Spectrum of a 50Hz Sine Wave');
xlabel('Frequency (Hz)');
ylabel('Power');
% returns the phase angles, in radians, for each element of complex array fftx
phase = unwrap(angle(fftx));
PHA = phase*180/pi;
subplot(212),plot(f,PHA),title('frequency response');
xlabel('Frequency (Hz)')
ylabel('Phase (Degrees)')
grid on
i took the frequency response from the phase plot at 90 degree phase angle, is this the right way to calculate the frequency response?
how do i compare this response to the values that is obtained from the analyzer? this is a cross check to see if the analyzer logic makes sense or not.
Looks OK at first glance, but a couple of things you're missing:
you should apply a window function to the time domain data before the FFT, see e.g. http://en.wikipedia.org/wiki/Window_function for windowing in general and http://en.wikipedia.org/wiki/Hann_window for the most commonly used window function (Hann aka Hanning).
you probably want to plot log magnitude in dB rather than just raw magnitude
You should consider looking at the cpsd() function for calculating the Frequency response. The scaling and normalisation for various window functions is handled for you.
the Frequency reponse would then be
G = cpsd (output,input) / cpsd (input,input)
then take the angle() to obtain the phase difference between the input and the output.
Your code snippet does not mention what the input and output data sets are.