MATLAB: Regarding phase compensation

phase compensation

Hello,
I am trying to do simple phase compensation. However, the code is always making some extra phase correction. Its not making an exact compensation. Please have a look at the code. I think I am formulating it wrongly. Although theta estimation is pretty correct.
theta = pi/5;
t=(0:1000)/1000;
freq = 20;
s1 = sin(2*pi*freq*t);
s2 = sin(2*pi*freq*t + theta);
theta_estimate = acos( 2*mean( s1.*s2) );
s3=s2*(cos(theta_estimate)+(1i*sin(theta_estimate)));
s3 and s1 are not completely phase synced.

Best Answer

I understand that you are trying to do phase compensation. Please note that a shift in time domain is equivalent to multiplication in frequency domain.
The value of 'theta_estimate" seems to be accurate.
However, one way to perform phase synchronization is to take the Fourier transform of the signal s2, and store it in S2. In the frequency domain, we can multiply S2 with exp(-theta_estimate*i/length(S2)). This is equivalent to shifting the phase of s2 by theta_estimate. We can then obtain the inverse Fourier transform of this product.
Please refer to the following code snippet:
theta = pi/5;
fs = 1000;
t=(0:1000)/fs;
freq = 20;
s1 = sin(2*pi*freq*t);
s2 = sin(2*pi*freq*t + theta);
theta_estimate = acos( 2*mean( s1.*s2) );
%Calculate fft of s2
S2 = fft(s2);
% Multiplication in frequency domain means, shift by "theta_estimate" in time domain. This will
% subtract "theta_estimate" from s2.
S3 = S2.*exp(-i*theta_estimate/(length(S2)));
% Take inverse fft to get signal in time domain
s3p = ifft(S3);
%Calculate the time shift
time_shift_num = theta_estimate/(2*pi*freq)
% Plot both the signals
figure
plot(t,s1,'r*')
hold on
plot(t+time_shift_num,real(s3p),'b--')
The plots should not have phase shift between them now.