使用python绘制短时傅里叶变换(STFT)频谱图(时频像)

使用python绘制短时傅里叶变换(STFT)频谱图(时频像)

使用scipy库函数计算stft。
参考https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.stft.html#scipy.signal.stft

import scipy.signal as signal
def stft(x, **params):
    '''
    :param x: 输入信号
    :param params: {fs:采样频率;
                    window:窗。默认为汉明窗;
                    nperseg: 每个段的长度,默认为256,
                    noverlap:重叠的点数。指定值时需要满足COLA约束。默认是窗长的一半,
                    nfft:fft长度,
                    detrend:(str、function或False)指定如何去趋势,默认为Flase,不去趋势。
                    return_onesided:默认为True,返回单边谱。
                    boundary:默认在时间序列两端添加0
                    padded:是否对时间序列进行填充0(当长度不够的时候),
                    axis:可以不必关心这个参数}
    :return: f:采样频率数组;t:段时间数组;Zxx:STFT结果
    '''
    f, t, zxx = signal.stft(x, **params) 
    return f, t, zxx

其中可选的窗函数有boxcar, triang, blackman, hamming, hann,bartlett, flattop, parzen, bohman, blackmanharris,nuttall, barthann, kaiser(needs beta),gaussian (needs standard deviation), general_gaussian (needs power, width), slepian (needs width),dpss (needs normalized half-bandwidth), chebwin (needs attenuation),exponential (needs decay scale), tukey (needs taper fraction)

import matplotlib.pyplot as plt
def stft_specgram(x, picname=None, **params):    #picname是给图像的名字,为了保存图像
    f, t, zxx = stft(x, **params)
    plt.pcolormesh(t, f, np.abs(zxx))
    plt.colorbar()
    plt.title('STFT Magnitude')
    plt.ylabel('Frequency [Hz]')
    plt.xlabel('Time [sec]')
    plt.tight_layout()
    if picname is not None:
        plt.savefig('..\\picture\\' + str(picname) + '.jpg')       #保存图像
    plt.clf()      #清除画布
    return t, f, zxx
  • 12
    点赞
  • 162
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值