Help with scope issue in Matlab
Afficher commentaires plus anciens
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
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
le 17 Sep 2016
Modifié(e) : Brendan Zotto
le 17 Sep 2016
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
le 17 Sep 2016
Modifié(e) : Brendan Zotto
le 17 Sep 2016
Star Strider
le 17 Sep 2016
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
le 17 Sep 2016
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
le 17 Sep 2016
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
le 18 Sep 2016
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
le 18 Sep 2016
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.
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
En savoir plus sur Get Started with Statistics and Machine Learning Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!