Index exceeds the number of array elements (480)

3 views (last 30 days)
Error occurs when attemping to validate the plugin
classdef modDelay < audioPlugin
properties
Depth = 0.004; % Range(0.001,0.007)
Rate = 0.6; % Range(0.1,20)
feedback = 0.5;
end
properties (Access = private)
modDelayLineL = zeros(round(0.01*48000),1);
modDelayLineR = zeros(round(0.01*48000),1);
modIdxL = 1;
modIdxR = 1;
phase = 0;
end
properties (Constant)
end
methods
function out = process(plugin,in)
L = length(in);
out = zeros(L,2);
Fs = getSampleRate(plugin);
T = 1/Fs;
for n = 1:L
modReadIdxL = plugin.modIdxL + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
modReadIdxR = plugin.modIdxR + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
% Left Channel Read Index
if modReadIdxL > round(0.01*Fs)
modReadIdxL = modReadIdxL - round(0.01*Fs);
end
% Right Channel Read Index
if modReadIdxR > round(0.01*Fs)
modReadIdxR = modReadIdxR - round(0.01*Fs);
end
% Increment the phase of control sinusoid
plugin.phase = plugin.phase + 2*pi*plugin.Rate*T;
% Wrap phase around 2*pi
if(plugin.phase > 2*pi)
plugin.phase = plugin.phase - 2*pi;
end
% Write to Output
out(n,1) = plugin.modDelayLineL(modReadIdxL);
out(n,2) = plugin.modDelayLineR(modReadIdxR);
% Write to Delay Lines
% Left Channel Delay
plugin.modDelayLineL(plugin.modIdxL) = in(n,1) +...
plugin.feedback*plugin.modDelayLineL(modReadIdxL);
% Right Channel Delay
plugin.modDelayLineR(plugin.modIdxR) = in(n,2) +...
plugin.modDelayLineR(modReadIdxR);
% Increment Modulation Index Values
plugin.modIdxL = plugin.modIdxL + 1; % Left Channel
plugin.modIdxR = plugin.modIdxL + 1; % Right Channel
% Wrap Index Values Around to Create a Circular Buffer
% Left Channel
if plugin.modIdxL > round(0.01*Fs)
plugin.modIdxL = 1;
end
% Right Channel
if plugin.modIdxR > round(0.01*Fs)
plugin.modIdxR = 1;
end
end
function reset(plugin)
plugin.modDelayLineL = zeros(round(0.01*48000),1);
plugin.modDelayLineR = zeros(round(0.01*48000),1);
plugin.modIdxL = 1;
plugin.modIdxR = 1;
plugin.phase = 0;
end
end
end
end
  4 Comments
jibrahim
jibrahim on 1 Dec 2020
Hi Nathanael,
validateAudioPlugin will run a testbench against your plugin. You can use the keeptestbench option to preserve the testbench m file:
validateAudioPlugin -keeptestbench modDelay
To reproduce the error, just run testbench_modDelay. You can put breakpoints in that file to hopefully give you insight into what is going wrong.
Nathanael Channings
Nathanael Channings on 1 Dec 2020
Thank you for your suggestion jibrahim, the problem seems to be caused by calling the...
getSampleRate(plugin) % Function
Where the sample rate seems to change from 48000 to 96000 inexplicably, defining the sample rate as a constant 48000 seems to have solved the issue so far.

Sign in to comment.

Accepted Answer

jibrahim
jibrahim on 1 Dec 2020
Hi Nathanael,
I took a look at the code. I saw two issues:
a. There seems to be a misplaced end that was making the reset method unreachable
b. In reset, instead of 48e3, I use getSampleRate to get the sample rate.
Note that reset is called automatically when the sample rate changes
With these changes, it seems validateAudioPlugin works.
classdef modDelay < audioPlugin
properties
Depth = 0.004; % Range(0.001,0.007)
Rate = 0.6; % Range(0.1,20)
feedback = 0.5;
end
properties (Access = private)
modDelayLineL = zeros(round(0.01*48000),1);
modDelayLineR = zeros(round(0.01*48000),1);
modIdxL = 1;
modIdxR = 1;
phase = 0;
end
properties (Constant)
end
methods
function out = process(plugin,in)
L = length(in);
out = zeros(L,2);
Fs = getSampleRate(plugin);
T = 1/Fs;
for n = 1:L
modReadIdxL = plugin.modIdxL + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
modReadIdxR = plugin.modIdxR + round(abs(round(plugin.Depth*Fs)*...
sin(plugin.phase)));
% Left Channel Read Index
if modReadIdxL > round(0.01*Fs)
modReadIdxL = modReadIdxL - round(0.01*Fs);
end
% Right Channel Read Index
if modReadIdxR > round(0.01*Fs)
modReadIdxR = modReadIdxR - round(0.01*Fs);
end
% Increment the phase of control sinusoid
plugin.phase = plugin.phase + 2*pi*plugin.Rate*T;
% Wrap phase around 2*pi
if(plugin.phase > 2*pi)
plugin.phase = plugin.phase - 2*pi;
end
% Write to Output
out(n,1) = plugin.modDelayLineL(modReadIdxL);
out(n,2) = plugin.modDelayLineR(modReadIdxR);
% Write to Delay Lines
% Left Channel Delay
plugin.modDelayLineL(plugin.modIdxL) = in(n,1) +...
plugin.feedback*plugin.modDelayLineL(modReadIdxL);
% Right Channel Delay
plugin.modDelayLineR(plugin.modIdxR) = in(n,2) +...
plugin.modDelayLineR(modReadIdxR);
% Increment Modulation Index Values
plugin.modIdxL = plugin.modIdxL + 1; % Left Channel
plugin.modIdxR = plugin.modIdxL + 1; % Right Channel
% Wrap Index Values Around to Create a Circular Buffer
% Left Channel
if plugin.modIdxL > round(0.01*Fs)
plugin.modIdxL = 1;
end
% Right Channel
if plugin.modIdxR > round(0.01*Fs)
plugin.modIdxR = 1;
end
end
end
function reset(plugin)
Fs = getSampleRate(plugin);
plugin.modDelayLineL = zeros(round(0.01*Fs),1);
plugin.modDelayLineR = zeros(round(0.01*Fs),1);
plugin.modIdxL = 1;
plugin.modIdxR = 1;
plugin.phase = 0;
end
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!