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

1 view (last 30 days)
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)

Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!