MATLAB: Fft

fftfrequency domain

A very fundamental doubt… I have a a signal as a 2xN array. The first row contains the time instants at which samples have been taken and the second row contains the data values at those sampling instants. Now when i take the fft of the data values, how do i map the corresponding time values in frequency domain. As in, i wish to plot the fft vs frequeny. How do i obtain the frequency axis?
code which i have used
Fs=500; L=length(sym_signal); y=sym_signal(7,:); NFFT = 2^nextpow2(L); % Next power of 2 from length of y Y = fft(y,NFFT)/L; f = Fs/2*linspace(0,1,NFFT/2+1); plot(f(1:100),2*(abs(Y(1:100))));
The Fs which i have used is obtained by finding the reciprocal time interval between two consecutive time values. Is this the correct way of doing it?

Best Answer

%Your data 2xN
t=1:0.001:10;
data=[t;sin(2*pi*50*t)+2*randn(size(t))];
%notice the sin+noise, noise added just for fun
Ts = data(1,2)-data(1,1); % Sample time
Fs = 1/Ts; % Sampling frequency
L = numel(t); % Length of signal
y = data(2,:); % data from signal
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
figure %create another figure for the other plot
% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Related Question