MATLAB Answers

How to determine phase of a sine wave using FFT

641 views (last 30 days)
Fuh-Cherng on 25 Mar 2016
Commented: Star Strider on 6 Jan 2018
Dear MATLAB experts,
For a small project that I want to do, I need to compute the phase of a sine wave. However, I was unable to obtain a correct result. I have posted my code with this message (please see attachment) for your review and comments. If anyone could help me with this, I would greatly appreciate it.
Thank you!

Accepted Answer

Star Strider
Star Strider on 25 Mar 2016
Edited: Star Strider on 25 Mar 2016
The code hasn’t posted. Use the angle function on the output of the fft function:
t = [...]; % Time Vector
s = [...]; % Signal Vector
Ts = mean(diff(t)); % Sampling Time
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
L = length(s);
fts = fft(s)/L; % Normalised Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
amp_fts = abs(fts(Iv))*2; % Spectrum Amplitude
phs_fts = angle(fts(Iv)); % Spectrum Phase
Note This is obviously UNTESTED CODE but it should work.


Show 3 older comments
Min on 25 May 2017
Because different trig forms can produce same results, phase output depends on how Matlab interprets the signal. For example, sin(x) and -sin(-x) produce the same output. But also -sin(x) = cos(x + pi/2). That is why the phase output in the above example was -45 (translated by -pi/2) when the value used in the original function was 45 (pi/4). Similarly, your phi = 90 degree (pi/2 in radian) was translated by -pi/2, producing 0. Hope this helps.
H S on 6 Jan 2018
Dear Star Strider,
I think for the zero frequency (DC), your code after "amp_fts = abs(fts(Iv))*2;" should be corrected as follows:

Sign in to comment.

More Answers (1)

Fuh-Cherng on 27 Mar 2016
Thank you so much for pointing out the mistake I made! Greatly appreciated!

  1 Comment

Star Strider
Star Strider on 27 Mar 2016
As always, my pleasure.
I would appreciate it then if you would Accept my Answer.
You can do that by deleting your Answer and then Accepting mine. (No one gets any credit for Accepting their own Answer, anyway.)

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!