Counter goes up than down to 1 in recursive function

Hello,
I want my function to return how many 'moving windows' of a particular size can fit in a given length_n-long signal, with a given hop size.
here is my function :
| function N_n = numWindows( N_n, currentSample_n, hopSize_n, length_n, width_n )
% check if we have reached the end of the signal
if currentSample_n + width_n >= length_n
return
else
currentSample_n = currentSample_n + hopSize_n ;
N_n = N_n + 1;
% here we call the same function on the rest of the signal
numWindows( N_n, currentSample_n, hopSize_n, length_n, width_n );
end
return
end|
This code returns systematically N_n = 2; When I entered debug mode I saw that N_n was increasing normally to the correct value, then as the function exited the "if"s one after the other, I saw that N_n was decreasing 1 by 1, just as it had increased in the first place. And the function stopped with N_n = 2.
I tried it with a while loop but it was quickly taking a very long time as the signals started to get long (= big length_n input value).
I would be very grateful to any kind of advice on this problem, thank you very much !
Léo.

댓글 수: 4

AC
AC 2012년 7월 4일
편집: AC 2012년 7월 4일
Hi,
For clarification, what's the nature of the variable currentSample_n? (vector, scalar, integer...)
AC
Léonard Roussel
Léonard Roussel 2012년 7월 4일
편집: Léonard Roussel 2012년 7월 4일
All variables are integers.
Thanks !
Nevermind, got it, see answer below.
Oh yeah I just saw it

댓글을 달려면 로그인하십시오.

 채택된 답변

AC
AC 2012년 7월 4일
Ok, I think I got it: I believe you need to recursively output N_n when you are calling numWindows within the function.
I don't know signal processing, so sorry if it doesn't make sense, but if you try:
N_n=numWindows(1,1,2,100,4);
Then you'll get N_n=49 after modification.
Does it sound about right?

댓글 수: 4

The reason for that, btw, is that in the current form of your function, you only call the "if" once per recursion. So within the recursion it computes all the right things but when you output N_n, you actually only output N_n=2 which corresponds to the first recursion. By the way, if you output currentSample_n as well, you'll have the same problem. So to make things very clear, here's how you may want to change your function:
function [N_n currentSample_n] = numWindows( N_n, currentSample_n, hopSize_n, length_n, width_n )
% check if we have reached the end of the signal
if currentSample_n + width_n >= length_n
return
else
currentSample_n = currentSample_n + hopSize_n ;
N_n = N_n + 1;
% here we call the same function on the rest of the signal
[N_n currentSample_n]=numWindows( N_n, currentSample_n, hopSize_n, length_n, width_n );
end
You should then get:
[N_n c]=numWindows(1,1,2,100,4)
N_n =
49
c =
97
Yes it DOES work, but I actually don't understand what really changed. Anyway, thank you very much !
Sure :) What changes is just the storage of your variable, not the computations. When you compute N_n=N_n+1; you do it within the recursive function (except for the first one), but you never actually store it. It's quite tricky, a bit like time travel :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Léonard Roussel
Léonard Roussel 2012년 7월 4일

0 개 추천

Hum ... not exactly : the command window returns successively all descending values of N_n from 49 to 2 ...
thanks again

댓글 수: 1

Did you try the bit of code I just submitted? It works on my computer. (I think we posted at the same time :))

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by