220430-三种短时傅立叶变换STFT的绘制及数据提取

  • Figure

在这里插入图片描述

  • Code
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal
# - [How to plot Spectrogram using STFT in python?](https://stackoverflow.com/questions/43109708/how-to-plot-spectrogram-using-stft-in-python/48205933#48205933)

sampling_rate = 8000.0
fft_size = 1024
step = fft_size/16
time = 2
t = np.arange(0, time, 1/sampling_rate)
sweep = signal.chirp(t, f0=100, t1=time, f1=0.8*sampling_rate/2, method='logarithmic')

fig, ax = plt.subplots(1,3, figsize=(12, 3))
ax = ax.ravel()

# Figure 1
[spectrum, freqs, t, im] = ax[0].specgram(sweep, fft_size, sampling_rate, noverlap=1024-step, cmap='jet')
ax[0].set_xlabel('Time (s)')
ax[0].set_ylabel('Freq. (Hz)')
ax[0].set_title('By plt.specgram()')

# Figure 2
# plt.imshow(im.get_array(), cmap='jet', aspect='auto')
ax[1].pcolormesh(t, freqs[::-1], im.get_array(), cmap='jet', shading='gouraud')
ax[1].set_xlabel('Time (s)')
ax[1].set_ylabel('Freq. (Hz)')
ax[1].set_title('By im.get_array()')

# Figure 3
ax[2].pcolormesh(t, freqs, np.log(spectrum), cmap='jet', shading='gouraud')
ax[2].set_xlabel('Time (s)')
ax[2].set_ylabel('Freq. (Hz)')
ax[2].set_title('By np.log(spectrum)')

plt.tight_layout()
plt.savefig('output.png', dpi=300)

# Compare values
print(im.get_array()[:4, :4])
print('\n')
print(np.log(spectrum)[:4, :4])
  • Comparison
[[-204.33209898 -219.84393176 -214.14525629 -203.59444001]
 [-201.31048748 -216.82498615 -210.87029865 -200.53882072]
 [-201.27672382 -216.79880707 -210.16099174 -200.40558268]
 [-201.22097657 -216.75575367 -209.18803361 -200.19223749]]
 
[[-22.84165273 -20.81034315 -21.35552053 -27.64431125]
 [-21.8274879  -20.04740739 -20.59310002 -24.67213702]
 [-21.15707948 -19.84181708 -20.38858459 -23.23503521]
 [-20.4209166  -19.50932131 -20.05665799 -22.21228083]]
  • STFT vs. CWT

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal
# - [How to plot Spectrogram using STFT in python?](https://stackoverflow.com/questions/43109708/how-to-plot-spectrogram-using-stft-in-python/48205933#48205933)

fs = 1024
fft_size = 1024
step = fft_size/16
time=2
tt = np.arange(0, time, 1/fs) # 
# sweep = signal.chirp(t, f0=100, t1=time, f1=0.8*sampling_rate/2, method='logarithmic')
sweep = signal.chirp(tt, f0=30, t1=time, f1=500, method='quadratic')
print(len(sweep))

# fig, ax = plt.subplots(1,3,figsize=(24,6))
fig = plt.figure(figsize=(12,6))

# ax[0]
ax1 = plt.subplot(211)
ax1.plot(sweep)
ax1.set_title('Raw signal')

ax2 = plt.subplot(234)
[spectrum, freqs, t, im] = ax2.specgram(sweep, Fs=fs, cmap='jet')
ax2.set_xlim(0,2)
ax2.set_title('By specgram')

ax3 = plt.subplot(235)
f1, t1, Zxx = signal.stft(sweep, fs=fs)
ax3.pcolormesh(t1, f1, np.log(np.abs(Zxx)), shading='gouraud', cmap='jet') # vmin=0,  vmax=np.max(sweep),
ax3.set_title('By STFT')

ax4 = plt.subplot(236)
import pywt
wavename = 'cmor3-3'
totalscal = 256
fc = pywt.central_frequency(wavename)
cparam = 2 * fc * totalscal
scales = cparam / np.arange(totalscal, 1, -1)
[cwtmatr, frequencies] = pywt.cwt(sweep, scales, wavename, 1.0 / fs)
ax4.pcolormesh(tt, frequencies, np.abs(cwtmatr), shading='gouraud', cmap='jet') 
ax4.set_title('By CWT')
ax4.set_xlim(0,2)

plt.tight_layout()
《MATLAB实现语音时频分布:STFT、WVD与小波变换》是一份详尽的实践指南,它将引导你完成对一段语音信号的时频分析,通过具体的MATLAB代码来展示如何操作。在这份资料中,首先会使用`wavread`函数读取语音文件并提取信号及采样频率。接着,你将学习到如何应用STFT、WVD和小波变换这三种不同的时频分析方法对信号进行深入研究。 参考资源链接:[MATLAB实现语音时频分布:STFT、WVD与小波变换](https://wenku.csdn.net/doc/32m86044zy) 对于STFT,它将信号分段并计算每段的傅立叶变换,以揭示信号随时间变化的频率特性。在MATLAB中,可以使用`spectrogram`或`tfrstft`函数来实现STFT,通过调整nfft参数来平衡时间和频率分辨率。STFT时频图谱的特点是它能展示信号在不同时间点的频谱分布,对于分析具有稳定频率成分的信号非常有效。 Wigner-Ville分布是一种高分辨率的时频分析方法,它能同时展示信号的时间和频率特性。MATLAB中的`tfrwv`函数可用于计算WVD。WVD的特点是它能够提供更精细的时间-频率表示,但同时它也很容易受到交叉项的干扰,适用于分析具有明确时间和频率特性的信号。 小波变换是一种时间-尺度分析方法,它使用小波基函数来分析信号。在MATLAB中,`cwt`函数可以实现连续小波变换。小波变换的特点是其时频分辨率可以随频率变化而自适应调整,非常适合分析具有瞬态或非平稳特性的信号。 以下是一个简单的MATLAB代码示例,展示了如何对一个语音信号进行STFT和WVD的时频分析: ```matlab % 读取语音文件 [signal, Fs] = wavread('T01.wav'); % STFT分析 nfft = 512; [stft_result, f_stft, t_stft] = spectrogram(signal, ones(1, nfft), [], nfft, 1/Fs); % WVD分析 wvd_result = tfrwv(signal, Fs); % 绘制STFT时频图谱 surf(t_stft, f_stft, abs(stft_result)); title('STFT 时频图谱'); xlabel('时间'); ylabel('频率'); zlabel('幅度'); % 绘制WVD时频图谱 surf(t_stft, f_stft, abs(wvd_result)); title('WVD 时频图谱'); xlabel('时间'); ylabel('频率'); zlabel('幅度'); ``` 在学习了如何使用STFT、WVD和小波变换进行时频分析后,你可以更深入地理解语音信号的特性,并根据信号的特点选择最适合的分析方法。本篇资料不仅提供了基础的理论知识和示例代码,还包含了深入的讨论和分析,帮助你全面掌握这些时频分析技术。 参考资源链接:[MATLAB实现语音时频分布:STFT、WVD与小波变换](https://wenku.csdn.net/doc/32m86044zy)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GuokLiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值