-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_fourier.py
67 lines (51 loc) · 1.44 KB
/
plot_fourier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python3
import numpy as np
import matplotlib.pyplot as plt
from collections import deque
import sounddevice as sd
from queue import Queue
from drawnow import drawnow
import serial
import numpy as np
# from matplotlib import pyplot as plt
# plt.ion() # set plot to animated
RATE = 44100
CHANNELS = 1
queue = Queue()
times = Queue()
def callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
time = np.linspace(time.inputBufferAdcTime, time.currentTime, 512)
if status:
print(status, flush=True)
times.put(time)
queue.put(indata)
plt.ion() # enable interactivity
fig=plt.figure() # make a figure
xList = []
yList = []
def makeFig():
spectrum = np.fft.fft(yList)
frequencies = np.fft.fftfreq(len(spectrum))
plt.plot(frequencies,spectrum)
plt.ylim(-10e9, 10e9)
plt.pause(1e-9)
# plt.show()
# plt.pause(1)
# plt.ylim(-1e8, 1e8)
# plt.scatter(xList, yList)
# plt.xlim(xList[0], xList[-1])
# plt.pause(1e-9)
with sd.InputStream(samplerate=RATE, device=None, channels=CHANNELS, callback=callback, dtype='int32'):
print("#" * 80)
print("press Ctrl+C to stop the recording")
print("#" * 80)
while True:
time = times.get()
data = queue.get().reshape(512)
xList = time
yList = data
# xList.append(time)
# yList.append(data)
drawnow(makeFig)
# plt.pause(1e-9)