1

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??

H. H
  • 63
  • 1
  • 6
  • You construct your curve with a sine function, but you fit with a cosine function. Phase difference is pi/2 which is 1.5708 – Mr. T Jul 22 '18 at 20:12
  • @Mr. T - please, tell me where is my fault? – H. H Jul 22 '18 at 20:16
  • There is no fault. You fit a sine wave with a cosine function, so amplitude and frequency are the same, but the phase shifts by pi/2. Everything works exactly as defined. – Mr. T Jul 22 '18 at 20:18
  • Yes thats correct, check your sin_signal function, you use cosine. – avermaet Jul 22 '18 at 20:28
  • @ Mr. T - Thanks for your notification I corrected it to the sine function. How to change the phase of this sine signal (add or subtract) to be like cos signal for example? – H. H Jul 22 '18 at 20:31
  • @ avermaet - Yes, thanks – H. H Jul 22 '18 at 20:39

0 Answers0