Index exceeds the number of array elements When Creating a Ping Pong Delay Algorithm

조회 수: 3 (최근 30일)
Hello, I'm following an example from a Will Pirkle book to create a Ping Pong Delay, when indexing from the first part of the if statement there are no errors, however when indexing from the else if statement I get the error from the title
Any help would be greatly appreciated, cheers
% Initialise the Script
clear all
clc
close all
% Declare Option Variable
algorithm = 2; % 1 = straight
% 2 = Ping Pong
% Issue does not Occur on 'algorithm 1'
% Read in Audio for Simulation
Fs = 48000; % Sampling Rate
L = 10; % Length of Simulation (Seconds)
N = L*Fs; % Length of Simulation (Samples)
x = audioread('Drum_Groove.wav'); % Read in Audio File
% Declare Time Delay Variables
tDelayL = 0.208; % Time Delay for the Left Channel (Seconds)
tDelayR = 0.3; % Time Delay for the Right Channel (Seconds)
sDelayL = round(tDelayL*Fs); % Time Delay for the Left Channel (Samples)
sDelayR = round(tDelayR*Fs); % Time Delay for the Right Channel (Samples)
delayLineL = zeros(sDelayL,1); % Storage Array for the Left Channel Delay Line
delayLineR = zeros(sDelayR,1); % Storage Array for the Right Channel Delay Line
idxL = 1; % Vector to Increment DDL(Left)
idxR = 1; % Vector to Increment DDL(Right)
% Declare User Control Parameters
DryL = 1; % Amplitude Value of the Left Channel Dry Signal
DryR = 1; % Amplitude Value of the Right Channel Dry Signal
WetL = 0.5; % Amplitude Value of the Left Channel Wet Signal
WetR = 0.5; % Amplitude Value of the Right Channel Wet Signal
Fbl = 0.3; % Feedback Value of the Left Channel Wet Signal
Fbr = 0.4; % Feedback Value of the Right Channel Wet Signal
GainL = 1; % Linear Output Gain For Both Left Channel Signals
GainR = 1; % Linear Output Gain For Both Right Channel Signals
GainDBL = 10^GainL/20; % Output Gain in Decibels for the Left Channel
GainDBR = 10^GainR/20; % Output Gain in Decibels for the Right Channel
% Create Output Storage Array
yL = zeros(N,1);
yR = zeros(N,1);
% Create Main Time Loop to Iterate Through
for n = 1:N
% Define the Output
yL(n,1) = GainDBL*(DryL*x(n,1) + WetL*delayLineL(idxL));
yR(n,2) = GainDBR*(DryR*x(n,2) + WetR*delayLineR(idxR));
% Write Samples
if algorithm == 1
delayLineL(idxL) = x(n,1) + Fbl*delayLineL(idxL);
delayLineR(idxR) = x(n,2) + Fbr*delayLineR(idxR);
else if algorithm == 2
delayLineL(idxR) = x(n,1) + Fbl*delayLineL(idxR);
delayLineR(idxL) = x(n,2) + Fbr*delayLineR(idxL);
end
end
% Increment the Index Value
idxL = idxL+1;
idxR = idxR+1;
% Create a Circular Buffer By Wrapping the Index Value
if idxL >= sDelayL
idxL = 1;
end
if idxR >= sDelayR
idxR = 1;
end
end
plot(yL + 0.15)
hold on
plot(yR - 0.15)
hold off
title('Stereo Delay');
legend('Left Channel','Right Channel')
audiowrite('Drum_Groove_Delay.wav',yL+yR,Fs)

답변 (1개)

Raag
Raag 2025년 6월 24일
Hi Nathanael,
Based on your description and the provided code, the error you are encountering is caused by a mismatch between the index values and the size of the delay line arrays when using the Ping Pong Delay algorithm.
In the ‘algorithm == 2’ block, the code attempts to access ‘delayLineL(idxR)’ and ‘delayLineR(idxL)’. However, the delay lines ‘delayLineL’ and ‘delayLineR’ are initialized with different lengths:
Since ‘idxR’ can exceed the length of ‘delayLineL’, and ‘idxL’ can exceed the length of ‘delayLineR’, this results in an out-of-bounds indexing error.
To resolve the issue, you should avoid cross-indexing the delay lines. Instead, use the correct index for each delay line while still achieving the ping-pong effect by feeding the delayed signal from one channel into the other.
Replace the ‘algorithm == 2’ block with the following:
else if algorithm == 2
delayLineL(idxL) = x(n,1) + Fbl * delayLineR(idxR);
delayLineR(idxR) = x(n,2) + Fbr * delayLineL(idxL);
end
After following it the output will look like this:
This ensures that:
  • delayLineL’ is indexed using ‘idxL (its own index)
  • delayLineR’ is indexed using ‘idxR (its own index)
  • The ping-pong effect is preserved by feeding the delayed signal from one channel into the other.
For more information, you may find the following MATLAB documentation helpful:

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by