我试图了解python中fft的工作原理(或一般情况下) 当我有一个记录了几秒钟的信号时,我只能显示该FFT的一秒钟 有没有办法获取所有数据并将其平均化? 我之前在LabView中做过这个。 任何人都可以帮忙吗?
这是我正在使用的示例代码。
from numpy import linspace, sin, pi, log10, average, arange
from matplotlib.pyplot import plot, show, figure
from numpy.fft import fft, fftfreq
N = 1000 * 60 * 4
dt = 1 / 1000
x = linspace(0, N*dt, N)
freq = linspace(10, 200, N)
sinsweep = sin(x*freq)
mavg = [average(sinsweep[i*60:(i+1)*60]) for i in range(int(N/60))]
plot(freq, sinsweep, '.')
plot(linspace(10, 200, int(N/60)), mavg, '.')
f = figure()
t = arange(60)
sp = fft(mavg, n=60)
freq = fftfreq(t.shape[-1])
plot(sp.imag)
show()
答案 0 :(得分:1)
我对您的代码进行了一些修改,以获得漂亮的光谱。首先,我增加点数以验证香农标准。和一些技巧 提高速度。
from numpy import linspace, sin, pi, log10, average, arange
from matplotlib.pyplot import plot, show, figure
from numpy.fft import fft, fftfreq
close()
N = 10000 * 60 * 4
dt = 1 / 10000
t = arange(0, N*dt, dt)
freq = linspace(10, 200, N)
sinsweep = sin(t*freq)
mavg = sinsweep.reshape(-1,60).mean(1)
tm=t[::60]
figure('signal')
plot(tm, mavg)
sp = fft(mavg)
freq = fftfreq(tm.size,dt*60)
valid=freq>0
figure('spectrum')
plot(freq[valid],abs(sp[valid]))
show()
这是你所期望的吗?