利用python实现线性预测系数(linear predictive coding coefficients,LPCC)
时间: 2024-05-05 22:22:57 浏览: 182
LPCC(线性预测系数)是一种用于语音信号处理的特征提取方法,用于语音识别、语音合成和语音压缩等应用中。下面是通过Python实现LPCC系数的步骤:
1. 导入所需的库和数据集
```
import numpy as np
from scipy.io import wavfile
from python_speech_features import mfcc
```
2. 读取音频文件并提取MFCC特征
```
(rate,sig) = wavfile.read("audio.wav")
mfcc_feat = mfcc(sig,rate)
```
3. 计算LPCC系数
```
def lpcc(signal, order):
signal = signal.astype(np.float64)
n_samples = len(signal)
signal = signal.reshape(n_samples, 1)
preemph_signal = signal.copy()
preemph_signal[1:] -= preemph_signal[:-1] * 0.97
frame_size = int(np.floor(0.025 * rate))
frame_step = int(np.floor(0.01 * rate))
nf = int(np.floor((n_samples - frame_size) / frame_step)) + 1
frames = np.zeros((frame_size, nf))
for i in range(nf):
frames[:, i] = preemph_signal[i * frame_step:i * frame_step + frame_size, 0]
w = np.hamming(frame_size)
frames = frames * w.reshape(frame_size, 1)
nfft = int(2 ** np.ceil(np.log2(frame_size)))
spectrum = np.abs(np.fft.fft(frames, nfft, axis=0)[:nfft // 2 + 1])
lpc = np.zeros((order + 1, nf))
r = np.zeros((order + 1,))
for i in range(nf):
auto_corr = np.correlate(frames[:, i], frames[:, i], mode='full')
auto_corr = auto_corr[frame_size - 1:]
for j in range(order + 1):
r[j] = auto_corr[:order + 1][j]
lpc[:, i] = np.linalg.solve(toeplitz(r[:-1]), -r[1:])
lpcc_feat = lpc[1:, :].transpose()
return lpcc_feat
```
4. 调用函数并输出LPCC系数
```
lpcc_feat = lpcc(sig, 12)
print(lpcc_feat)
```
注意:上述代码中的order表示LPCC系数的阶数,一般取12。同时,为了能够使用该代码,需要安装python_speech_features库。
阅读全文