MATLAB Answers

Issue with getting correct FFT phase plot

6 views (last 30 days)
I am converting a time-domain synthetic 1D signal to frequency domain using MATLAB fft. In the frequency domain, the amplitude vs frequency plot is coming reasonable which means it is showing the correct amplitudes at the desired frequencies. But in the phase vs frequency plot, things are not coming any close to being reasonable. It is showing incorrect phase values at the frequency values present in the fabricated signal. I have pasted my code here:
Ws=100; % sample @ 100 Hz
T=10; % collect data long enough for at least a couple cycles
N=T*Ws; % compute number samples needed for above
t = linspace(0,T,N); % and generate time vector
y = (2.2*cos((2*pi*4*t)+(pi/3)))+(1.4*cos((2*pi*10*t)+(pi/6)))+(0.8*cos((2*pi*16*t))); % and a sample signal
Y = fft(y)/N; % FFT
PSD = 2*abs(Y(1:N/2+1)); % and the PSD one-sided
f = linspace(0,Ws/2,N/2+1); % compute freq vector for Fs
stem(f,PSD) % plot the result
Y2=Y; %store the FFT results in another array
%detect noise (very small numbers (eps)) and ignore them
threshold = max(abs(Y))/10; %tolerance threshold
Y2(abs(Y)<threshold) = 0; %mask out values that are below the threshold
phase=rad2deg(atan2(imag(Y2),real(Y2))); %phase information
stem(f,phase(1:length(f))) % plot the result
I am also attaching the plot which I got. Any help would be greatly appreciated.

Accepted Answer

David Goodmanson
David Goodmanson on 15 Apr 2021
Hello SB,
the issue here is that the signal window is not the correct width to give the periodicity that you would like. Right now
t(1) = 0 t(end) = 10 y(1) = 3.1124 y(end) = 3.1124
The first point in y is the same value as the the last point. Periodicity means that time windows can be appended side-by-side to continue the waveform. That means that 3.1124, which is the last point in the first time window, and the first point in the next time window, will be repeated. You no longer have three continuous cosine waves, and instead there is a very small flat part. You can see the effect of that in the fft because the signal, which supposedly only contains three discrete frequencies, shows nonzero amplitude for a lot of other frequencies as well.
If you delete the last point with
t = linspace(0,T,N);
t(end) = [];
and proceed from there, everything works. There are three distinct sharp frequency peaks, no other frequency content, and the phases are correct.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!