Gammatone滤波器组归一化代码
时间: 2023-06-14 22:06:49 浏览: 158
cochleagram.tar.gz_Gammatone_cochleagram_cochleagram.tar_gammato
以下是Python代码实现Gammatone滤波器组归一化:
```python
import numpy as np
def gammatone_filterbank(fs, num_bands, freq_range):
"""Calculate the center frequencies of the gammatone filterbank.
Args:
fs (float): The sampling rate.
num_bands (int): The number of filterbanks.
freq_range (tuple): The frequency range of the filterbank.
Returns:
array: The center frequencies of the filterbank.
"""
# Get the lower and upper frequency.
f_low, f_high = freq_range
# Calculate the equivalent rectangular bandwidth (ERB) per band.
erb_per_band = (f_high - f_low) / num_bands
# Calculate the center frequencies of the filterbank using the ERB scale.
erb = lambda x: 24.7 * (4.37 * x / 1000 + 1)
center_freqs_erb = np.arange(1, num_bands + 1) * erb(erb_per_band)
center_freqs_hz = (center_freqs_erb / 0.00437 - 1.0) * 1000.0
return center_freqs_hz
def gammatone_filter(fs, freq, b=1.019, q=1.0):
"""Calculate the coefficients of a gammatone filter.
Args:
fs (float): The sampling rate.
freq (float): The center frequency of the filter.
b (float): The bandwidth of the filter.
q (float): The Q factor of the filter.
Returns:
tuple: The coefficients of the filter.
"""
# Calculate the filter coefficients.
t = 1.0 / fs
a0 = 1.0
a1 = -2.0 * np.cos(2.0 * np.pi * freq * t) / np.exp(b * t)
a2 = np.exp(-2.0 * b * t)
b0 = q * np.exp(b / 2.0) * t ** (q - 1.0)
b1 = 0.0
b2 = -q * np.exp(-b / 2.0) * t ** (q - 1.0)
return a0, a1, a2, b0, b1, b2
def gammatone_filterbank_matrix(fs, num_bands, freq_range, b=1.019, q=1.0):
"""Calculate the filterbank matrix for the gammatone filterbank.
Args:
fs (float): The sampling rate.
num_bands (int): The number of filterbanks.
freq_range (tuple): The frequency range of the filterbank.
b (float): The bandwidth of the filter.
q (float): The Q factor of the filter.
Returns:
array: The filterbank matrix.
"""
# Calculate the center frequencies of the filterbank.
center_freqs = gammatone_filterbank(fs, num_bands, freq_range)
# Calculate the filter coefficients for each band.
filters = [gammatone_filter(fs, freq, b, q) for freq in center_freqs]
# Calculate the filterbank matrix.
filterbank = np.zeros((num_bands, int(fs / 2)))
for i, (a0, a1, a2, b0, b1, b2) in enumerate(filters):
h = np.zeros(int(fs / 2))
for n in range(int(fs / 2)):
t = n / fs
h[n] = b0 * t ** (q - 1.0) * np.exp(b * t) * np.cos(2.0 * np.pi * center_freqs[i] * t)
h /= np.linalg.norm(h)
filterbank[i, :] = np.fft.fft(h)
return filterbank
```
其中,`gammatone_filterbank()`函数用于计算Gammatone滤波器组的中心频率,`gammatone_filter()`函数用于计算单个Gammatone滤波器的系数,`gammatone_filterbank_matrix()`函数用于计算Gammatone滤波器组的滤波矩阵。在计算滤波矩阵时,我们对每个滤波器的频率响应进行了归一化,以避免能量放大或缩小的问题。
阅读全文