【发布时间】:2014-04-28 09:21:19
【问题描述】:
我的问题是关于快速傅里叶变换,因为这是我第一次使用它们。 因此,我有一组按年份(从 1700 年到 2009 年)和每年对应于某个值(读数)的数据。 当我根据年份绘制读数时,它给了我下面的第一个图。现在,我的目标是使用 FFT 和 python 找到读数最高的主导时期(从图中看来,它似乎在 1940 年至 1950 年左右)。所以我执行了 FFT 并得到了它的幅度和功率谱(见功率谱的第二个图)。功率谱显示主要频率在 0.08 和 0.1(周期/年)之间。我的问题是,我如何将其与读数与年份联系起来?即我如何从这个主导频率中知道哪一年(或几年)是主导频率(或者我如何使用它来找到它)?
数据列表可以在这里找到: http://www.physics.utoronto.ca/%7Ephy225h/web-pages/sunspot_yearly.txt
我写的代码是:
from pylab import *
from numpy import *
from scipy import *
from scipy.optimize import leastsq
import numpy.fft
#-------------------------------------------------------------------------------
# Defining the time array
tmin = 0
tmax = 100 * pi
delta = 0.1
t = arange(tmin, tmax, delta)
# Loading the data from the text file
year, N_sunspots = loadtxt('/Users/.../Desktop/sunspot_yearly.txt', unpack = True) # years and number of sunspots
# Ploting the data
figure(1)
plot(year, N_sunspots)
title('Number of Sunspots vs. Year')
xlabel('time(year)')
ylabel('N')
# Computing the FFT
N_w = fft(N_sunspots)
# Obtaining the frequencies
n = len(N_sunspots)
freq = fftfreq(n) # dt is default to 1
# keeping only positive terms
N = N_w[1:len(N_w)/2.0]/float(len(N_w[1:len(N_w)/2.0]))
w = freq[1:len(freq)/2.0]
figure(2)
plot(w, real(N))
plot(w, imag(N))
title('The data function f(w) vs. frequency')
xlabel('frequency(cycles/year)')
ylabel('f(w)')
grid(True)
# Amplitude spectrum
Amp_spec = abs(N)
figure(3)
plot(w, Amp_spec)
title('Amplitude spectrum')
xlabel('frequency(cycles/year)')
ylabel('Amplitude')
grid(True)
# Power spectrum
Pwr_spec = abs(N)**2
figure(4)
plot(w, Pwr_spec 'o')
title('Power spectrum')
xlabel('frequency(cycles/year)')
ylabel('Power')
grid(True)
show()
【问题讨论】: