語音識別第4講:語音特征參數MFCC https://zhuanlan.zhihu.com/p/88625876/
Speech Processing for Machine Learning: Filter banks, Mel-Frequency Cepstral Coefficients (MFCCs) and What’s In-Between https://haythamfayek.com/2016/04/21/speech-processing-for-machine-learning.html
#梅爾頻率倒譜系數(MFCC)的原理講解及python實現
# https://www.cnblogs.com/LXP-Never/p/10918590.htmlimport numpy
import numpy as np
import scipy.io.wavfile
from scipy.fftpack import dctsample_rate, signal = scipy.io.wavfile.read(r'H:\p227_003.wav')
signal = signal[0:int(3.5 * sample_rate)] # 我們只取前3.5s
pre_emphasis = 0.97#1、預加重 (Pre-Emphasis)
emphasized_signal = numpy.append(signal[0], signal[1:] - pre_emphasis * signal[:-1])frame_size = 0.025
frame_stride = 0.01
overlap=0.015#2、分幀 (Framing)
frame_length, frame_step = frame_size * sample_rate, frame_stride * sample_rate # 從秒轉換為采樣點
signal_length = len(emphasized_signal)
frame_length = int(round(frame_length))
frame_step = int(round(frame_step))
# 確保我們至少有1幀
num_frames = int(numpy.ceil(float(numpy.abs(signal_length - frame_length)) / frame_step)) + 1 #原文這里錯了
pad_signal_length = (num_frames-1) * frame_step + frame_length #原文這里錯了z = numpy.zeros((pad_signal_length - signal_length))
# 填充信號,確保所有幀的采樣數相等,而不從原始信號中截斷任何采樣
pad_signal = numpy.append(emphasized_signal, z)indices = numpy.tile(numpy.arange(0, frame_length), (num_frames, 1)) + numpy.tile(numpy.arange(0, num_frames * frame_step, frame_step), (frame_length, 1)).T
frames = pad_signal[indices.astype(numpy.int32, copy=False)]#3、加窗 (Window)
frames *= numpy.hamming(frame_length)
# frames *= 0.54 - 0.46 * numpy.cos(( numpy.pi * n ) / (frame_length - 1)) # 內部實現NFFT=512
# 二、FFT (Fourier-Transform)
mag_frames = numpy.absolute(numpy.fft.rfft(frames, NFFT)) # fft的幅度(magnitude)# 三、功率譜 (Power Spectrum)
pow_frames = ((1.0 / NFFT) * ((mag_frames) ** 2)) # 功率譜# 四、濾波器組 (Filter Banks)
nfilt = 40
low_freq_mel = 0
high_freq_mel = (2595 * np.log10(1 + (sample_rate / 2) / 700)) # 求最高hz頻率對應的mel頻率
# 我們要做40個濾波器組,為此需要42個點,這意味著在們需要low_freq_mel和high_freq_mel之間線性間隔40個點
mel_points = np.linspace(low_freq_mel, high_freq_mel, nfilt + 2) # 在mel頻率上均分成42個點
hz_points = (700 * (10 ** (mel_points / 2595) - 1)) # 將mel頻率再轉到hz頻率
# bin = sample_rate/2 / NFFT/2=sample_rate/NFFT # 每個頻點的頻率數
# bins = hz_points/bin=hz_points*NFFT/ sample_rate # hz_points對應第幾個fft頻點
bins = np.floor((NFFT + 1) * hz_points / sample_rate)fbank = np.zeros((nfilt, int(np.floor(NFFT / 2 + 1))))
for m in range(1, nfilt + 1):f_m_minus = int(bins[m - 1]) # 左f_m = int(bins[m]) # 中f_m_plus = int(bins[m + 1]) # 右for k in range(f_m_minus, f_m):fbank[m - 1, k] = (k - bins[m - 1]) / (bins[m] - bins[m - 1])for k in range(f_m, f_m_plus):fbank[m - 1, k] = (bins[m + 1] - k) / (bins[m + 1] - bins[m])
filter_banks = np.dot(pow_frames, fbank.T)
filter_banks = np.where(filter_banks == 0, np.finfo(float).eps, filter_banks) # 數值穩定性
filter_banks = 20 * np.log10(filter_banks) # dB#五、梅爾頻率倒譜系數(MFCCs)
num_ceps = 12
mfcc = dct(filter_banks, type=2, axis=1, norm='ortho')[:, 1 : (num_ceps + 1)] # 保持在2-13cep_lifter =22
(nframes, ncoeff) = mfcc.shape
n = numpy.arange(ncoeff)
lift = 1 + (cep_lifter / 2) * numpy.sin(numpy.pi * n / cep_lifter)
mfcc *= lift# 六、均值歸一化(Mean Normalization)
filter_banks -= (numpy.mean(filter_banks, axis=0) + 1e-8)
mfcc -= (numpy.mean(mfcc, axis=0) + 1e-8)