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) );
S2 = fft(s2);
S3 = S2.*exp(-i*theta_estimate/(length(S2)));
s3p = ifft(S3);
time_shift_num = theta_estimate/(2*pi*freq)
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.
Best Answer