1 year ago
#378131
pattylyn
fourier transform for time series
I tried to do a spectrum analysis with time series (150years * 12 months) using numpy.fft, but my amplitude for the fft seems very ood, may I ask what's wrong with it?
f = xr.open_dataset('/ERSSTONI1950-2021.nc')
x = f["oni"].values
data = stats.zscore(x)
PXX = fft.fft(data)
f = fft.fftfreq(len(data),1)
def auto_corrcoef(x):
result=np.corrcoef(x[1:-1], x[2:])[0,1]
return result
result = auto_corrcoef(data)
red = 5.991*(1-result**2)/(len(data)*(1+result**2 - 2*result*np.cos(2*np.pi*f)))
fig,ax = plt.subplots(figsize=(10,5))
ax.set_title("R(1)=" '%.2f' % result,loc='right',fontsize=14)
ax.plot(1/f/12,PXX,'-k',label='Power Spectrum')
ax.set_xlabel("Period(year)")
ax.plot(1/f/12,red,'--r',label='red noise(95%)')
ax.set_ylabel("PSD")
ax.set_xlim(100,2)
ax.set_ylim(0,0.4)
plt.title("Spectral analysis of Nino3.4(DJF)")
plt.show()
plt.savefig("Spectrum-ERSSToni.png")`your text`
plt.show()
python
spectrum
0 Answers
Your Answer