Why do I get an array out of bounds error

조회 수: 5 (최근 30일)
Niels Krog
Niels Krog 2019년 6월 9일
댓글: dpb 2019년 6월 9일
Hello,
I am attempting to implement a wah-wah effect in the app designer and get an index out of bounds error when running this code:
function out = ApplyWahWah(app, out)
t = [1:length(out)*5] * (1/app.Fs);
t = t(:);
LFO = sin(20 * t)*200;
channel = out;
for n = 1:length(out)
if n-fix(LFO(n)) > 1
out(n) = out(n) + channel(n-fix(LFO(n)));
end
end
end
The error is
Index exceeds the number of array elements (1338204)
And apparently it is because LFO doesn't have n items.
However, as seen in the code, I create LFO with a length, than n does not exceed. Also, this code works fine when I run it in a .m script, but fails when doing it in an app. What on eart is happening? I exported the app as a .m file and it did not work there either, but I have pasted the entire code here: https://pastebin.com/T13j1rDF - The t= [1:length(out)*5] is something I added to try and fix it, but it didn't work. Howver, it should not make any difference.
I really hope you can help me, because I am lost. Thanks a lot :-)
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2019년 6월 9일
Niels - can you copy and paste the full error message to this question? Since LFO is based on t which is based on out, then all three arrays should have the same dimensions. Can you tell us what the dimensions for out are? What is app.Fs set to? You may need to put a breakpoint in your code and then run the app to see what is being assigned to all of these variables (including t) so that you can get idea of whay may be going wrong.
Niels Krog
Niels Krog 2019년 6월 9일
편집: Niels Krog 2019년 6월 9일
This is the whole error message:
Index exceeds the number of array elements (1338204).
Error in APminiproject/Pluck (line 113)
out = app.ApplyWahWah(out);
Error in APminiproject/EButton_2Pushed (line 153)
app.Pluck(app, app.E2);
out is 1338204x1, so is t and LFO. However, n only reaches 1338106.
app.Fs is 44100.
Also, I replicated the same error in the .m file, so it is easier to debug. Thanks for you help
Edit: I just resolved the issue. Thank you for your help. I did the following to avoid negative values in LFO:
out(n) = out(n) + channel(n-fix(abs(LFO(n))));

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

채택된 답변

dpb
dpb 2019년 6월 9일
out(n) = out(n) + channel(n-fix(LFO(n)));
the subscript for channel is n-fix(LFO(n)) where LFO=200*sin(t) so fix(LFO) ranges from [-200, 200](*) and then (n-(-fix(LFO)) -->n+200.
(*) Theoretically, probably never actually makes 200 so is +/-199 instead, but conclusion is the same...you're addressing elements beyond n.
  댓글 수: 2
Niels Krog
Niels Krog 2019년 6월 9일
Ah, I fixed it. I did not take into account, that the values in LFO of course will be negative. Therefore, I did this:
out(n) = out(n) + channel(n-fix(abs(LFO(n))));
This resolved the issue. Thanks for your help :-)
dpb
dpb 2019년 6월 9일
"This resolved the issue. Thanks for your help :-)"_
If resovled, please ACCEPT Answer to indicate to others have satisfactory solution to other if for no other reason...

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by