8000 Merge pull request #8158 from vollbier/spectrum_demo · matplotlib/matplotlib@a8547fa · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a8547fa

Browse files
authored
Merge pull request #8158 from vollbier/spectrum_demo
[DOC] Fix layout of spectrum_demo.py
2 parents 0a4c97f + f2bc54b commit a8547fa

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed
Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
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
1625

17-
plt.subplot(3, 2, 1)
18-
plt.plot(t, s)
26+
s = 0.1*np.sin(4*np.pi*t) + cnse # the signal
27+
28+
fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(7, 7))
29+
30+
# plot time signal:
31+
axes[0, 0].set_title("Signal")
32+
axes[0, 0].plot(t, s, color='C0')
33+
axes[0, 0].set_xlabel("Time")
34+
axes[0, 0].set_ylabel("Amplitude")
35+
36+
# plot different spectrum types:
37+
axes[1, 0].set_title("Magnitude Spectrum")
38+
axes[1, 0].magnitude_spectrum(s, Fs=Fs, color='C1')
1939

20-
plt.subplot(3, 2, 3)
21-
plt.magnitude_spectrum(s, Fs=Fs)
40+
axes[1, 1].set_title("Log. Magnitude Spectrum")
41+
axes[1, 1].magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
2242

23-
plt.subplot(3, 2, 4)
24-
plt.magnitude_spectrum(s, Fs=Fs, scale='dB')
43+
axes[2, 0].set_title("Phase Spectrum ")
44+
axes[2, 0].phase_spectrum(s, Fs=Fs, color='C2')
2545

26-
plt.subplot(3, 2, 5)
27-
plt.angle_spectrum(s, Fs=Fs)
46+
axes[2, 1].set_title("Angle Spectrum")
47+
axes[2, 1].angle_spectrum(s, Fs=Fs, color='C2')
2848

29-
plt.subplot(3, 2, 6)
30-
plt.phase_spectrum(s, Fs=Fs)
49+
axes[0, 1].remove() # don't display empty ax
3150

51+
fig.tight_layout()
3252
plt.show()

0 commit comments

Comments
 (0)
0