My goal is to do spectrum analysis of a gear motor. I used Python with the Pyaudio package and made a crude spectrum analyzer that displays the FFT as the sound is being recorded. I recorded 10 seconds of sound just for test. Sample rate is 44100, each frame is 1024 samples. For each frame I plotted the FFT, and made a dynamic plot thru the 10 seconds.
I also downloaded a FFT analyzer to my smartphone and compared the results as I turned on a motor. The smartphone app is able to display relatively stable spectrum. In my plot, the magnitudes are very high upon turning on the motor, and only "settled" after a couple of seconds.
I wonder why that is. Is it due to my sound card or laptop mic? I tested using another laptop and got similar result. Is it because I recorded in Mono mode? The spectrum changes too much as I move the motor around, whereas on the smartphone the spectrum is more stable.
Or is it because I didn't use a Window function? Here is my code:
import numpy as np import os import time import pyaudio import matplotlib.pyplot as plt from scipy.fftpack import fft CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 RECORD_SECONDS = 10 WAVE_OUTPUT_FILENAME = "test.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') #delay so keystroke doesn't get recorded time.sleep(1) print("* recording") frames =  n = 1024 k=np.arange(n) T = n/RATE frq = k/T frq = frq[range(int(n/2))] # one side frequency range for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) decoded = np.fromstring(data, dtype=np.int16) #grab the data in stream fft_decode=fft(decoded)/(len(decoded)/2) #normalized FFT mags=np.absolute(fft(decoded)) # plt.ylim(top=55000) 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()
How can I improve my analyzer? Is there anything wrong with my code or the way I approached this?
Edit: Nevermind. The reason turns out to be the boost function of my microphone, and disabling it solved my problem.