I have a two problems:
1- estimate the phase of an unknown wave, after that 2- correct/change the phase of the investigated signal by adding or subtract to be in the phase of another wave.
I used the following code to create a sine wave and tried my idea with it
import numpy as np
import scipy.optimize as optimize
import scipy.fftpack as fftpack
import matplotlib.pyplot as plt
pi = np.pi
##############
# generate a perfect sine wave
def mysine(time, amplitude, frequency, phase):
return amplitude * np.sin(frequency * time + phase)
def sin_signal(time, amplitude, frequency, phase):
return amplitude * np.sin(frequency * time + phase)
#****************************** Signal conditions ******************************
number_of_points = 200
time = np.linspace(0,0.002,number_of_points)
frequency = 2500
amplitude = 1
phase = 0
##############################################
sin1_signal = sin_signal(time, amplitude, frequency,phase)
Real_time = time
Real_signal = sin1_signal
# Get the frequency of signal
yhat = fftpack.rfft(Real_signal)
idx = (yhat**2).argmax()
freqs = fftpack.rfftfreq(number_of_points,d = (Real_time[1]-Real_time[0])/(2*pi))
frequency_est = freqs[idx]
# Get the amplitude of signal
amplitude_est = Real_signal.max()
# Guess amplitude_est, frequency_est and phase
guess = [amplitude_est, frequency_est, 0]
(amplitude_est, frequency_est, phase_est), pcov = optimize.curve_fit(mysine, Real_time, Real_signal, guess)
period = 2*pi/frequency_est
print(amplitude_est, frequency_est, phase_est)
this code gives me the following result: amplitude = 1.0 frequency = 2500.0 Phase = 0
So, the 1st problem is solved.
The 2nd problem: How to change the phase of the investigated signal??