Why do I get an array out of bounds error

5 views (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 9 Jun 2019
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 on 9 Jun 2019
Edited: Niels Krog on 9 Jun 2019
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))));

Sign in to comment.

Accepted Answer

dpb
dpb on 9 Jun 2019
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 Comments
Niels Krog
Niels Krog on 9 Jun 2019
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 on 9 Jun 2019
"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...

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!