-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_window.py
More file actions
135 lines (106 loc) · 4.26 KB
/
custom_window.py
File metadata and controls
135 lines (106 loc) · 4.26 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Custom Window Functions Example
================================
This example demonstrates how to use custom window functions with the spectrograms library.
You can use windows from NumPy, SciPy, or create your own custom designs.
"""
import numpy as np
import spectrograms as sg
from scipy.signal.windows import tukey
# Generate a test signal: 440 Hz tone
sample_rate = 16000
duration = 1.0
t = np.linspace(0, duration, int(sample_rate * duration), dtype=np.float64)
signal = np.sin(2 * np.pi * 440 * t)
n_fft = 512
hop_size = 256
print("=" * 70)
print("Custom Window Functions Example")
print("=" * 70)
# Example 1: Using NumPy windows
print("\n1. Using NumPy Blackman Window")
print("-" * 70)
numpy_window = sg.WindowType.custom(np.blackman(n_fft))
stft = sg.StftParams(n_fft=n_fft, hop_size=hop_size, window=numpy_window)
params = sg.SpectrogramParams(stft, sample_rate=sample_rate)
spec = sg.compute_linear_power_spectrogram(signal, params)
print(f"Window: {numpy_window}")
print(f"Spectrogram shape: {spec.data.shape}")
# Example 2: Using SciPy windows (if available)
print("\n2. Using SciPy Tukey Window")
print("-" * 70)
scipy_window = sg.WindowType.custom(tukey(n_fft, alpha=0.5))
stft = sg.StftParams(n_fft=n_fft, hop_size=hop_size, window=scipy_window)
params = sg.SpectrogramParams(stft, sample_rate=sample_rate)
spec = sg.compute_linear_power_spectrogram(signal, params)
print(f"Window: {scipy_window}")
print(f"Spectrogram shape: {spec.data.shape}")
# Example 3: Creating a custom window design
print("\n3. Creating a Custom-Designed Window")
print("-" * 70)
# Create a window that's a product of Hanning and a Gaussian
hann = sg.WindowType.make_hanning(n_fft)
gaussian = np.exp(-0.5 * ((np.arange(n_fft) - n_fft / 2) / (n_fft / 8)) ** 2)
custom_design = hann * gaussian
custom_window = sg.WindowType.custom(custom_design)
stft = sg.StftParams(n_fft=n_fft, hop_size=hop_size, window=custom_window)
params = sg.SpectrogramParams(stft, sample_rate=sample_rate)
spec = sg.compute_linear_power_spectrogram(signal, params)
print(f"Window: {custom_window}")
print(f"Spectrogram shape: {spec.data.shape}")
# Example 4: Using normalization
print("\n4. Using Window Normalization")
print("-" * 70)
# Create a window normalized to unit sum
window_coeffs = sg.WindowType.make_hamming(n_fft)
print(f"Original sum: {window_coeffs.sum():.6f}")
window_sum_norm = sg.WindowType.custom(window_coeffs.copy(), normalize="sum")
print(f"Window with sum normalization: {window_sum_norm}")
window_peak_norm = sg.WindowType.custom(window_coeffs.copy(), normalize="peak")
print(f"Window with peak normalization: {window_peak_norm}")
window_energy_norm = sg.WindowType.custom(window_coeffs.copy(), normalize="energy")
print(f"Window with energy normalization: {window_energy_norm}")
# Example 5: Comparing different windows
print("\n5. Comparing Different Windows")
print("-" * 70)
windows = {
"Rectangular": sg.WindowType.rectangular,
"Hanning": sg.WindowType.hanning,
"Hamming": sg.WindowType.hamming,
"Blackman": sg.WindowType.blackman,
"Kaiser (β=5)": sg.WindowType.kaiser(5.0),
"Custom NumPy Blackman": sg.WindowType.custom(np.blackman(n_fft)),
}
for name, window in windows.items():
stft = sg.StftParams(n_fft=n_fft, hop_size=hop_size, window=window)
params = sg.SpectrogramParams(stft, sample_rate=sample_rate)
spec = sg.compute_linear_power_spectrogram(signal, params)
print(f"{name:25s} -> Shape: {spec.data.shape}")
# Example 6: Error handling
print("\n6. Error Handling Examples")
print("-" * 70)
# Size mismatch detected early
try:
wrong_size = sg.WindowType.custom(np.blackman(256))
stft = sg.StftParams(n_fft=512, hop_size=256, window=wrong_size)
except Exception as e:
print(f"Size mismatch caught: {e}")
# Empty array
try:
empty_window = sg.WindowType.custom(np.array([]))
except Exception as e:
print(f"Empty array caught: {e}")
# NaN values
try:
nan_window = sg.WindowType.custom(np.array([1.0, np.nan, 1.0]))
except Exception as e:
print(f"NaN values caught: {e}")
# Invalid normalization
try:
invalid_norm = sg.WindowType.custom(np.hamming(512), normalize="invalid")
except Exception as e:
print(f"Invalid normalization caught: {e}")
print("\n" + "=" * 70)
print("All examples completed successfully!")
print("=" * 70)