# Creating Mel triangular filters function

27 views (last 30 days)

Show older comments

Hello, i know there are already plenty functions that create mel filter banks , but i need to create my own function. I found i decent guide http://practicalcryptography.com/miscellaneous/machine-learning/guide-mel-frequency-cepstral-coefficients-mfccs/ but i got stuck in the creation of triangular mel filters . So i need to make them form that equation

where m is the mel scaled frequencies and k is the length of DFT. I tried the code below, but i get warnings that exceded indeces

f_low=300;

f_high=8000;

filt_num=12;

fs=16000;

%%computing band in mel-scale

mel_low=2595*log10(1+(f_low/100));

mel_high=2595*log10(1+(f_high/100));

%%creating the mel-scaled vector

Mel = linspace(mel_low,mel_high,filt_num);

%%computing frequencies of the Mel vector

%FREQ=700*((10.^(Mel/2595))-1);

Freq=mel2hz(Mel);

%%convert frequencies to nearest bins

for i=1:filt_num

f(i) = floor((nfft+1)*Freq(i)/fs);

end

for m =2:length(Mel)+2

for k=1:nfft+1

if (k<f(Mel(m)-1));H(Mel,k)=0;

elseif (k>=f(Mel(m)-1) && k<=(f(Mel(m))));H(Mel,k)=(k-f(Mel(m)-1))/(f(Mel(m))-f(Mel(m)-1));

elseif (k>=f(Mel(m)) && k<=f(Mel(m)+1));H(Mel,k)=(f(Mel(m)+1)-k)/(f(Mel(m)+1)-f(Mel(m)));

elseif (k>f(Mel(m)+1));H(Mel,k)=0;

end

end

end

Please help any advice appreciated, thank in advance.

##### 2 Comments

Suchithra K S
on 23 Mar 2019

Sir when i trying this code it showing the error like this

Subscript indices must either be real positive integers or logicals.

### Answers (2)

Christiaan
on 2 Apr 2015

Dear Manolis,

In your code you have computed an array 'f', where the frequencies are defined. These frequencies are calculated from this code: '' for i=1:filt_num f(i) = floor((nfft+1)*Freq(i)/fs);end '' However you have not specified nfft, you cannot calculate this. If you mean for nfft, the side of your FFT vector, please have a look at this website how NFFT in MATLAB is defined.

Then you wrote down the following in your code: ' f(Mel(m)) '

However when f has only 12 variables and Mel(1) is equal to a value higher then 12, MATLAB will give an error.

What may can help you is to define a formula to calculate your H value:

function H=formula(k,f,m)

if k<f(m-1)

H = 0;

elseif (k>=f(m-1)&&k<=f(m))

H = (k-f(m-1))/(f(m)-f(m-1));

elseif (k>=f(m+1)&&k<=f(m))

H = (f(m+1)-k)/(f(m+1)-f(m));

elseif k>f(m+1)

H = 0;

end

Then you can call the function in the mainfile to use in the loop.

Good luck! Christiaan

##### 2 Comments

Awais Asghar
on 4 Apr 2018

Aula Rizkiyani
on 17 Jul 2017

Hello manolis and christian, i've tried the function that you both gave but still wrong.

hz2mel = @ (hz) (1127*log(1+hz/700)); %Hertz to mel mel2hz = @ (mel) (700*exp(mel/1127)-700); %mel to Hertz f_low=300; f_high=8000; filt_num=12; fs=16000;

%% computing band in mel-scale mel_low=2595*log10(1+(f_low/100)); mel_high=2595*log10(1+(f_high/100));

%% creating the mel-scaled vector Mel = linspace(mel_low,mel_high,filt_num);

%% computing frequencies of the Mel vector Freq=700*((10.^(Mel/2595))-1); Freq=mel2hz(Mel);

Then i give the function that is explained by christian. like this : %% convert frequencies to nearest bins function H=formula(k,f,m) if k<f(m-1) H = 0; elseif (k>=f(m-1)&&k<=f(m)) H = (k-f(m-1))/(f(m)-f(m-1)); elseif (k>=f(m+1)&&k<=f(m)) H = (f(m+1)-k)/(f(m+1)-f(m)); elseif k>f(m+1) H = 0; end

After that I call the variable "Freq" to display on the plot. So the results I get like this.

Can you help me. thank you...

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!