Is it possible in principle to correctly extract the phase from Fourier transform?
I just tried to do so using Python
, here some attempts:
# attempt 1, phase=pi/2
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fs = 1000
T = 1/fs
t = np.linspace(0,N*T, N)
N=len(t)
phase = np.pi/2
signal = 4*np.sin(2*np.pi*2*t-phase)
FFT = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(signal), T)
plt.plot(freqs[0:N//2], 2/N*np.abs(FFT[0:N//2]), label='amplitude')
plt.plot(freqs[0:N//2], np.angle(FFT[0:N//2]), label='phase')
plt.axhline(phase, color='k', linestyle='--', label='phase value')
plt.legend()
plt.grid()
which outputs:
zoomed-in:
The first question that comes to my mind - why does the phase so blurred (why it is not just one point as for the amplitude spectrum?)
if I try another phase:
fs = 1000
T = 1/fs
t = np.linspace(0,N*T, N)
N=len(t)
phase = -np.pi/8
signal = 4*np.sin(2*np.pi*2*t-phase)
FFT = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(signal), T)
plt.plot(freqs[0:N//2], 2/N*np.abs(FFT[0:N//2]), label='amplitude')
plt.plot(freqs[0:N//2], np.angle(FFT[0:N//2]), label='phase')
plt.axhline(phase, color='k', linestyle='--', label='phase value')
plt.legend()
plt.grid()
it outputs:
The second question that I have - why does the phase exist in the whole frequency range, not only the frequency I'm interested in?
According to these examples I can't extract exact phase of original signal, is it correct?