I'm trying to use FFT to extract the phase of the following signal:

enter image description here

This is just a function in the form of: m*t + 5*sin(2*π*f*t - π/4)

Is it possible to get the π/4 phase using FFT without detrending the signal? I don't want to detrend first because when I have real data, I'm afraid that by detrending I will affect the phase.

  • $\begingroup$ Is your system synchronized so that t=0 for when the waveform starts is indeed t=0 for the start of the capture, and is the capture and waveform synchronized to the same sampling clock? Assuming you are synchronized, do you need to use the FFT or would you be open to simpler approaches? $\endgroup$ Commented Sep 18, 2023 at 21:47
  • $\begingroup$ The synchronization for the waveform at t=0 is doable. The sampling frequency is two orders of magnitude higher than the signal, but I don't think I can sync them to start at the same time. I wanted to use FFT to do further analysis later, and also understand the math side of it. $\endgroup$ Commented Sep 18, 2023 at 22:15
  • $\begingroup$ I didn’t mean sync at the same time but to be phase locked or generated from same master clock (same thing) -otherwise there will inevitably be - frequency offset which is a phase ramp vs time. $\endgroup$ Commented Sep 18, 2023 at 23:55
  • $\begingroup$ I'm not sure exactly what you mean. Physically, this is a signal that is being measured using a sensor, so there is no way to sync between the sampling of the sensor and what creates the signal. $\endgroup$ Commented Sep 19, 2023 at 0:16
  • $\begingroup$ Well any frequency offset is a phase ramp with time, and your sensor will have an arbitrary frequency offset and therefore an arbitrary phase offset with an arbitrary ramp change in that phase at any given time. Consider how if you had two independent sensors with independent clocks: they will not have clocks running at exactly the same frequency unless they are locked to each other, so each sensor would give you a different result $\endgroup$ Commented Sep 19, 2023 at 0:35

1 Answer 1


Is it possible to get the π/4 phase using FFT without detrending the signal?

Not really. You have the sum of two signals: the drift and the sinusoid, i.e.

$$x[n] = M\cdot n + A \cdot \sin(\omega n + \varphi) = d[n] + s[n]$$

The DFT of this is

$$X[k] = D[k] + S[k]$$

so it depends on the spectral overlap between the drift and the sine wave frequency. As drawn, there would be a non-trivial overlap since the drift signal has significant energy at the sine frequency so they two will interfere and you'll see errors for both amplitude and phase in the 10% range.

A highpass or DC blocker won't help here since they only remove the frequency component of the drift that don't interfere with your sine wave in the first place.

Detrending can indeed help. In your specific example it can reduce the error to less than 0.2% (see example below). However that only works if

  1. The drift can be easily modelled as a low order polynomial and this model is mostly stationary (changes very slowly).
  2. The frequency is an exact integer multiple of the FFT bin spacing. Otherwise you have to deal with spectral leakage which is a completely different can of worms.

Example code

%% phase recovery of a sine wave with a linear drift on top
n = 1024;         % number of samples
t = (0:n-1)';     % time axise
m = 25/n;         % slope of linear trend
bin = 16;         % frequency bin
f =  bin/n;       % relative frequency
x = m*t + 5*cos(2*pi*f*t - pi/4);
xlabel('Time in samples');

ref = 5*exp(-1i*pi/4);  % spectrum reference
fx = 2*fft(x)/n;
[a,p] = relError(fx(bin+1),ref);
fprintf('No detrend: Amplitude error = %3.2f%%, Phase error = %3.2f%%\n',a,p);

%% detrend
y = detrend(x);
fy = 2*fft(y)/n;
[a,p] = relError(fy(bin+1),ref);
fprintf('Detrend:    Amplitude error = %3.2f%%, Phase error = %3.2f%%\n',a,p);

function [amp,phase] = relError(x,ref)
amp = 100*(abs(x)-abs(ref))/abs(ref);
phase = 100*(angle(x)-angle(ref))/angle(ref);


Results are

No detrend: Amplitude error = -7.13%, Phase error = -9.17%
Detrend:    Amplitude error = -0.13%, Phase error = -0.15%

enter image description here

  • $\begingroup$ Thank you so much! Beautiful answer! Can you please help me understand three things? A. How did you get the frequency bin to be 16? B. In the fx calculation, why did you multiply the FFT by 2? C. Is there a general way to determine the bin width so it would be a multiple integer of the frequency? $\endgroup$ Commented Sep 20, 2023 at 16:47

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.