-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrit.py
More file actions
executable file
·95 lines (74 loc) · 3.39 KB
/
scrit.py
File metadata and controls
executable file
·95 lines (74 loc) · 3.39 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding=utf-8
# !/usr/bin/python3
import pyaudio
import wave
import matplotlib.pyplot as plt
import numpy as np
RATE = 44100
CHUNK = 2048 # RATE / number of updates per second
# ======================================================================================================================
# Setup graphics
# ======================================================================================================================
def configure_graphics():
global ax1, ax2
plt.ion()
fig = plt.figure("Sinal Elétrico")
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
# ======================================================================================================================
# Plot time domain
# ======================================================================================================================
def show_song_device(audio_handler):
for i in range(audio_handler.get_device_count()):
dev = audio_handler.get_device_info_by_index(i)
print(i, dev['name'], dev['maxInputChannels'])
# ======================================================================================================================
# Plot time domain
# ======================================================================================================================
def plot_signal_time(signal_temp):
ax1.cla()
ax1.plot(signal_temp)
ax1.grid()
# ax1.axis([0, CHUNK, -32768, 32767]) # 65536 = 2^16 bits
ax1.axis([0, CHUNK, min(signal_temp), max(signal_temp)])
ax1.set_xlabel("Sinal no tempo")
# ======================================================================================================================
# Plot frequency domain
# ======================================================================================================================
def plot_signal_frequency(signal_freq):
array_freq = np.fft.rfftfreq(CHUNK, 1. / RATE)
ax2.cla()
ax2.plot(array_freq, signal_freq)
ax2.grid()
ax2.axis([0, 5000, 0, max(signal_freq)])
ax2.set_xlabel("Sinal em frequencia")
# ======================================================================================================================
# Plot signals
# ======================================================================================================================
def plot_signal(data):
signal_temp = np.array(wave.struct.unpack("%dh" % CHUNK, data)) * np.blackman(CHUNK)
signal_freq = np.abs(np.fft.rfft(signal_temp))
plot_signal_time(signal_temp)
plot_signal_frequency(signal_freq)
plt.pause(0.0001)
# ======================================================================================================================
# Main
# ======================================================================================================================
if __name__ == "__main__":
p = pyaudio.PyAudio()
# show_song_device(p)
id_song_device = 1 # default -> USB Audio Device: - (hw:1,0) 2
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
input_device_index=id_song_device)
configure_graphics()
try:
while 1:
plot_signal(stream.read(CHUNK, exception_on_overflow=False))
except:
stream.stop_stream()
stream.close()
p.terminate()