matlab cwtfilterbank,在Python中为gammatone filterbank实现打开音频信号窗口

我不熟悉编程,尤其是python。我正在尝试用四阶伽玛通滤波器实现听觉模型。我需要把信号分成39个频道。当我使用较小的信号(大约884726位)时,代码运行良好,但我认为缓冲区已满,因此我必须重新启动shell来再次运行代码。尝试使用flush()但没有成功。

所以,我决定用汉宁窗打开信号,但也没成功。为了清楚起见,我需要将一个音频信号分成39个通道,对其进行校正(半波),然后将其传递到第二组四阶滤波器中,这次是10个通道。我试图在信号进入第二组滤波器之前降低信号的采样率。这是通过使用另一个函数生成的系数来实现滤波器组的代码。b的尺寸为39x4096。在def filterbank_application(input, b, verbose = False):

"""

A function to run the input through a bandpass filter bank with parameters defined by the b and a coefficients.

Parameters:

* input (type: array-like matrix of floats) - input signal. (Required)

* b (type: array-like matrix of floats) - the b coefficients of each filter in shape b[numOfFilters][numOfCoeffs]. (Required)

Returns:

* y (type: numpy array of floats) - an array with inner dimensions equal to that of the input and outer dimension equal to

the length of fc (i.e. the number of bandpass filters in the bank) containing the outputs to each filter. The output

signal of the nth filter can be accessed using y[n].

"""

input = np.array(input)

bshape = np.shape(b)

nFilters = bshape[0]

lengthFilter = bshape[1]

shape = (nFilters,) + (np.shape(input))

shape = np.array(shape[0:])

shape[-1] = shape[-1] + lengthFilter -1

y = np.zeros((shape))

for i in range(nFilters):

if(verbose):

sys.stdout.write("\r" + str(int(np.round(100.0*i/nFilters))) + "% complete.")

sys.stdout.flush()

x = np.array(input)

y[i] = signal.fftconvolve(x,b[i])

if(verbose): sys.stdout.write("\n")

return y

samplefreq,input = wavfile.read('sine_sweep.wav')

input = input.transpose()

input = (input[0] + input[1])/2

b_coeff1 = gammatone_filterbank(samplefreq, 39)

Output = filterbank_application(input, b_coeff1)

Rect_Output = half_rectification(Output)

我想把音频分成20秒长的块。如果你能告诉我一个有效的方法来打开我的信号,因为整个音频将比我使用的信号大6倍。提前谢谢。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要安装一些必要的库: ```python !pip install numpy scipy matplotlib soundfile ``` 然后,我们可以编写代码来提取STM特征: ```python import numpy as np import scipy.signal as signal import soundfile as sf import matplotlib.pyplot as plt # 载入音频文件 wav, sr = sf.read('example.wav') # 定义一些参数 nfft = 512 # STFT的窗口大小 hop_size = 256 # STFT的步长 n_filters = 64 # 滤波器的数量 f_low = 80 # 滤波器组的最低频率 f_high = 8000 # 滤波器组的最高频率 # 计算ERB滤波器组的心频率 erb_bands = np.linspace(0, 1, n_filters + 2)[1:-1] erb_cfreqs = (f_high - f_low) * signal.gammatonefrequency(erb_bands) + f_low # 计算ERB滤波器组 f_coef = signal.gammatonefir(erb_cfreqs, sr, 0.25, 1) f_coef = f_coef[:, ::-1] # 计算STFT f, t, spec = signal.stft(wav, fs=sr, window='hann', nperseg=nfft, noverlap=nfft-hop_size, nfft=nfft) # 对每个滤波器应用滤波器组 filtered_spec = np.zeros((n_filters, spec.shape[1]), dtype=np.complex) for i in range(n_filters): filtered_spec[i] = signal.fftconvolve(spec, f_coef[i, :], mode='same') # 计算包络 env = np.abs(filtered_spec) env = signal.savgol_filter(env, 51, 3, axis=1) # 平滑包络 # 计算2D傅里叶变换 stm = np.fft.fft2(env) # 显示STM的频谱图 plt.imshow(np.abs(stm), origin='lower', aspect='auto', cmap='jet', extent=[t[0], t[-1], erb_cfreqs[0], erb_cfreqs[-1]]) plt.xlabel('Time (s)') plt.ylabel('Frequency (Hz)') plt.title('Spectro-temporal modulation (STM) feature') plt.colorbar() plt.show() ``` 在此示例,我们使用了一个示例音频文件`example.wav`,并使用ERB gammatone filterbank滤波器提取了STM特征。我们还使用STFT、包络和2D傅里叶变换来计算STM特征,并使用matplotlib库显示了它的频谱图。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值