我正在尝试从齿轮电动机记录几秒钟的声音以进行频谱分析。我使用了PyAudio,但问题是,每当开始记录时,打开电动机时,声音数据的大小就会很大,即使电动机发出恒定的声音,也要花费几秒钟的时间才能“稳定”下来。当我实时绘制FFT时,我注意到了这一点。这是代码:
我尝试记录很长时间,例如15秒,这给了我时间多次打开和关闭电机。每次打开时,幅度都大于其应有的大小。马达的声音相同。即使从Youtube发出恒定的声音,我也尝试了,但结果还是一样。我不知道为什么。
%matplotlib qt5
import time
import numpy as np
import os
import pyaudio
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.io import wavfile # get the api
import wave
os.chdir('C:\\Users\wang.p.22\Desktop')
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
#RATE = 44100
RATE = 44100
RECORD_SECONDS = 10
WAVE_OUTPUT_FILENAME = "bad1.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
input('Press key to start recording:\n')
print('1 sec delay started')
time.sleep(1)
print("* recording")
frames = []
n = CHUNK
k=np.arange(n)
T = n/RATE
frq = k/T
frq = frq[range(int(n/2))] # one side frequency range
#tmp_fft2 = tmp_fft[range(int(n/2))]
#plt.plot(frq,np.absolute(tmp_fft2),'b')
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
decoded = np.fromstring(data, dtype=np.int16)
fft_decode=fft(decoded)/(len(decoded)/2)
#mags=np.absolute(fft(decoded))
mags=decoded
plt.ylim(top=4000)
plt.xlabel('Freq (Hz)')
plt.ylabel('|Y(freq)|')
plt.plot(frq, mags[range(int(n/2))],'b')
plt.pause(.01)
plt.gcf().clear()
print("* done recording")
plt.close()
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()