-
Notifications
You must be signed in to change notification settings - Fork 17
/
plot.py
93 lines (64 loc) · 2.45 KB
/
plot.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
import matplotlib
import matplotlib.pyplot as plot
import numpy as np
def spectrogram(frames, framesize, hopsize, samplerate, xlim=None, ylim=None, clim=-120, cmap='inferno'):
def lim():
if xlim is not None:
if isinstance(xlim, (list, tuple)):
plot.xlim(xlim)
else:
plot.xlim(0, xlim)
if ylim is not None:
if isinstance(ylim, (list, tuple)):
plot.ylim(ylim)
else:
plot.ylim(0, ylim)
if clim is not None:
if isinstance(clim, (list, tuple)):
plot.clim(clim)
else:
plot.clim(clim, 0)
with np.errstate(divide='ignore', invalid='ignore'):
data = 20 * np.log10(np.abs(frames))
timestamps = np.arange(frames.shape[0]) * hopsize / samplerate
frequencies = np.arange(frames.shape[1]) * samplerate / framesize
roi = (np.min(timestamps), np.max(timestamps), np.min(frequencies), np.max(frequencies))
colormap = matplotlib.cm.get_cmap(cmap)
colormap.set_bad(colormap(0))
plot.imshow(data.T, extent=roi, cmap=colormap, aspect='auto', interpolation='nearest', origin='lower')
plot.xlabel('s')
plot.ylabel('Hz')
colorbar = plot.colorbar()
colorbar.set_label('dB')
lim()
return plot
def phasogram(frames, framesize, hopsize, samplerate, xlim=None, ylim=None, clim=None, cmap='twilight'):
def lim():
if xlim is not None:
if isinstance(xlim, (list, tuple)):
plot.xlim(xlim)
else:
plot.xlim(0, xlim)
if ylim is not None:
if isinstance(ylim, (list, tuple)):
plot.ylim(ylim)
else:
plot.ylim(0, ylim)
if clim is not None:
if isinstance(clim, (list, tuple)):
plot.clim(clim)
else:
plot.clim(-clim, +clim)
else:
plot.clim(-np.pi, +np.pi)
data = np.angle(frames)
timestamps = np.arange(frames.shape[0]) * hopsize / samplerate
frequencies = np.arange(frames.shape[1]) * samplerate / framesize
roi = (np.min(timestamps), np.max(timestamps), np.min(frequencies), np.max(frequencies))
plot.imshow(data.T, extent=roi, cmap=cmap, aspect='auto', interpolation='nearest', origin='lower')
plot.xlabel('s')
plot.ylabel('Hz')
colorbar = plot.colorbar()
colorbar.set_label('rad')
lim()
return plot