-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow_functions_plot.py
More file actions
executable file
·57 lines (44 loc) · 1.49 KB
/
window_functions_plot.py
File metadata and controls
executable file
·57 lines (44 loc) · 1.49 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
#! /usr/bin/env python3
from scipy.fft import fft, fftfreq, fftshift
from scipy.signal.windows import blackman, hamming, hann, gaussian
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
# setup figure
fig = plt.figure()
gs0 = gridspec.GridSpec(1, 2, figure=fig)
gs00 = gridspec.GridSpecFromSubplotSpec(4, 1, subplot_spec=gs0[1])
ax0 = fig.add_subplot(gs0[0])
N = 1024
x = np.linspace(0, 1, N)
windows = [hamming(N), blackman(N), hann(N), gaussian(N, N/6)]
names = ["Hamming", "Blackman", "Hann", "Gaussian"]
colors = ["tab:blue", "tab:orange", "tab:green", "tab:red"]
axes_right = []
for i in range(len(windows)):
ax0.plot(x, windows[i], label=names[i] + " Window", color=colors[i])
# pad window
window = np.concatenate((np.zeros((10*N)), windows[i], np.zeros((10*N))))
f = fftshift(fft(window))
# normalize fft to max
f /= np.max(f)
f_x = np.linspace(0,f.size,f.size) - f.size/2
if i > 0:
ax_fft = fig.add_subplot(gs00[i], sharex=axes_right[i-1])
else:
ax_fft = fig.add_subplot(gs00[i])
if i < len(windows)-1:
plt.setp(ax_fft.get_xticklabels(), visible=False)
else:
ax_fft.set_xlabel("bins\n\n(b)")
ax_fft.semilogy(f_x, np.abs(f), label=names[i] + " Window", color=colors[i])
ax_fft.set_xlim(-500, 500)
ax_fft.set_ylim(1e-6, 5)
ax_fft.grid()
ax_fft.legend()
axes_right.append(ax_fft)
ax0.grid()
ax0.legend()
ax0.set_xlabel("n/N\n\n(a)")
ax0.set_ylabel("g(n)")
plt.show()