我不知道这是编程还是数学问题,但我已经汇总了一些简短的FFT例子。我加载440hz波并在顶部添加一些正弦波,但由于某种原因,光谱有一个我不明白的“波”。 据我所知,光谱应该具有相同的| Y(freq)|所有频率的值。
from pylab import plot, show, xlabel, ylabel, subplot
from scipy import fft, arange
from numpy import linspace, array
# from scipy.io.wavfile import read,write
import scikits.audiolab as audio
import math
def plotSpectru(y,Fs):
n = len(y) # lungime semnal
k = arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n/2)] # one side frequency range
Y = fft(y)/n # fft computing and normalization
Y = Y[range(n/2)]
plot(frq,abs(Y),'r') # plotting the spectrum
xlabel('Freq (Hz)')
ylabel('|Y(freq)|')
Fs = 44100; # sampling rate
# (data, rate, bits) = audio.wavread('440Hz_44100Hz_16bit_05sec.wav')
(data, rate, bits) = audio.wavread('250Hz_44100Hz_16bit_05sec.wav')
for n in xrange(0,4*120, 4):
n=n/40.
data = array([x+math.sin(n*idx) for idx,x in enumerate(data)])
y=data[:]
lungime=len(y)
timp=len(y)/44100.
t=linspace(0,timp,len(y))
subplot(2,1,1)
plot(t,y, color="green")
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
plotSpectru(y,Fs)
show()
答案 0 :(得分:0)
我不完全明白你想要如何计算正弦波?这是一个如何用numpy添加正弦波的小例子。
duration = len(data)/float(rate)
t = np.linspace(0, len(data), rate*duration)
sinewave = np.sin(2*np.pi*440*t)
data += sinewave
修改强>
抱歉,我回答了你的问题并认出我的答案与你的问题不符。 即使你真的添加了所有正频率(由fft分析),你也没有统一| Y(频率)|。
duration = len(data)/float(rate)
t = np.linspace(0, len(data), rate*duration)
allfreqs = np.fft.fftfreq(len(data), 1.0/rate)
for f in allfreqs[:len(allfreqs)/2]:
data += np.sin(2*np.pi*f*t)
据我所知,这是因为干扰。如果加上这么多正弦波,很可能会有些变弱,有些变得更强。
如果为每个wave指定一个随机阶段,情况会有所不同:
duration = len(data)/float(rate)
t = np.linspace(0, len(data), rate*duration)
allfreqs = np.fft.fftfreq(len(data), 1.0/rate)
for f in allfreqs[:len(allfreqs)/2]:
data += np.sin(2*np.pi*f*t + np.random.rand()*np.pi)