Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Matplotlib realtime audio FFT
#!/usr/bin/env python
# encoding: utf-8
## Module infomation ###
# Python (3.4.4)
# numpy (1.10.2)
# PyAudio (0.2.9)
# matplotlib (1.5.1)
# All 32bit edition
########################
import numpy as np
import pyaudio
import matplotlib.pyplot as plt
class SpectrumAnalyzer:
FORMAT = pyaudio.paFloat32
CHANNELS = 1
RATE = 16000
CHUNK = 512
START = 0
N = 512
wave_x = 0
wave_y = 0
spec_x = 0
spec_y = 0
data = []
def __init__(self):
self.pa = pyaudio.PyAudio()
self.stream = self.pa.open(format = self.FORMAT,
channels = self.CHANNELS,
rate = self.RATE,
input = True,
output = False,
frames_per_buffer = self.CHUNK)
# Main loop
self.loop()
def loop(self):
try:
while True :
self.data = self.audioinput()
self.fft()
self.graphplot()
except KeyboardInterrupt:
self.pa.close()
print("End...")
def audioinput(self):
ret = self.stream.read(self.CHUNK)
ret = np.fromstring(ret, np.float32)
return ret
def fft(self):
self.wave_x = range(self.START, self.START + self.N)
self.wave_y = self.data[self.START:self.START + self.N]
self.spec_x = np.fft.fftfreq(self.N, d = 1.0 / self.RATE)
y = np.fft.fft(self.data[self.START:self.START + self.N])
self.spec_y = [np.sqrt(c.real ** 2 + c.imag ** 2) for c in y]
def graphplot(self):
plt.clf()
# wave
plt.subplot(311)
plt.plot(self.wave_x, self.wave_y)
plt.axis([self.START, self.START + self.N, -0.5, 0.5])
plt.xlabel("time [sample]")
plt.ylabel("amplitude")
#Spectrum
plt.subplot(312)
plt.plot(self.spec_x, self.spec_y, marker= 'o', linestyle='-')
plt.axis([0, self.RATE / 2, 0, 50])
plt.xlabel("frequency [Hz]")
plt.ylabel("amplitude spectrum")
#Pause
plt.pause(.01)
if __name__ == "__main__":
spec = SpectrumAnalyzer()
@hyperconcerto

This comment has been minimized.

Copy link
Owner Author

hyperconcerto commented Jan 27, 2016

Windows 7 64bit
Python 3.4.4 x86
numpy 1.10.2 x86
matplotlib 1.5.1 x86
Pyaudio 0.2.9 x86

@bru32

This comment has been minimized.

Copy link

bru32 commented Jul 24, 2018

this is great but how would you apply a Hanning window to the fft plot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.