MATLAB Answers

0

Why do I get an array out of bounds error

Niels Krog さんによって質問されました 2019 年 6 月 9 日
最新アクティビティ dpb
さんによって コメントされました 2019 年 6 月 9 日
dpb
さんの 回答が採用されました
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
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 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))));

サインイン to comment.

1 件の回答

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 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
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...

サインイン to comment.



Translated by