mfcc特征提取代码matlab
时间: 2023-10-07 14:06:31 浏览: 153
以下是一个基本的MFCC特征提取的MATLAB代码:
% 定义常量
frame_size = 256; % 帧大小
frame_shift = 128; % 帧移
num_filters = 20; % 滤波器数量
num_ceps = 12; % MFCC系数数量
lifter = 22; % lifter系数
% 读取音频文件
[x, fs] = audioread('sample.wav');
% 预加重
pre_emph = [1 -0.97];
x = filter(pre_emph, 1, x);
% 分帧
frames = buffer(x, frame_size, frame_size-frame_shift, 'nodelay');
% 加窗
win = hamming(frame_size);
frames = frames .* repmat(win, 1, size(frames, 2));
% 计算功率谱
pow_frames = abs(fft(frames)).^2;
pow_frames = pow_frames(1:frame_size/2+1, :);
% 计算Mel滤波器组
mel_filters = melFilterBank(fs, num_filters, frame_size);
% 应用Mel滤波器组
mel_pow_frames = mel_filters * pow_frames;
% 取对数
log_mel_pow_frames = log(mel_pow_frames);
% 应用DCT
dct_mel_pow_frames = dct(log_mel_pow_frames);
% 取MFCC系数
mfcc = dct_mel_pow_frames(1:num_ceps, :);
% 应用lifter
lifter_coeffs = (1:lifter)';
lifter_coeffs = sin(pi/lifter*(lifter_coeffs-0.5));
mfcc = mfcc .* repmat(lifter_coeffs, 1, size(mfcc, 2));
% 显示MFCC系数
imagesc(mfcc);
colorbar;
% Mel滤波器组函数
function mel_filters = melFilterBank(fs, num_filters, frame_size)
% 定义频率范围
f_min = 0;
f_max = fs/2;
mel_min = 1125*log(1+f_min/700);
mel_max = 1125*log(1+f_max/700);
% 计算Mel刻度的间隔
mel_interval = (mel_max - mel_min) / (num_filters + 1);
% 将Mel刻度转换为频率
freqs = 700*(exp(mel_min+(0:num_filters+1)*mel_interval/1125)-1);
% 将频率转换为FFT索引
fft_indices = floor((frame_size+1)*freqs/fs);
% 构建滤波器组
mel_filters = zeros(num_filters, frame_size/2+1);
for i = 1:num_filters
left = fft_indices(i);
center = fft_indices(i+1);
right = fft_indices(i+2);
for j = left:center
mel_filters(i, j) = (j-left)/(center-left);
end
for j = center+1:right
mel_filters(i, j) = (right-j)/(right-center);
end
end
end
阅读全文