lpcc特征提取python代码
时间: 2023-07-02 17:03:30 浏览: 279
LPCC(Linear Prediction Cepstral Coefficients)特征提取是一种语音信号处理中常用的特征提取方法,以下是使用Python进行LPCC特征提取的示例代码:
```
import numpy as np
from scipy.signal import lfilter
from scipy.fftpack import fft, ifft
def lpcc(speech_signal, order):
# Pre-emphasis
pre_emphasis = 0.97
emphasized_signal = np.append(speech_signal[0], speech_signal[1:] - pre_emphasis * speech_signal[:-1])
# Framing
frame_size = 0.025
frame_stride = 0.01
sample_rate = 16000
frame_length = int(round(frame_size * sample_rate))
frame_step = int(round(frame_stride * sample_rate))
signal_length = len(emphasized_signal)
num_frames = int(np.ceil(float(np.abs(signal_length - frame_length)) / frame_step))
pad_signal_length = num_frames * frame_step + frame_length
z = np.zeros((pad_signal_length - signal_length))
pad_signal = np.append(emphasized_signal, z)
indices = np.tile(np.arange(0, frame_length), (num_frames, 1)) + np.tile(np.arange(0, num_frames * frame_step, frame_step), (frame_length, 1)).T
frames = pad_signal[indices.astype(np.int32, copy=False)]
# Windowing
frames *= np.hamming(frame_length)
# LPCC Calculation
lpcc_coeffs = np.zeros((num_frames, order))
for i in range(num_frames):
# Autocorrelation
autocorr = lfilter([1], np.array([1, -pre_emphasis]), frames[i])
r = autocorr[:order+1]
# Durbin's algorithm
a = np.zeros(order+1)
k = np.zeros(order)
a[0] = 1
E = r[0]
for m in range(1, order+1):
k[m-1] = -np.sum(a[1:m] * r[m-1:0:-1]) / E
a[m] = k[m-1]
for j in range(1, m):
a[j] += k[m-1] * a[m-j]
E *= 1 - k[m-1] ** 2
# Cepstral coefficients
lpcc_coeffs[i] = fft(np.log(E) + ifft(np.log(np.abs(fft(a, 512)))[:order].real)).real
return lpcc_coeffs
```
其中,`speech_signal`是输入的语音信号,`order`是LPCC系数的阶数。函数返回一个矩阵,每行代表一个帧的LPCC系数。
阅读全文