Scipy Fft - How To Get Phase Angle
Solution 1:
An FFT measures circular phase, referenced to both the very beginning and very end of the input data window. If your input sine wave isn't exactly integer periodic in the FFT aperture, then there will be a discontinuity between the phase at the beginning and end of the window, thus the FFT phase measurement won't be what you might expect.
Instead, I recommend one begin (or reference) the input sinewave at the desired phase in the middle of your data window (at N/2), not the beginning. Then do an circular fftshift before the FFT. The resulting FFT phase measurement will then represent phase with respect to the middle of your original data window, where there is no discontinuity; and atan2() will represent the ratio between the even and odd components of your continuous input waveform as is more typically expected.
Solution 2:
This is the simplest code to show how to get the angles.
Note that I've created the signal y
such that there is an integer number of periods in it (as I suggested in a comment, and @hotpaw2 suggested in their answer).
This is the problem with OP's code.
I used linspace
to create the time axis, using the endpoint=False
option. This is important, if t
were to contain the number 10, then we no longer have an exact integer number of periods. When working with the discrete Fourier transform, it helps to think of what happens when you repeat the signal. Simply take y
, and concatenate a copy of itself: np.hstack((y,y))
. Is this new signal still a sampling of a single sine wave, or did we create something more complex? What happens at that point where the two copies meet?
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack
phase = np.pi / 4
t = np.linspace(0, 10, num=200, endpoint=False)
y = np.cos(2 * np.pi * t + phase)
Y = scipy.fftpack.fftshift(scipy.fftpack.fft(y))
f = scipy.fftpack.fftshift(scipy.fftpack.fftfreq(len(t)))
p = np.angle(Y)
p[np.abs(Y) < 1] = 0
plt.plot(f, p)
plt.show()
Post a Comment for "Scipy Fft - How To Get Phase Angle"