# why is my FFT magnitude of real time recording so volatile?

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)):
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.

• While the parameters of your program are stated here, we don’t know what they are on your cell phone app. it is probably averaging a few frames prior to displaying. this is a guess and remotely guessing is not efficient. i suggest you do something like using an app like audacity to capture the data in real time and process the wave file separately as your first version. once satisfied, develop a real time version. a separate microphone, away from your computer might be helpful – user28715 Jan 15 '19 at 7:28
• the rate that you are processing data is about 40 frames per second which is a bit too fast to display it. 20 frames a second for display is a practical display rate. your phone app probably averages fft chunks for a reasonable display rate and it smooths the fft magnitudes that see. you should expect to see start up transients that quickly subside – user28715 Jan 15 '19 at 16:49