我在信号上使用scipy.fft
,用移动窗口绘制频率随时间变化的幅度(此处为an example,时间为X,频率为Y,幅度为颜色)。
但是,只有少数频率让我感兴趣(仅限3,4个频率)。使用FFT,似乎我不能只选择我想要的频率(显然频率范围由算法确定),所以我计算了很多无用的东西,我的程序甚至崩溃了{{1如果信号太长。
我该怎么办?我是否必须使用自定义傅立叶变换 - 在这种情况下,欢迎良好实现的链接 - 或者是否有MemoryError
方式?
修改
在@jfaller回答之后,我决定(尝试)实施Goertzel算法。我提出了这个问题:https://gist.github.com/4128537但它不起作用(频率440不会出现,永远不会找到峰值,我没有费心去应用合适的窗口)。任何帮助!?我对DSP很不好。
答案 0 :(得分:8)
您真的希望使用Goertzel算法:http://en.wikipedia.org/wiki/Goertzel_algorithm。基本上,它是单点FFT,如果您只需要信号中有限数量的频率,则效率很高。如果您无法从维基百科中分离算法,请回过头来,我会帮助您。此外,如果你谷歌一些资源,存在用python编写的DTMF解码器(按键式电话解码器)。你可以看看他们是如何做到的。
答案 1 :(得分:7)
在@ jfaller,@eryksun的帮助下,...我在Python中实现了一个简单的Goertzel算法:https://gist.github.com/4128537。我会在这里重新粘贴它。正如@eryksun所提到的那样,可以使用scipy.signal.lfilter
和lfiltic
来优化它,但目前我会坚持使用它,因为我可能想用另一种语言实现它:
import math
def goertzel(samples, sample_rate, *freqs):
"""
Implementation of the Goertzel algorithm, useful for calculating individual
terms of a discrete Fourier transform.
`samples` is a windowed one-dimensional signal originally sampled at `sample_rate`.
The function returns 2 arrays, one containing the actual frequencies calculated,
the second the coefficients `(real part, imag part, power)` for each of those frequencies.
For simple spectral analysis, the power is usually enough.
Example of usage :
# calculating frequencies in ranges [400, 500] and [1000, 1100]
# of a windowed signal sampled at 44100 Hz
freqs, results = goertzel(some_samples, 44100, (400, 500), (1000, 1100))
"""
window_size = len(samples)
f_step = sample_rate / float(window_size)
f_step_normalized = 1.0 / window_size
# Calculate all the DFT bins we have to compute to include frequencies
# in `freqs`.
bins = set()
for f_range in freqs:
f_start, f_end = f_range
k_start = int(math.floor(f_start / f_step))
k_end = int(math.ceil(f_end / f_step))
if k_end > window_size - 1: raise ValueError('frequency out of range %s' % k_end)
bins = bins.union(range(k_start, k_end))
# For all the bins, calculate the DFT term
n_range = range(0, window_size)
freqs = []
results = []
for k in bins:
# Bin frequency and coefficients for the computation
f = k * f_step_normalized
w_real = 2.0 * math.cos(2.0 * math.pi * f)
w_imag = math.sin(2.0 * math.pi * f)
# Doing the calculation on the whole sample
d1, d2 = 0.0, 0.0
for n in n_range:
y = samples[n] + w_real * d1 - d2
d2, d1 = d1, y
# Storing results `(real part, imag part, power)`
results.append((
0.5 * w_real * d1 - d2, w_imag * d1,
d2**2 + d1**2 - w_real * d1 * d2)
)
freqs.append(f * sample_rate)
return freqs, results
if __name__ == '__main__':
# quick test
import numpy as np
import pylab
# generating test signals
SAMPLE_RATE = 44100
WINDOW_SIZE = 1024
t = np.linspace(0, 1, SAMPLE_RATE)[:WINDOW_SIZE]
sine_wave = np.sin(2*np.pi*440*t) + np.sin(2*np.pi*1020*t)
sine_wave = sine_wave * np.hamming(WINDOW_SIZE)
sine_wave2 = np.sin(2*np.pi*880*t) + np.sin(2*np.pi*1500*t)
sine_wave2 = sine_wave2 * np.hamming(WINDOW_SIZE)
# applying Goertzel on those signals, and plotting results
freqs, results = goertzel(sine_wave, SAMPLE_RATE, (400, 500), (1000, 1100))
pylab.subplot(2, 2, 1)
pylab.title('(1) Sine wave 440Hz + 1020Hz')
pylab.plot(t, sine_wave)
pylab.subplot(2, 2, 3)
pylab.title('(1) Goertzel Algo, freqency ranges : [400, 500] and [1000, 1100]')
pylab.plot(freqs, np.array(results)[:,2], 'o')
pylab.ylim([0,100000])
freqs, results = goertzel(sine_wave2, SAMPLE_RATE, (400, 500), (1000, 1100))
pylab.subplot(2, 2, 2)
pylab.title('(2) Sine wave 660Hz + 1200Hz')
pylab.plot(t, sine_wave2)
pylab.subplot(2, 2, 4)
pylab.title('(2) Goertzel Algo, freqency ranges : [400, 500] and [1000, 1100]')
pylab.plot(freqs, np.array(results)[:,2], 'o')
pylab.ylim([0,100000])
pylab.show()