I have a sine wave of the known frequency with some noise with uniform samples near Nyquist frequency. I want to get approximate values of amplitude, phase, and DC offset. I searched for an answer and found a couple of answers close to what I needed, but still was unable to write a proper code that achieves what I need. When I run the code below, I get the wrong phase and amplitude. Would be happy to get some help.
import sys
import numpy
import pylab as plt
def cosfunc(time, amplitude, omega, phase, offset):
''' Function to create sine wave. Phase in radians '''
return amplitude * numpy.cos(omega*time + phase) + offset
def get_cosine_approx(timeline,sine_data):
points_num=len(timeline)
fft_freq = numpy.fft.fftfreq(points_num-1, timeline[1]-timeline[0]) # assume uniform spacing
fft_result=numpy.fft.fft(sine_data)
#Remove negative frequencies
for i in range(len(fft_freq)):
if fft_freq[i]<0:
fft_result[i]=0
ampl=numpy.abs(fft_result)/points_num*2
max_index=numpy.argmax(ampl)
guess_amplitude=ampl[max_index]
phase_unwrapped=numpy.unwrap(numpy.angle(fft_result))
guess_phase=phase_unwrapped[max_index]
guess_phase_dig=guess_phase*180./numpy.pi
print("freq",fft_freq[max_index])
print("amplitude",guess_amplitude)
print("phase",guess_phase_dig)
plt.plot(timeline, sine_data, "ok", label="sine")
new_timeline=numpy.linspace(timeline[0], timeline[-1], len(timeline)*1000)
plt.plot(new_timeline, cosfunc(new_timeline,guess_amplitude,2.*numpy.pi*56e9,guess_phase,0), "r-", label="fit")
plt.legend(loc="best")
plt.show()
return {"amp":guess_amplitude, "ph":guess_phase,"ph_dig":guess_phase_dig}
N = 256 # Sample points
f=56e9 #56GHz
t = numpy.linspace(0.0, 100./f, N) # Time
omega = 2.*numpy.pi*f
offset=0
phase=0
A=1.
cos=cosfunc(t,A,omega,phase,offset)
result=get_cosine_approx(t,cos)
You are catching the phase at an inflection point, where the phase is suddenly transitioning from +pi/2 to -pi/2, and the bin you are looking at is just partway through the downhill slide. This is just because the FFT results are not continuous. A single bin spans a range of frequencies.
Notice when we plot the phase and the amplitude:
import sys
import numpy as np
import matplotlib.pyplot as plt
def cosfunc(time, amplitude, omega, phase, offset):
''' Function to create sine wave. Phase in radians '''
return amplitude * np.cos(omega*time + phase) + offset
def get_cosine_approx(timeline,sine_data):
points_num=len(timeline)
fft_freq = np.fft.fftfreq(points_num, timeline[1]-timeline[0])
fft_result=np.fft.fft(sine_data)
fft_freq = np.fft.fftshift(fft_freq)
fft_result = np.fft.fftshift(fft_result)
ampl = np.abs(fft_result) * 2 / points_num
phase = np.angle(fft_result)
plt.plot(fft_freq, ampl, label='ampl' )
plt.plot(fft_freq, phase, label='phase' )
plt.legend(loc="best")
plt.show()
return 0
N = 256 # Sample points
f=56e9 #56GHz
t = np.linspace(0.0, 100./f, N) # Time
omega = 2.*np.pi*f
offset=0
phase=0
A=1.
cos=cosfunc(t,A,omega,phase,offset)
result=get_cosine_approx(t,cos)
The plot shows the inflection point right at the peak frequency bin.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.