-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize-histogram.py
62 lines (54 loc) · 1.94 KB
/
visualize-histogram.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
import serial
import matplotlib.pyplot as plt
import numpy as np
# Replace with your serial port and baud rate
SERIAL_PORT = '/dev/ttyUSB0'
BAUD_RATE = 115200
def parse_fft_data(line):
try:
data = line.strip().decode('utf-8')
if data.startswith('(') and data.endswith(')'):
# Split by commas, remove parentheses, and convert to float
data = data[1:-1].split('),(')
frequencies = []
magnitudes = []
for point in data:
freq, mag = map(float, point.split(','))
frequencies.append(freq)
magnitudes.append(mag)
return frequencies, magnitudes
except Exception as e:
print(f"Error parsing data: {e}")
return [], []
def main():
ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
plt.ion()
fig, ax = plt.subplots()
bars = None
maxMag = 0
try:
while True:
line = ser.readline()
frequencies, magnitudes = parse_fft_data(line)
if frequencies and magnitudes:
if bars is None:
bars = ax.bar(frequencies, magnitudes, width=2)
ax.set_xlim(min(frequencies), max(frequencies))
ax.set_ylim(0, 8500)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.title('Real-time FFT Data')
else:
for bar, mag in zip(bars, magnitudes):
bar.set_height(mag)
if mag > maxMag:
maxMag = mag
ax.set_ylim(0, maxMag)
ax.set_xticks(frequencies)
ax.set_xticklabels([f"{int(freq)}" for freq in frequencies], rotation=45)
plt.pause(0.01) # Adjust the pause time as needed
finally:
print("max magnitude:")
print(maxMag)
if __name__ == "__main__":
main()