-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp10_spectrogram.py
More file actions
41 lines (31 loc) · 1.04 KB
/
sp10_spectrogram.py
File metadata and controls
41 lines (31 loc) · 1.04 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
# Spectrogram, power spectral density
# Demo spectrogram and power spectral density on a frequency chirp
import numpy as np
from matplotlib import pyplot as plt
from scipy import signal
# Generate a chirp signal
time_step = .01
time_vec = np.arange(0, 70, time_step)
# A signal with a small frequency chirp
sig = np.sin(0.5 * np.pi * time_vec * (1 + .1 * time_vec))
plt.figure(figsize=(8, 5))
plt.plot(time_vec, sig)
# Compute and plot the spectrogram
# The spectrum of the signal on consecutive time windows
freqs, times, spectrogram = signal.spectrogram(sig)
plt.figure(figsize=(5, 4))
plt.imshow(spectrogram, aspect='auto', cmap='hot_r', origin='lower')
plt.title('Spectrogram')
plt.ylabel('Frequency band')
plt.xlabel('Time window')
plt.tight_layout()
# Compute and plot the power spectral density (PSD)
# The power of the signal per frequency band
freqs, psd = signal.welch(sig)
plt.figure(figsize=(5, 4))
plt.semilogx(freqs, psd)
plt.title('PSD: power spectral density')
plt.xlabel('Frequency')
plt.ylabel('Power')
plt.tight_layout()
plt.show()