MATLAB Answers

0

Audio - Wah wah effect - 'Integer Operands Are Required for Colon Operator When Used as Index'

Asked by Ditte Brodersen on 18 Apr 2019
Latest activity Answered by Geoff Hayes
on 18 Apr 2019
Hi, I'm making a 'wah wah' effect in matlab.
I'm getting the error 'Integer Operands Are Required for Colon Operator When Used as Index' in my while loop
while(length(cutoff_freq) < length(input_sound))
cutoff_freq = [cutoff_freq(max_f:-centre_f:min_f)];
cutoff_freq = [cutoff_freq(min_f:centre_f:max_f)];
end
How can I fix that?
The full code can be seen below:
% Read the audio file
[input_sound, Fs] = audioread('AcGtr.wav');
% Variables
dampFactor = 0.05; %The lower it is, the smaller pass band
wahFreq = 1000; % How many Hz per second are cycles through
min_f = 250; % Minimum cut off frequency
max_f = 5000; % Maximum cut off frequency
centre_f = wahFreq/Fs; % The change in centre frequency per samlple (Hz)
% Triangle wave based on the centre frequency values
cutoff_freq = min_f:centre_f:max_f;
while(length(cutoff_freq) < length(input_sound))
cutoff_freq = [cutoff_freq(max_f:-centre_f:min_f)];
cutoff_freq = [cutoff_freq(min_f:centre_f:max_f)];
end
% Make it fit the audio input size
cutoff_freq = cutoff_freq(1:length(input_sound));
% Filter coefficient
coF = 2*sin((pi*cutoff_freq(1)/Fs));
Q = 2*dampFactor; %Q = quality factor, band width
% Arrays of zeros for the filters, size fits the input sound
highpass = zeros(size(input_sound));
lowpass = zeros(size(input_sound));
bandpass = zeros(size(input_sound));
% Avoid neagtive signals (multiplying by zero errors) the first is con calculated
highpass(1) = input_sound(1);
bandpass(1) = coF*highpass(1);
lowpass(1) = coF*bandpass(1);
% Differenetial equations
for n = 2:length(input_sound)
highpass(n) = input_sound(n) - lowpass(n-1) - Q*bandpass(n-1);
bandpass(n) = coF*highpass(n) + bandpass(n-1);
lowpass(n) = coF*bandpass(n) + lowpass(n-1);
coF = 2*sin((pi*cutoff_freq(n))/Fs);
end
% Normalise audio and play it
norm = bandpass./max(max(abs(bandpass)));
audiowrite('AcGtr.wav', norm, Fs);
sound(norm,Fs)

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 18 Apr 2019

Ditte - what is the intent of this code
while(length(cutoff_freq) < length(input_sound))
cutoff_freq = [cutoff_freq(max_f:-centre_f:min_f)];
cutoff_freq = [cutoff_freq(min_f:centre_f:max_f)];
end
You only enter the body of the while loop if the cutoff_freq array is shorter than the input_sound array...but while you re-assign new values to cutoff_freq, nothing changes on the next iteration of the loop. I think that you want to augment/concatenate the cutoff_freq array with a subset of its data (since you have the []) but you need to include this array in the command. For example, if
x = [1 2 3 4];
x = [x 5 6 7]; % so now x = [1 2 3 4 5 6 7]
As for the error/warning, since max_f:-centre_f:min_f) is an array of doubles, you cannot use them as indices into cutoff_freq...you need to use integers as indices (hence the error message). Do you mean these to be indices or are you trying to augment cutoff_freq with these values like
cutoff_freq = [cutoff_freq max_f:-centre_f:min_f];

  0 Comments

Sign in to comment.