How to define the frequency domain for plotting the FFT?

113 views (last 30 days)
I am new to DSP and am trying to figure out how the fft function works. From what I understand it returns the DFT of a input sequence: a vector of complex numbers whose magnitude and phase are that of the frequency components. But I do not understand what these frequencies are and am a little confused from some explanations that I have seen. To better explain my point: imagine if the fft() function returned a frequency vector over which it was calculated:
[X, freq] = fft(x); % some signal x <--> X
plot(freq, abs(X)); % plot magnitude response
figure;
plot(freq, angle(X)); % plot phase response
I have seen the following example from the documentation page: <https://in.mathworks.com/help/matlab/ref/fft.html>
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
X = S + 2*randn(size(t));
Y = fft(X);
All well and good. Following are the lines that I do not understand with the quote: "Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L." (?)
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
"Define the frequency domain f and plot the single-sided amplitude spectrum P1" (?)
f = Fs*(0:(L/2))/L;
Can someone explain what the last few lines are doing? The rest simply involves plotting the spectrum against the frequency vector which is easy. Any help is appreciated. Thanks!

Answers (1)

Matt J
Matt J on 7 Feb 2022
Edited: Matt J on 7 Feb 2022
To better explain my point: imagine if the fft() function returned a frequency vector over which it was calculated:
The frequency axis sample locations are not uniquely determined by the input x. You need to know the time sampling period as well. Basically, the relationship constraining everything is N*dT*dF=1 where N is the number of samples, dT is the time sampling period, and dF is the frequency sampling period
Fs = 1000; % Sampling frequency
dT = 1/Fs; % Sampling period
N = 1500; % Length of signal
t=( (0:N-1)-ceil((N-1)/2) )*dT ; % Time vector
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
[X,freq]=continuousFTsamples(x,dT);
tiledlayout(1,2)
nexttile
plot(t,x); axis square; xlabel 'Time (sec)'; ylabel 'x(t)'
nexttile
plot(freq,abs(X)); axis square; xlabel 'Freq. (Hz)'; ylabel 'X(f)'; xlim([-140,140])
function [X,freq]=continuousFTsamples(x,dT,varargin)
X=fftshift( fft( ifftshift(x), varargin{:} ) )*dT;
N=numel(X);
dF=1/N/dT;
freq=( (0:N-1)-ceil((N-1)/2) )*dF;
end

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!