PNCC的python代码
时间: 2023-11-12 15:57:25 浏览: 120
python圣诞树编写实例详解
PNCC是一种语音特征提取方法,其Python代码如下:
```python
import numpy as np
from scipy.fftpack import dct
def pncc(signal, samplerate=16000, pre_emphasis=0.97, frame_size=0.025, frame_stride=0.01, N=512, M=512, L=512, alpha=0.5, R=0.95):
emphasized_signal = np.append(signal[0], signal[1:] - pre_emphasis * signal[:-1])
frames = np.array([emphasized_signal[i:i + int(frame_size * samplerate)] for i in range(0, len(emphasized_signal) - int(frame_size * samplerate), int(frame_stride * samplerate))])
frames *= np.hamming(int(frame_size * samplerate))
NFFT = int(pow(2, np.ceil(np.log2(N))))
magnitude_spectrum = np.absolute(np.fft.rfft(frames, NFFT))
power_spectrum = ((1.0 / NFFT) * ((magnitude_spectrum) ** 2))
filter_bank = np.zeros((M, int(NFFT / 2 + 1)))
for m in range(1, M + 1):
f_m_minus = int(np.floor((m - 1) * N / M))
f_m = int(np.floor(m * N / M))
f_m_plus = int(np.floor((m + 1) * N / M))
for k in range(f_m_minus, f_m):
filter_bank[m - 1, k] = (k - f_m_minus) / (f_m - f_m_minus)
for k in range(f_m, f_m_plus):
filter_bank[m - 1, k] = (f_m_plus - k) / (f_m_plus - f_m)
filter_banks = np.dot(power_spectrum, filter_bank.T)
filter_banks = np.where(filter_banks == 0, np.finfo(float).eps, filter_banks)
filter_banks = 20 * np.log10(filter_banks)
cepstral_coefficents = dct(filter_banks, type=2, axis=1, norm='ortho')[:, :L]
cepstral_coefficents -= (np.mean(cepstral_coefficents, axis=0) + 1e-8)
delta_cepstral_coefficents = librosa.feature.delta(cepstral_coefficents)
delta_delta_cepstral_coefficents = librosa.feature.delta(cepstral_coefficents, order=2)
numerator = np.power(delta_cepstral_coefficents, 2) + alpha * np.power(delta_delta_cepstral_coefficents, 2)
denominator = np.ones_like(numerator) + R * np.power(cepstral_coefficents, 2)
pncc_features = np.divide(numerator, denominator)
return pncc_features
```
阅读全文