问题描述
随机构建一个非常简单的声谱分析仪.
Randomly building a very easy sound spectrum analyzer.
给出以下python代码:
http://rosettacode.org/wiki/Fast_Fourier_transform#Python
像这样实现:
Given this python code:
http://rosettacode.org/wiki/Fast_Fourier_transform#Python
Implemented like this:
import math
from cmath import exp, pi
def fft(x):
N = len(x)
if N <= 1: return x
even = fft(x[0::2])
odd = fft(x[1::2])
return ([even[k] + exp(-2j * pi * k / N) * odd[k] for k in xrange(N / 2)] +
[even[k] - exp(-2j * pi * k / N) * odd[k] for k in xrange(N / 2)])
#res = fft([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0])
res = [math.sin(k) / 2.0 + 0.5 for k in xrange(64)] # positive sine wave
res = fft(res)
for k in xrange(64/2):
# get the amplitude...
sqr = math.sqrt(res[k].real * res[k].real + res[k].imag * res[k].imag)
if sqr > 0:
print 20 * math.log10(sqr) # ...in decibels
else:
print "-INF"
我得到这些结果:
30.1160980385
-22.9398603241
-18.5295301528
-15.0005952198
-11.9809319241
-9.15035811436
-6.26269080991
-3.05038111824
0.930742121289
6.85461898041
23.4890109499 <-- PEAK
11.1462405429
4.62796511517
1.22261338459
-1.03611868864
-2.69639200998
-3.98905968693
-5.03263380282
-5.89570135908
-6.62130526828
-7.23808293566
-7.76594168565
-8.21919399668
-8.60840088318
-8.94151058446
-9.22459042752
-9.46231245749
-9.6582838436
-9.81527579404
-9.93538377539
-10.0201395611
-10.070588143
哪个还可以.我应该在输入正弦波的频率处有一个23 dB的峰值,一切似乎都很好.
Which are pretty OK. I have a 23 dB peak supposedly at the frequency of the input sine wave and everything seems fine.
唯一的是,第一个值是什么:30.1160980385
?难道是低音很大吗?或更可能是算法中的缺陷.在这种情况下,我该如何解决.
Only thing, what is the first value: 30.1160980385
?Is that some extremely loud bass going on? Or more likely a flaw in the algorithm. In this case how do I fix that.
最后一个问题. fft是否可以输入负值可以吗?在上面的示例中,我使用了一个正弦波,该正弦波始终在[0,1]范围内为正.如果我要传递[-1,1]范围内的值,我会搞砸吗?
Last question. Is it ok for the fft to have negative values as input? In the above example I used a sine wave which is always positive ranging in [0,1]. Do I screw up everything if I want to pass values ranging in [-1,1]?
我删除了编辑内容,因为它是题外话.您可以在这里阅读内容:
I removed the edit because it was off-topic. You can read what it was about here:
推荐答案
您显然有很大的 DC组件,因为您的正弦波的偏移量为+0.5.如果您使正弦波的均值为零(即+/- 0.5或+/- 1.0的范围),则您的频谱应在DC(0 Hz)端看起来更好.
You apparently have a large DC component due to the fact that your sine wave has an offset of +0.5. If you make your sine wave have a zero mean (i.e. a range of say +/- 0.5 or +/- 1.0) then your spectrum should look better at the DC (0 Hz) end.
还请注意,在进行FFT之前,您尚未使用窗口函数,因此您将获得光谱泄漏(频谱的拖尾"),这可能会加剧任何直流分量的影响
Note also that you have not use a window function prior to the FFT so you will get spectral leakage (a "smearing" of the spectrum) and this can exacerbate the effect of any DC component.
这篇关于FFT值错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!