-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlivegram.py
More file actions
56 lines (45 loc) · 1.5 KB
/
livegram.py
File metadata and controls
56 lines (45 loc) · 1.5 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
#-------------------------SAR Highperformance-----------------------
#------------------------Spectrogram Generator----------------------
import pyaudio
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import matplotlib.animation as animation
from matplotlib.animation import PillowWriter
from scipy import signal
# Set the audio parameters
RATE = 44100
CHUNK = 1024
DURATION = 0.1
MAX_FRAMES = 500
# Initialize PyAudio and open the audio stream
p = pyaudio.PyAudio()
stream = p.open(
format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=CHUNK
)
# Create the figure and axes for the plot
fig, ax = plt.subplots()
# Function to update the spectrogram
def update_spectrogram(frame):
audio_array = np.frombuffer(stream.read(CHUNK), dtype=np.int16)
frequencies, times, Sxx = signal.spectrogram(audio_array, RATE)
ax.clear()
ax.pcolormesh(times, frequencies, 10 * np.log10(Sxx), shading='auto', cmap='inferno')
ax.set_ylabel('Frequency [Hz]')
ax.set_xlabel('Time [s]')
ax.set_title('Spectrogram')
return ax,
# Create the animation
ani = animation.FuncAnimation(fig, update_spectrogram, frames=None, interval=DURATION * 1000, blit=True, save_count=MAX_FRAMES)
# Show the plot
plt.show()
# Save the animation as a GIF file
ani.save('live_spectrogram.gif', writer=PillowWriter(fps=24))
# Close the audio stream
stream.stop_stream()
stream.close()
p.terminate()