Using a filter difference equation in a for loop to filter a .wav file within the loop

12 views (last 30 days)
Based on the bandpass filter difference equation inside a for loop:
x = zeros (30,1);
y = zeros (30,1);
x(3,1) = 1;
for
y(n) = a1*x(n) + a2*x(n-1) + a3*x(n-2) - b1*y(n-1) - b2*y(n-2);
end
How can change a .wav file by running that difference equation through the audio?

Answers (1)

Mathieu NOE
Mathieu NOE on 8 Dec 2021
hello
see example below - notice b are usually coeffcient names for numerator so they are used for the x (input) , wheras a coefficients are for the denominator - output y (hence the transfer H = B(z^-1)/A(z^-1))
but you are free to use your own convention as soon as the coeffs have the good value !
enjoy
%% data
[x,Fs] = audioread('test_voice_mono.wav');
[samples,channels] = size(x);
dt = 1/Fs;
time = (0:samples-1)*dt;
%% IIR filter recursive equation
c1 = 8;
c2 = 2;
c3 = 7;
b0 = 0.05 * c1;
b1 = 0.03 * c2;
b2 = 0.02 * c3;
a1 = 0.5;
a2 = 0.5;
% manual for loop coding IIR filter
y(1) = b0*x(1) + 0 + 0 + 0 + 0; % 1st iteration
y(2) = b0*x(2) + b1*x(1) + 0 + a1*y(1) + 0; % 2nd iteration
for k = 3:samples % for iteration # 3 and after
y(k) = b0*x(k) + b1*x(k-1) + b2*x(k-2) + a1*y(k-1) + a2*y(k-2);
end
figure(1)
plot(time,x,time,y)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!