|
| 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 | +""" |
1 | 10 | import matplotlib.pyplot as plt
|
2 | 11 | import numpy as np
|
3 | 12 |
|
4 | 13 |
|
5 | 14 | np.random.seed(0)
|
6 | 15 |
|
7 |
| -dt = 0.01 |
8 |
| -Fs = 1/dt |
| 16 | +dt = 0.01 # sampling interval |
| 17 | +Fs = 1/dt # sampling frequency |
9 | 18 | t = np.arange(0, 10, dt)
|
| 19 | + |
| 20 | +# generate noise: |
10 | 21 | nse = np.random.randn(len(t))
|
11 | 22 | r = np.exp(-t/0.05)
|
12 |
| - |
13 | 23 | cnse = np.convolve(nse, r)*dt
|
14 | 24 | 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) |
19 | 25 |
|
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 |
22 | 27 |
|
23 |
| -plt.subplot(3, 2, 4) |
24 |
| -plt.magnitude_spectrum(s, Fs=Fs, scale='dB') |
| 28 | +fig, axx = plt.subplots(3, 2) |
25 | 29 |
|
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)$") |
28 | 34 |
|
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) |
31 | 41 |
|
| 42 | +fig.tight_layout() |
32 | 43 | plt.show()
|
0 commit comments