Skip to content

Instantly share code, notes, and snippets.

@vfrico
Last active June 5, 2017 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfrico/50caaf44cb8fae5e09d8ee63d322104d to your computer and use it in GitHub Desktop.
Save vfrico/50caaf44cb8fae5e09d8ee63d322104d to your computer and use it in GitHub Desktop.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parametrizador MEL cepstrum
% Par�metros:
% Nombre del fichero de entrada: n_fichero
% Longitud de la ventana: l_v
% Desplazamiento: desplaza
% N�mero de bandas: n_bandas
% Filtrado de preenfasis: preen
% Salida gr�fica: dibuja
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [matr_param, n_trozos, matr_dparam, matr_fft, matr_bandas] = ...
mel_param(n_fichero, n_bandas, l_v, desplaza, preen, dibuja)
[x,fs]=audioread(n_fichero);
if (size(x,2) == 2) % estéreo
x = x(:, 1);
end
%descartamos las muestras finales
descarta = ceil(l_v/desplaza)-1;
n_trozos = floor(length(x)/desplaza) -descarta; %trozos sobre los que evaluar el espectro
% funci�n de ventana sobre la que calcular la fft
ventana = hamming(l_v);
% filtrado de preenfasis
if (preen==1)
a = 1;
cero=0.95;
b = [1,-cero];
x_fil = filter(b,a,x);
x=x_fil;
end
Matriz_bandas=[];
Matriz_fft=[];
Matriz_cepstrum=[];
Matriz_dcepstrum=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% C�lculo de las bandas MEL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Frecuencia m�xima representable
fmax=fs/2;
% Valor m�ximo en escala mel
mmax=1127.01048*(log(1+fmax/700));
% Valores equiespaciados en escala mel
f_mels=0:mmax/(n_bandas+1):mmax;
% Valores en Hz equivalentes a los mel calculados
f_frecs=700*(exp(f_mels/1127.01048)-1);
for i=1:n_bandas
f_ini(i)=f_frecs(i);
f_fin(i)=f_frecs(i+2);
end
% muestras de inicio y fin
m_ini=floor(f_ini*(l_v/2)/fmax)+1;
m_fin=floor(f_fin*(l_v/2)/fmax)+1;
% filtros en las bandas mel
filtro=zeros(l_v,n_bandas);
for i=1:n_bandas
filtro(m_ini(i):m_fin(i),i)=bartlett(m_fin(i)-m_ini(i)+1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Bucle principal para el c�lculo de los par�metros MEL cepstrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:n_trozos
trozo=x((i-1)*desplaza+1 : (i-1)*desplaza+l_v); % Corte del trozo
trozo_e = trozo.*ventana; % Enventanado
TROZO_E = fft(trozo_e); % fft
TROZO_E2= (abs(TROZO_E)).^2; % abs()^2
% Rellenamos una matriz con todos los valores
Matriz_fft(:,i)=TROZO_E2;
% Filtrado con las bandas MEL
for j=1:n_bandas
banda(j)=sum(filtro(:,j).*TROZO_E2);
end
Matriz_bandas(:,i)=banda'; % Energ�a en bandas
Matriz_cepstrum(:,i)=dct(log(banda))'; % DCT
end
a=1;
b=[2,1,0,-1,-2];
for(i=1:n_bandas)
Matriz_dcepstrum(i,:)=filter(b,a,Matriz_cepstrum(i,:));
end
matr_param = Matriz_cepstrum';
matr_dparam = Matriz_dcepstrum;
matr_fft = Matriz_fft;
matr_bandas = Matriz_bandas;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Representaci�n gr�fica
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (dibuja==1)
figure;
plot(filtro);
title('Filtros para MEL cepstrum');
xlabel('Muestras');
ylabel('Amplitud');
figure;
surf(log(Matriz_bandas));
title('Energía en bandas');
xlabel('Trama');
ylabel('Banda');
zlabel('Amplitud');
figure;
plot(Matriz_cepstrum');
title('Parámetros MEL cepstrum');
xlabel('Trama');
ylabel('Valor del Cepstrum');
grid;
figure;
surf(log(Matriz_fft(1:l_v/2+1,:)));
title('Espectrograma');
xlabel('Trama');
ylabel('Banda en frecuencia');
zlabel('Amplitud');
shading interp;
figure;
plot3(Matriz_cepstrum(1,:), Matriz_cepstrum(2,:),Matriz_cepstrum(3,:), 'marker', '*', 'color', 'r');
title('Parámetros MEL cesptrum de las observaciones');
xlabel('Primer Componente');
ylabel('Segundo Componente');
zlabel('Tercer Componente');
grid;
figure;
title('Parámetros Delta cepstrum');
plot(Matriz_dcepstrum');
xlabel('Trama');
ylabel('Valor del Delta Cepstrum');
grid;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment