-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
86 lines (75 loc) · 2.51 KB
/
test.py
File metadata and controls
86 lines (75 loc) · 2.51 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
# Copyright (C) 2021 Xiyuan Li
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import chirp
from s import s_transform, inverse_s_transform
def test_s_transform():
"""
Test the S-Transform and its inverse on a quadratic chirp signal.
"""
# Time vector and sampling rate
dt = 0.001
t_end = 3.0
fs = int(1.0 / dt)
t = np.linspace(0, t_end, int(t_end * fs), endpoint=False)
# Generate quadratic chirp signal
x = chirp(t, f0=10.0, t1=t_end, f1=120.0, method='quadratic')
# Compute S-Transform spectrogram
st = s_transform(
x,
sample_rate=fs,
freq_range=(0.0, 500.0),
freq_step=fs / x.size,
downsample=None
)
plt.figure()
plt.imshow(np.abs(st), origin='lower', aspect='auto')
plt.title('Original Spectrogram')
plt.colorbar()
plt.show()
# Inverse S-Transform
x_rec, fft_rec = inverse_s_transform(st)
# Magnitude compensation (real signal, one-sided FFT)
x_rec_comp = x_rec * 2.0
# Plot original vs. recovered
fig, axes = plt.subplots(2, 1, figsize=(8, 6))
axes[0].plot(x)
axes[0].set_title('Original Signal')
axes[1].plot(x_rec_comp.real)
axes[1].set_title('Recovered Signal (magnitude-compensated)')
plt.tight_layout()
plt.show()
# Reconstruction error
plt.figure()
plt.plot(x_rec_comp.real, label='Recovered Signal')
plt.plot(x - x_rec_comp.real, label='Error')
plt.title('Time Series Reconstruction Error')
plt.legend()
plt.show()
# Spectrogram of recovered signal
st_rec = s_transform(
x_rec.real,
sample_rate=fs,
freq_range=(0.0, 500.0),
freq_step=fs / x_rec.size
)
plt.figure()
plt.imshow(np.abs(st_rec), origin='lower', aspect='auto')
plt.title('Recovered Spectrogram')
plt.colorbar()
plt.show()
if __name__ == '__main__':
test_s_transform()