Sei sulla pagina 1di 7

___________________

___________________
ECE515FL
___________________

ACTIVITY NO. 8
IIR Filter Design

Date Performed:
______________
Date Submitted:
______________

Activity:
1. Design a 2nd-order Butterworth bandpass digital filter with fc=500Hz when the
sampling frequency is 22050Hz.
(a) Plot the magnitude response over the frequency range 300Hz to 700Hz.
Program:
clc;clear;
stacksize(10^8)
s=poly(0,'s');
//PRE-WARP THE CUT-OFF FREQUENCIES
//Prewarping equation: wa=2*Fs*tan(wd/2/Fs)
//wa - pre-warped analog filter cut-off frequencies, in radians/second
//wd - digital filter cut-off frequencies, in radians/second
Fs=22050; //sampling frequency
fd1=500; //3dB cut-off
fd2=1000; //3dB cut-off
wd1=2*%pi*fd1;
wd2=2*%pi*fd2;
wa1=2*Fs*tan(wd1/2/Fs);
wa2=2*Fs*tan(wd2/2/Fs);
//GENERATE THE BUTTERWORTH DENOMINATOR POLYNOMIAL
n=2; //number of poles
Hs_den=2; //Initialize the denominator polynomial
//nth-order
for k=1:n
Theta=(%pi/2)+((2*k-1)*%pi)/(2*n);
pole=exp(Theta*%i);
Hs_den=Hs_den*(s-pole);
end;
Hslp=1/real(Hs_den)
//TRANSFORM THE LOWPASS TO BANDPASS
Hs=horner(Hslp,(s^2+wa1*wa2)/((wa2-wa1)*s))
//TRANSFORM H(s) TO H(z) USING BILINEAR TRANSFORMATION
z=poly(0,'z');
Hz=horner(Hs,2*Fs*(z-1)/(z+1))
//PLOT THE FREQUENCY RESPONSE OF THE DIGITAL FILTER
1

___________________
___________________
ECE515FL
___________________

ACTIVITY NO. 8
IIR Filter Design

Date Performed:
______________
Date Submitted:
______________

Fs=22050;
f=300:700;
W=(2*%pi*f)/Fs;
HW=horner(Hz,exp(W*%i));
plot2d(f,abs(HW)); xgrid();

(b) Load the audio file dinar_16bit_22050Hz_stereo.wav and filter the audio signal.
Filter each channel separately and then save the result as a stereo signal. Save the filtered
audio signal as filter1.wav.

Program:
clc;clear;
stacksize(10^8)
s=poly(0,'s');
//PRE-WARP THE CUT-OFF FREQUENCIES
//Prewarping equation: wa=2*Fs*tan(wd/2/Fs)
//wa - pre-warped analog filter cut-off frequencies, in radians/second
//wd - digital filter cut-off frequencies, in radians/second
Fs=22050; //sampling frequency
fd1=500; //3dB cut-off
fd2=1000; //3dB cut-off
2

___________________
___________________
ECE515FL
___________________

ACTIVITY NO. 8
IIR Filter Design

Date Performed:
______________
Date Submitted:
______________

wd1=2*%pi*fd1;
wd2=2*%pi*fd2;
wa1=2*Fs*tan(wd1/2/Fs);
wa2=2*Fs*tan(wd2/2/Fs);
//GENERATE THE BUTTERWORTH DENOMINATOR POLYNOMIAL
n=2; //number of poles
Hs_den=2; //Initialize the denominator polynomial
//nth-order
for k=1:n
Theta=(%pi/2)+((2*k-1)*%pi)/(2*n);
pole=exp(Theta*%i);
Hs_den=Hs_den*(s-pole);
end;
Hslp=1/real(Hs_den)
//TRANSFORM THE LOWPASS TO BANDPASS
Hs=horner(Hslp,(s^2+wa1*wa2)/((wa2-wa1)*s))

//TRANSFORM H(s) TO H(z) USING BILINEAR TRANSFORMATION


z=poly(0,'z');
Hz=horner(Hs,2*Fs*(z-1)/(z+1))
//FILTER AN AUDIO FILE
stacksize(5*10^7);
x=wavread('F:\DINAR_16bit_22050_stereo.wav');
yleft=dsimul(tf2ss(Hz),x(1,:)); //Filter the left channel samples
yright=dsimul(tf2ss(Hz),x(2,:)); //Filter the right channel samples
yleft=yleft/max(abs(yleft)); //Normalize the filtered left channel samples
yright=yright/max(abs(yright)); //Normalize the filtered right channel samples
z=[yleft;yright];
wavwrite(z, Fs, 'F:\filter1.wav');

2. Filter the audio signal in sonatina.wav to separate its treble and bass parts. The
bass part has a highest frequency of 196Hz while the treble part has a lowest
frequency of 261.63Hz. Hence, to extract the bass part, use a lowpass filter with
fc=200Hz. For the treble part, use a highpass filter with fc=250Hz. Use 4th-order
Butterworth filters.
3

___________________
___________________
ECE515FL
___________________

ACTIVITY NO. 8
IIR Filter Design

(a) Plot the magnitude response of the filters from 0 to 400Hz.


Program:
clc;clear;
stacksize(10^8)
s=poly(0,'s');
//PRE-WARP THE CUT-OFF FREQUENCIES
//Prewarping equation: wa=2*Fs*tan(wd/2/Fs)
//wa - pre-warped analog filter cut-off frequencies, in radians/second
//wd - digital filter cut-off frequencies, in radians/second
Fs=11025; //sampling frequency
fd1=200; //3dB cut-off lowpass
fd2=250; //3dB cut-off highpass
wd1=2*%pi*fd1;
wd2=2*%pi*fd2;
wa1=2*Fs*tan(wd1/2/Fs);
wa2=2*Fs*tan(wd2/2/Fs);
//GENERATE THE BUTTERWORTH DENOMINATOR POLYNOMIAL
n=4; //number of poles
Hs_den=4; //Initialize the denominator polynomial
//nth-order
for k=1:n
Theta=(%pi/2)+((2*k-1)*%pi)/(2*n);
pole=exp(Theta*%i);
Hs_den=Hs_den*(s-pole);
end;
Hslp=1/real(Hs_den)
//TRANSFORM THE LOWPASS TO LOWPASS
Hs1=horner(Hslp,s/wa1);
//TRANSFORM THE LOWPASS TO HIGHPASS
Hs2=horner(Hslp,wa2/s);
//PLOT THE FREQUENCY RESPONSE OF THE ANALOG FILTERS
f=0:(Fs/2);
w=2*%pi*f;
Hw1=horner(Hs1,w*%i);
subplot(2,1,1)
plot2d(f,abs(Hw1),13); xgrid();
Hw2=horner(Hs2,w*%i);
4

Date Performed:
______________
Date Submitted:
______________

___________________
___________________
ECE515FL
___________________

ACTIVITY NO. 8
IIR Filter Design

Date Performed:
______________
Date Submitted:
______________

subplot(2,1,2)
plot2d(f,abs(Hw2),13); xgrid();
//TRANSFORM H(s) TO H(z) USING BILINEAR TRANSFORMATION
z=poly(0,'z');
Hz1=horner(Hs1,2*Fs*(z-1)/(z+1));
Hz2=horner(Hs2,2*Fs*(z-1)/(z+1));

(b) Save the filtered signal in stereo with the bass part on the left channel and the
treble part on the right channel. Save as sonatina_stereo.wav .

Program:
clc;clear;
stacksize(10^8)
s=poly(0,'s');
5

___________________
___________________
ECE515FL
___________________

ACTIVITY NO. 8
IIR Filter Design

//PRE-WARP THE CUT-OFF FREQUENCIES


//Prewarping equation: wa=2*Fs*tan(wd/2/Fs)
//wa - pre-warped analog filter cut-off frequencies, in radians/second
//wd - digital filter cut-off frequencies, in radians/second
Fs=11025; //sampling frequency
fd1=200; //3dB cut-off lowpass
fd2=250; //3dB cut-off highpass
wd1=2*%pi*fd1;
wd2=2*%pi*fd2;
wa1=2*Fs*tan(wd1/2/Fs);
wa2=2*Fs*tan(wd2/2/Fs);
//GENERATE THE BUTTERWORTH DENOMINATOR POLYNOMIAL
n=4; //number of poles
Hs_den=4; //Initialize the denominator polynomial
//nth-order
for k=1:n
Theta=(%pi/2)+((2*k-1)*%pi)/(2*n);
pole=exp(Theta*%i);
Hs_den=Hs_den*(s-pole);
end;
Hslp=1/real(Hs_den)
//TRANSFORM THE LOWPASS TO LOWPASS
Hs1=horner(Hslp,s/wa1);
//TRANSFORM THE LOWPASS TO HIGHPASS
Hs2=horner(Hslp,wa2/s);
//PLOT THE FREQUENCY RESPONSE OF THE ANALOG FILTERS
f=0:(Fs/2);
w=2*%pi*f;
Hw1=horner(Hs1,w*%i);
subplot(2,2,1)
plot2d(f,abs(Hw1)); xgrid();
Hw2=horner(Hs2,w*%i);
subplot(2,2,2)
plot2d(f,abs(Hw2)); xgrid();
//TRANSFORM H(s) TO H(z) USING BILINEAR TRANSFORMATION
z=poly(0,'z');
6

Date Performed:
______________
Date Submitted:
______________

___________________
___________________
ECE515FL
___________________

ACTIVITY NO. 8
IIR Filter Design

Date Performed:
______________
Date Submitted:
______________

Hz1=horner(Hs1,2*Fs*(z-1)/(z+1));
Hz2=horner(Hs2,2*Fs*(z-1)/(z+1));
//PLOT THE FREQUENCY RESPONSE OF THE DIGITAL FILTER
W=0.001:0.001:%pi;
f=W*Fs/2/%pi;
HW1=horner(Hz1,exp(W*%i));
subplot(2,2,3)
plot2d(f,abs(HW1)); xgrid();
HW2=horner(Hz2,exp(W*%i));
subplot(2,2,4)
plot2d(f,abs(HW2)); xgrid();
//Normalize transfer function
Hz1=Hz1/(max(abs(HW1)));
Hz2=Hz2/(max(abs(HW2)));
//FILTER AN AUDIO FILE
stacksize(5*10^7);
x=wavread('F:\sonatina.wav');
yleft=dsimul(tf2ss(Hz1),x); //Filter the left channel samples
yright=dsimul(tf2ss(Hz2),x); //Filter the right channel samples
yleft=yleft/max(abs(yleft)); //Normalize the filtered left channel samples
yright=yright/max(abs(yright)); //Normalize the filtered right channel samples
z=[yleft;yright];
wavwrite(z, Fs, 'F:\sonatina_stereo.wav');

Potrebbero piacerti anche