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 Ws


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. Output of the code

  • $\begingroup$ quicknote, you simply use to need the phase function on a complex number. You don't need to call atan2 $\endgroup$
    – Ben
    Apr 15, 2021 at 17:10
  • $\begingroup$ Tried that too. same output is coming $\endgroup$ Apr 15, 2021 at 17:11
  • $\begingroup$ I never claimed this would fix your problem ;) $\endgroup$
    – Ben
    Apr 15, 2021 at 17:23

1 Answer 1


There's an error in your script. Your sampling frequency is not 100 Hz like you intended but is actually 99.9 Hz. Therefore you don't actually have a whole number of cycles in your FFT which creates some leakage. This leakage will also impact the phase estimation.

If you want 1000 points at 100 Hz, the last point should not end at 10 seconds but at 10 seconds - 1/100 = 9.99. You should be careful when using linspace to define a time vector.

Use this n = 1 : 1000; t = (n-1)/100;

FFT with proper time vector

  • $\begingroup$ Yes this exactly solved my problem $\endgroup$ Apr 16, 2021 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.