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
- The drift can be easily modelled as a low order polynomial and this model is mostly stationary (changes very slowly).
- 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);
plot(x);
xlabel('Time in samples');
ylabel('Amplitude');
grid('on');
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);
end
Results are
No detrend: Amplitude error = -7.13%, Phase error = -9.17%
Detrend: Amplitude error = -0.13%, Phase error = -0.15%
