-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathDemo_Matplotlib_Image_Elem_Spetrogram_Animated.py
154 lines (122 loc) · 4.99 KB
/
Demo_Matplotlib_Image_Elem_Spetrogram_Animated.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import FreeSimpleGUI as sg
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
import matplotlib.pyplot as plt
import io
import time
"""
Demo_Matplotlib_Image_Elem_Spetrogram_Animated Demo
Demo to show
* How to use an Image element to show a Matplotlib figure
* How to draw a Spectrogram
* How to animate the drawing by simply erasing and drawing the entire figure
The point here is to keep things simple to enable you to get started.
The example static graph can be found in the matplotlib gallery:
https://matplotlib.org/stable/gallery/images_contours_and_fields/specgram_demo.html
Copyright 2021 PySimpleGUI
"""
np.random.seed(19801)
# .d88888b dP dP
# 88. "' 88 88
# `Y88888b. d8888P .d8888b. 88d888b. d8888P
# `8b 88 88' `88 88' `88 88
# d8' .8P 88 88. .88 88 88
# Y88888P dP `88888P8 dP dP
# oooooooooooooooooooooooooooooooooooooooooo of your Matplotlib code
def your_matplotlib_code():
# The animated part of this is the t_lower, t_upper terms as well as the entire dataset that's graphed.
# An entirely new graph is created from scratch every time... implying here that optimization is possible.
if not hasattr(your_matplotlib_code, 't_lower'):
your_matplotlib_code.t_lower = 10
your_matplotlib_code.t_upper = 12
else:
your_matplotlib_code.t_lower = (your_matplotlib_code.t_lower + .5) % 18
your_matplotlib_code.t_upper = (your_matplotlib_code.t_upper + .5) % 18
dt = 0.0005
t = np.arange(0.0, 20.0, dt)
s1 = np.sin(2 * np.pi * 100 * t)
s2 = 2 * np.sin(2 * np.pi * 400 * t)
# create a transient "chirp"
# s2[t <= 5] = s2[15 <= t] = 0 # original line of code (not animated)
# If running the animation, use the t_lower and t_upper values
s2[t <= your_matplotlib_code.t_lower] = s2[your_matplotlib_code.t_upper <= t] = 0
# add some noise into the mix
nse = 0.01 * np.random.random(size=len(t))
x = s1 + s2 + nse # the signal
NFFT = 1024 # the length of the windowing segments
Fs = int(1.0 / dt) # the sampling frequency
fig, (ax2) = plt.subplots(nrows=1)
# ax1.plot(t, x)
Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
return fig
# 88888888b dP
# 88 88
# 88aaaa 88d888b. .d888b88
# 88 88' `88 88' `88
# 88 88 88 88. .88
# 88888888P dP dP `88888P8
# ooooooooooooooooooooooooooooo of your Matplotlib code
# ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
# dP dP dP
# 88 88 88
# 88aaaaa88a .d8888b. 88 88d888b. .d8888b. 88d888b.
# 88 88 88ooood8 88 88' `88 88ooood8 88' `88
# 88 88 88. ... 88 88. .88 88. ... 88
# dP dP `88888P' dP 88Y888P' `88888P' dP
# ooooooooooooooooooooooo~88~oooooooooooooooooooooooo function starts here
# dP
def draw_figure(element, figure):
"""
Draws the previously created "figure" in the supplied Image Element
:param element: an Image Element
:param figure: a Matplotlib figure
:return: The figure canvas
"""
plt.close('all') # erases previously drawn plots
canv = FigureCanvasAgg(figure)
buf = io.BytesIO()
canv.print_figure(buf, format='png')
if buf is not None:
buf.seek(0)
element.update(data=buf.read())
return canv
else:
return None
# .88888. dP dP dP
# d8' `88 88 88 88
# 88 88 88 88
# 88 YP88 88 88 88
# Y8. .88 Y8. .8P 88
# `88888' `Y88888P' dP
# ooooooooooooooooooooooo
def main():
# define the window layout
layout = [[sg.Text('Spectrogram Animated - Not Threaded', font='Helvetica 24')],
[sg.pin(sg.Image(key='-IMAGE-'))],
[sg.T(size=(50, 1), k='-STATS-')],
[sg.B('Animate', focus=True, k='-ANIMATE-')]]
# create the form and show it without the plot
window = sg.Window('Animated Spectrogram', layout, element_justification='c', font='Helvetica 14')
counter = delta = start_time = 0
timeout = None
while True:
event, values = window.read(timeout=timeout)
if event == sg.WIN_CLOSED:
break
sg.timer_start()
if event == '-ANIMATE-':
timeout = 0
window['-IMAGE-'].update(visible=True)
start_time = time.time()
elif event == sg.TIMEOUT_EVENT:
plt.close('all') # close all plots
window['-IMAGE-'].update() # clears the image
draw_figure(window['-IMAGE-'], your_matplotlib_code())
seconds_elapsed = int(time.time() - start_time)
fps = counter/seconds_elapsed if seconds_elapsed != 0 else 1.0
window['-STATS-'].update(f'Frame {counter} Write Time {delta} FPS = {fps:2.2} seconds = {seconds_elapsed}')
counter += 1
delta = sg.timer_stop()
window.close()
if __name__ == '__main__':
main()