心电信号S变换Python
时间: 2023-11-05 21:14:24 浏览: 64
以下是心电信号S变换的Python代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# Generate test signal
t = np.linspace(0, 1, 1000)
x = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*20*t) + np.sin(2*np.pi*30*t)
# S-transform
freqs = np.linspace(0, 50, 500)
window = np.hanning(100)
S = np.zeros((len(freqs), len(t)), dtype=np.complex)
for i, f in enumerate(freqs):
kernel = np.exp(-1j*2*np.pi*f*t) * window
S[i, :] = np.convolve(x, kernel, mode='same')
# Plot results
fig, ax = plt.subplots(2, 1, figsize=(8, 6))
ax[0].plot(t, x)
ax[0].set_xlabel('Time (s)')
ax[0].set_ylabel('Amplitude')
im = ax[1].imshow(np.abs(S), aspect='auto', origin='lower',
extent=[t[0], t[-1], freqs[0], freqs[-1]],
cmap='viridis')
ax[1].set_xlabel('Time (s)')
ax[1].set_ylabel('Frequency (Hz)')
cb = fig.colorbar(im, ax=ax[1])
cb.set_label('Magnitude')
plt.show()
```
这个代码生成一个包含三个正弦波的测试信号,并对其进行S变换。结果用热图显示,其中水平轴是时间,垂直轴是频率,颜色表示信号强度。