-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwft_spectrogram_generated_signal.py
More file actions
49 lines (39 loc) · 1.47 KB
/
wft_spectrogram_generated_signal.py
File metadata and controls
49 lines (39 loc) · 1.47 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
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal as sgn
# Parameters
fs = 1000 # Sampling frequency (Hz)
duration = 1.0 # Signal duration (seconds)
window_type = 'hann'
window_size = 200 # WFT window size
function_freq = 50
function_type = 'chirp' # Choose 'sine_wave', 'chirp' or 'noisy_sine'
def get_signal():
if function_type == 'sine_wave':
return np.sin(function_freq * 2.0*np.pi*t)
if function_type == 'chirp':
return np.sin(function_freq * 2.0*np.pi*(t**2))
if function_type == 'noisy_sine':
return np.sin(function_freq * 2.0*np.pi*t) + np.random.normal(scale=0.5, size=t.shape)
# Generating the input signal
t = np.linspace(0, duration, int(fs * duration), endpoint=False)
signal = get_signal()
# Compute wft
wft_freqs, wft_times, wft_coeffs = sgn.stft(signal, fs, window_type, window_size)
# PLOTTING
fig, axs = plt.subplots(2, 1, figsize=(12, 12), sharex=False)
# Plot original signal
axs[0].plot(t, signal)
axs[0].set_title("Signal")
axs[0].set_ylabel("Amplitude")
axs[0].grid()
# Plot WFT spectrogram
im1 = axs[1].imshow(np.abs(wft_coeffs), aspect='auto',
extent=[wft_times[0], wft_times[-1], wft_freqs[0], wft_freqs[-1]],
origin='lower', cmap='viridis')
axs[1].set_title("Windowed Fourier Transform (Spectrogram)")
axs[1].set_ylabel("Frequency [Hz]")
#fig.colorbar(im1, ax=axs[1], label='Magnitude')
plt.tight_layout()
fig.subplots_adjust(hspace=0.3)
plt.show()