You are on page 1of 3

Lab 2b : Mel-Filter Cepstral Coefficients (MFCCs)

Aim: Obtain Mel filterbank (triangular) and find out the MFCCs for the sample speech signal.

MATLAB Script :
%% MFCC coefficients

clc; clear; close all;


Q=10; % no. of filters in filterbank
fhz = [0 8000]; % end frequencies
fmel=2595*log10(1+fhz/700); % Hz-to-Mel conversion
fmel1 = linspace(0,fmel(2),Q+2); % linearly spaced center frequencies in
mel-scale
fhz1 = 700*(10.^(fmel1/2595)-1); % Mel-to-Hz conversion

NFFT=512; % no. of FFT bins


fs=16000; % sampling frequency
f=linspace(0,fs/2,NFFT/2+1); % mapping of frequency bin index to
frequency in Hz.
%% Compute boarder point for triangle filters

B=zeros(Q,NFFT/2+1); % Preallocate One sided filters


kb=(NFFT/fs)*fhz1; % border points of the filters

for l=1:Q % For every filter of filterbank


y=zeros(1,NFFT/2+1); % Preallocate the filter vector
k=1; % Initial value of dft bin index
for x=0:1:max(kb); % Running (free) variable for filter design

% slope and intercepts


m1=1/(kb(l+1)-kb(l));
m2=-1/(kb(l+2)-kb(l+1));

c1=-kb(l)*m1;
c2 = -kb(l+2)*m2;

% triangle design for filters


if (x<=kb(l))
y(k)=0;
elseif ((x>kb(l)) && (x<=kb(l+1)))
y(k)=m1*x+c1;

elseif ((x>kb(l+1))&&(x<=kb(l+2)))
y(k)=m2*x+c2;
else
y(k)=0;
end
k=k+1; % Update the dft bin index
end
B(l,:)=y; % stores all filter op in bank B ; all row vectors of B gives
tringle filter
% C(l,:)=[fliplr(y),y(2:end-1)]; % Two sided filters
plot(f,B(l,:))

hold on;
pause;
axis tight; ylabel('Amplitude'), xlabel('Frequency')
end

%% Compute MFCC
[x, fs]=audioread('sample_male.flac');
win=floor(30*fs/1000); % Default window duration = 20 ms
inc=floor(win/2);
wintype='h';
% Framing and spectral analysis
y=enframe(x,win,inc)'; % Perform framing
NF=size(y,2); % Get number of frames
y1=zeros(1+NFFT/2,NF); % Preallocate the data vector to store the DFT
coefficients
for k=1:NF % For every frame
y_tmp=abs(fft(y(:,k),NFFT)); % Compute Magnitude spectrum
y1(1:end,k)=y_tmp(1:1+NFFT/2); % Take positive spectrum values % NFFT/2 X
NF
end

% Take Log
c=log((B*y1).^2);
Nc=10;
% Take DCT
cep=dct(c);
% mfcc1 = cep(1:Nc,:)'; % Nc x p
figure
plot(cep(:,1))
xlabel('No. of coefficients'); ylabel('Amplitude')

You might also like