Help with scope issue in Matlab

Hey I am trying to figure out how to get the follow code to compile in Matlab, I am new to the language, (mainly from C background), and I think I am getting a scope issue in my first for loops. I am getting an undefined variable error. Specifically "Undefined function or variable 'M'. I use M in the nested for loops, and want to multiply it by M2, i dont know how to do this,Can someone help me fix this, and get this function working? Any help is appreciated.
function r = mfcc(s,fs)
m = 100;
n = 256;
l = length(s);
nbFrame = floor((1-n)/m) + 1;
for i =1:n
for j = 1:nbFrame
M(i,j) = s(((j-1)*m)+i);
end
end
h = hamming(n);
M2 = diag(h) * M;
for i = 1:nbFrame
frame(:,i) =fft(M2(:,i));
end
t = n/2;
tmax = 1/ fs;
m = melfb(20,n, fs);
n2 = 1 + floor(n/2);
z = m * abs(frame(1:n2,:)).^2;
r = dct(log(z));

Réponses (2)

Star Strider
Star Strider le 17 Sep 2016
Modifié(e) : Star Strider le 17 Sep 2016
MATLAB is case-sensitive, so m ~= M.
You have several problems, however. You use both ‘m’ and ‘M’ in different contexts in your code.
I would initially reassign ‘M’ as:
M = 100;
but then you need to decide what you want to do with:
M(i,j) = s(((j-1)*m)+i);
because this now creates a matrix ‘M’, using ‘m’ to calculate it.
You later assign:
m = melfb(20,n, fs);
and use it to calculate:
z = m * abs(frame(1:n2,:)).^2;
further confusing the issue. You can see how MATLAB would get confused, much as I would be confused if I didn’t switch contexts, and I’m quite definitely not certain what you’re referring to. Eliminate the ambiguities and all will be well. I would use different variable names and assignments as necessary to avoid confusion and make your code easier to follow. MATLAB does not restrict you to specific variable name lengths (providing you don’t overdo it), so ‘M_constant’, ‘M_matrix’ and ‘final_result’ are all both valid and descriptive. The typing can get tedious, but that’s the worst that can be said about it.
I would also comment-document it, because that will help you understand what you’re doing, will tell other people what you’re doing, and you will need those descriptions when you come back to your code in a while and have to figure out what you were thinking. (Believe me, you won’t remember the details in a few days and especially in a few weeks.)
LATER THAT SAME MINUTE I see some other problems and inefficiencies, but lets get the ‘M,m’ problem sorted first.

12 commentaires

Brendan Zotto
Brendan Zotto le 17 Sep 2016
Modifié(e) : Brendan Zotto le 17 Sep 2016
Hey again. I always appreciate your help. Youre right about my documentation, I usually comment after I finish writing my code, bad habit, especially when I ask for help. I changed the names a bit, but I still seems to be getting the same error. Except now M_matrix is undefined, still in the for loops (line 15).
function r = mfcc(s,fs)
m_const = 100;
n = 256;
l = length(s);
nbFrame = floor((1-n)/m_const) + 1;
for i =1:n
for j = 1:nbFrame
*ERROR* M_matrix(i,j) = s(((j-1)*m_const)+i);
end
end
h = hamming(n);
M2 = diag(h) * M_matrix;
for i = 1:nbFrame
frame(:,i) =fft(M2(:,i));
end
t = n/2;
tmax = 1/ fs;
x = melfb(20,n, fs);
n2 = 1 + floor(n/2);
z = x * abs(frame(1:n2,:)).^2;
r = dct(log(z));
Star Strider
Star Strider le 17 Sep 2016
Greetings, mate!
One problem is that ‘nbFrame’ evaluates to -2 (probably because it contains the (1-n) term where n=256), so ‘j’ is empty because of the colon operator increment and termination conventions, and this empty index creates problems for creating ‘M_matrix’.
I would use the debugger or some other method of your choice to step through this to be sure you know where the problems are. It’s not obvious to me what you’re doing (and I can’t find a reference for the melfb function), so I’ll be glad to do what I can to help with a bit of help from you to explain it.
This is the end of my day (23:20 UTC-6, 05:20 UTC), so let’s continue this tomorrow.
Brendan Zotto
Brendan Zotto le 17 Sep 2016
Modifié(e) : Brendan Zotto le 17 Sep 2016
Yeah I noticed that error with the floor value too. Melfb is a program (source code below), that creates a mel-spaced filter bank, useful for extracting power spectral components of the frequency of a signal.
This all culminates towards a speech recognition program. The filter bank works best when a wave is broken up into 'frames' of speech, so it analyzes a bit of the wave at a time. I dont know exactly what number I should be using for the frame length, and I dont know how exactly it correlates with the accuracy of the program.
I dont know if you know anything of this subject, but those are my general problems. I appreciate any help!
function m = melfb(p, n, fs)
% MELFB Determine matrix for a mel-spaced filterbank
%
% Inputs: p number of filters in filterbank
% n length of fft
% fs sample rate in Hz
%
% Outputs: x a (sparse) matrix containing the filterbank amplitudes
% size(x) = [p, 1+floor(n/2)]
%
% Usage: For example, to compute the mel-scale spectrum of a
% colum-vector signal s, with length n and sample rate fs:
%
% f = fft(s);
% m = melfb(p, n, fs);
% n2 = 1 + floor(n/2);
% z = m * abs(f(1:n2)).^2;
%
% z would contain p samples of the desired mel-scale spectrum
%
% To plot filterbanks e.g.:
%
% plot(linspace(0, (12500/2), 129), melfb(20, 256, 12500)'),
% title('Mel-spaced filterbank'), xlabel('Frequency (Hz)');
f0 = 700 / fs;
fn2 = floor(n/2);
lr = log(1 + 0.5/f0) / (p+1);
% convert to fft bin numbers with 0 for DC term
bl = n * (f0 * (exp([0 1 p p+1] * lr) - 1));
b1 = floor(bl(1)) + 1;
b2 = ceil(bl(2));
b3 = floor(bl(3));
b4 = min(fn2, ceil(bl(4))) - 1;
pf = log(1 + (b1:b4)/n/f0) / lr;
fp = floor(pf);
pm = pf - fp;
r = [fp(b2:b4) 1+fp(1:b3)];
c = [b2:b4 1:b3] + 1;
v = 2 * [1-pm(b2:b4) pm(1:b3)];
m = sparse(r, c, v, p, 1+fn2);
Should this be:
nbFrame = floor((n-1)/m_const) + 1;
because if it’s negative, the ‘j’ loop (assuming a +1 step by default) will never execute.
Brendan Zotto
Brendan Zotto le 17 Sep 2016
Im pretty sure it should be, that makes the code compile, but it isnt working properly
Star Strider
Star Strider le 17 Sep 2016
What does ‘isn’t working properly’ mean?
What is it doing that it shouldn’t be, or what isn’t it doing that it should be?
Brendan Zotto
Brendan Zotto le 17 Sep 2016
I need it to recognize specific voices that are given to me. I.e. create a codebook of trained voice samples, and then test the same samples to see if the machine can recognize the voices. http://minhdo.ece.illinois.edu/teaching/speaker_recognition/speaker_recognition.html
Image Analyst
Image Analyst le 18 Sep 2016
OK, but I hope you're not asking us to write or debug a speaker recognition program. That would go way, way beyond the typical short answers we can give here. You'll have to ask shorter, more targeted questions about small code chunks that are small parts of what will probably be thousands and thousands of lines of code in a speaker recognition program.
Brendan Zotto
Brendan Zotto le 18 Sep 2016
Im not haha, I was saying what my problem was, and to be honest its mostly finished, and its not as complicated as you think. Really more like 400-500 lines (but mine isn't exactly working yet). I was really asking about a specific bug, I fixed the bug, Star Strider asked more questions about what I was trying to do, and I answered them. I need to figure out where my math is going wrong, hopefully I'll get it down soon. If not ill ask another question to the board.
Star Strider
Star Strider le 18 Sep 2016
@Brendan Zotto — I was hoping for specifics about what parts or your code aren’t working, the idea being that I might be able to help you find the problem and fix it. My problem is that ‘not working’ doesn’t give me any really useful information, and certainly doesn’t describe what’s wrong with your code.
Brendan Zotto
Brendan Zotto le 18 Sep 2016
Its ok, I couldnt give you specifics, because Im not too sure myself. Ill try to post again when I know more. Thanks though.
Star Strider
Star Strider le 18 Sep 2016
understand. However please understand that in order for me to understand what your code is doing, need to know what you intend it to do, and what the parameters (constants) are. By running it, can understand what it’s doing, so only if it°s not possible for me to put that in the context of what it should be doing will need help to understand it.

Connectez-vous pour commenter.

hello_world
hello_world le 6 Juil 2018

0 votes

Hello,
Any progress on this code so far?
If so, can you share the code?

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by