Skip to content

Commit ac09d60

Browse files
committed
Fix layout of spectrum_demo.py
1 parent c2f675d commit ac09d60

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed
Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
1+
"""
2+
========================
3+
Spectrum Representations
4+
========================
5+
6+
The plots show different spectrum representations of a sine signal with
7+
additive noise. A (frequency) spectrum of a discrete-time signal is calculated
8+
by utilizing the fast Fourier transform (FFT).
9+
"""
110
import matplotlib.pyplot as plt
211
import numpy as np
312

413

514
np.random.seed(0)
615

7-
dt = 0.01
8-
Fs = 1/dt
16+
dt = 0.01 # sampling interval
17+
Fs = 1/dt # sampling frequency
918
t = np.arange(0, 10, dt)
19+
20+
# generate noise:
1021
nse = np.random.randn(len(t))
1122
r = np.exp(-t/0.05)
12-
1323
cnse = np.convolve(nse, r)*dt
1424
cnse = cnse[:len(t)]
15-
s = 0.1*np.sin(2*np.pi*t) + cnse
16-
17-
plt.subplot(3, 2, 1)
18-
plt.plot(t, s)
1925

20-
plt.subplot(3, 2, 3)
21-
plt.magnitude_spectrum(s, Fs=Fs)
26+
s = 0.1*np.sin(2*np.pi*t) + cnse # the signal
2227

23-
plt.subplot(3, 2, 4)
24-
plt.magnitude_spectrum(s, Fs=Fs, scale='dB')
28+
fig, axx = plt.subplots(3, 2)
2529

26-
plt.subplot(3, 2, 5)
27-
plt.angle_spectrum(s, Fs=Fs)
30+
# plot time signal:
31+
axx[0, 0].plot(t, s)
32+
axx[0, 0].set_xlabel("Time $t$")
33+
axx[0, 0].set_ylabel("Signal $s(t)$")
2834

29-
plt.subplot(3, 2, 6)
30-
plt.phase_spectrum(s, Fs=Fs)
35+
# plot different spectrum types:
36+
axx[1, 0].magnitude_spectrum(s, Fs=Fs)
37+
axx[2, 0].phase_spectrum(s, Fs=Fs)
38+
axx[0, 1].remove() # don't display empty ax
39+
axx[1, 1].magnitude_spectrum(s, Fs=Fs, scale='dB')
40+
axx[2, 1].angle_spectrum(s, Fs=Fs)
3141

42+
fig.tight_layout()
3243
plt.show()

0 commit comments

Comments
 (0)