I want to obtain the power spectral density (PSD) of an autoregressive sequence, AR(1). The analytical solution according to this reference (page 12) is
For $X_t = \phi_1X_{t-1}+W_t, W_t \sim N(0,\sigma^2_w)$,
$$f(w) = \frac{\sigma_w^2}{1-2\phi_1 \cos(2 \pi w)+\phi_1^2}$$
Let $\sigma_w = 1$, $\phi_1 = 0.4$, then the corresponding PSD curve in [0, 2pi] using fft and analytical solution is
As can be seen from the figure below, the PSD obtained from fft is far from being a U-shaped curve obtained by the analytical solution.
Can anyone enlighten me on why the results are different?
The code I used to generate the plot is:
import matplotlib.pyplot as plt
import numpy as np
from scipy.fft import fft, fftfreq
def analy(x):
PSD = 1/(1.16-0.8*np.cos(2*np.pi*x))
return PSD
def my_fft(N):
T = 1 # sample spacing
t = np.linspace(0.0, N*T, N, endpoint=True)
y = np.zeros([N,1])
rho = 0.4
for i in range(len(t)-1):
y[i+1] = rho*y[i] + (1-0)*np.random.normal(0,1)
yf = fft(y)
xf = fftfreq(N, T)[:N//2] # sample frequency points
PSD = 2.0/N * np.abs(yf[0:N//2])
return xf*np.pi*2, PSD
def compare_PSD():
N = 100 # Number of sample points
UB = np.pi # sample upper bound
x_analy = np.linspace(0, UB, num=N)
PSD_analy = analy(x_analy)
x_fft, PSD_fft = my_fft(N)
plt.plot(x_analy, PSD_analy, label='Analytical')
plt.plot(x_fft, PSD_fft, label='scipy.fft')
plt.xlabel('Frequency')
plt.ylabel('PSD')
plt.legend()
plt.show()
compare_PSD()