Digital Filter Design
3 Design of a Simple FIR Filter
the difference equation, system diagram, and the analytical expression
of the impulse response for the filter Hf (z).
We have,
Hf(z) =1- 2cosz-1 +z-2
= 1- 2cosz-1 +z-2
y(n) = x(n) +ax(n-1)+x(n-2) (Difference equation)
the impulse response h(n) can be found by putting x(n)= (n) in above equation
h(n) = (n) +a (n-1)+ (n-2) (Impulse Response)
where, a= - 2cos
SYSTEM
DIAGRAM
i) = pi/6
Hf(z) =1- 2cos(pi/6)z-1 +z-2
ii) = pi/3
Hf(z) =1- 2cos(pi/3)z-1 +z-2
ii) = pi/2
Hf(z) =1- 2cos(pi/2)z-1 +z-2
Hf(z) =1 + z-2
Digital Filter Design
plot of the magnitude response for the three values of ..
%%Matlab commands for the magnitude response for the three values of
w = -pi:2*pi/200:pi;
theta1=pi/6;
theta2=pi/3;
theta3=pi/2;
H1 =1-2*(cos(theta1) )*exp(-j*w)+ exp(-j*2*w);
H2 =1-2*(cos(theta2) )*exp(-j*w)+ exp(-j*2*w);
H3 =1-2*(cos(theta3) )*exp(-j*w)+ exp(-j*2*w);
subplot(311)
plot(w,abs(H1))
grid
ylabel('Magnitude')
xlabel('(rad)')
title('pi/6')
subplot(312)
plot(w,abs(H2))
grid
ylabel('Magnitude')
xlabel('(rad)')
title('pi/3')
subplot(313)
plot(w,abs(H3))
grid
ylabel('Magnitude')
xlabel('(rad)')
title('pi/2')
Digital Filter Design
Explain how the value of affects the magnitude of the filters frequency
response
Digital Filter Design
The time domain plot of 101 samples of original nspeech1
load nspeech1
sound(nspeech1)
sample=nspeech1(100:200);
t=100:200;
plot(t,sample)
title('101 sample values of npeech1')
Digital Filter Design
plot of the magnitude of the DTFT for 1001 samples of npeech1
sample1=nspeech1(100:1100);
[X,w]=DTFT(sample1,0);
plot(w,abs(X));
ylabel('Magnitude')
xlabel('(rad)')
[Xmax,Imax]=max(abs(X));
theta= w(Imax)
ans =
-1.3254
Digital Filter Design
Matlab function FIRfilter(x) that implements the filter Hf (z) with the
measured value of and outputs the filtered signal
Version1
function [ F ] = FIRfilter(x,theta)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
a=-2*cos(theta);
f=[1,a,1];
F=conv(x,f);
Version2
function [ F ] = FIRfilter(x)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
a=-2*cos( -1.3254);
f=[1,a,1];
F=conv(x,f);
Filtering using version 1
%%Matlab commands
theta= w(Imax)
F=Firfilter(nspeech1,theta);
sound(f)
Digital Filter Design
The time domain plot of 101 samples of filtered nspeech1
sample2=F(100:200);
t=100:200;
figure(1)
plot(t,sample2)
title('101 sample values of filtered nspeech1')
plot of the magnitude of the DTFT for 1001 samples of filtered npeech1
sample3=F(100:1100);
[X,w]=DTFT(sample3,0);
figure(2)
plot(w,abs(X));
ylabel('Magnitude')
xlabel('(rad)')
title('Magnitude response of Filtered signal')
Digital Filter Design
Comment on how the frequency
content of the signal changed after filtering. Is the filter we used a lowpass, highpass,
bandpass, or a bandstop filter? Comment on how the filtering changed the quality of the
audio signal.
Digital Filter Design
4 Design of A Simple IIR Filter
the difference equation, system diagram, and the analytical expression
of the impulse response for the filter Hi(z).
We have,
HI(z) = (1-r)/( 1- 2cos.rz-1 +r2z-2)
= (1-r)/( 1- 2rcosz-1 + r2z-2)
( 1- 2rcosz-1 + r2z-2)Y(z) = (1-r)X(z)
y(n)+ ary(n-1)+ r2y(n-2) = (1-r)x(n)
(Difference equation)
h(n) = (1-r) rnsin (n+1) /sin *U(n+1) (Impulse Response)
the plot of the magnitude of the frequency response for each value of r.
Matlab Commands
clear all
close all
clc
r1= 0.99;
r2 = 0.9;
r3 = 0.7;
theta=pi/3;
w = -pi:2*pi/200:pi;
H1 = (1-r1)./ (1-(2*r1*(cos(theta)) *exp(-j*w))+(r1^2)* exp(-j*2*w));
H2 = (1-r2)./ (1-(2*r2*(cos(theta))* exp(-j*w))+(r2^2)* exp(-j*2*w));
H3 = (1-r3)./(1-(2*r3*(cos(theta)) *exp(-j*w))+(r3^2)* exp(-j*2*w));
subplot(311)
plot(w,abs(H1))
grid
ylabel('Magnitude')
Digital Filter Design
xlabel('(rad)')
title('r=0.99')
subplot(312)
plot(w,abs(H2))
grid
ylabel('Magnitude')
xlabel('(rad)')
title('r=0.9')
subplot(313)
plot(w,abs(H3))
grid
ylabel('Magnitude')
xlabel('(rad)')
title('r=0.7')
Explain how the value of r affects this magnitude.
Digital Filter Design
time domain plot of the signal for 101 points of PCM
clear all
close all
load pcm
sound(pcm)
sample=pcm(100:200);
t=100:200;
plot(t,sample)
title('101 sample values of pcm')
Digital Filter Design
plot of the magnitude of the DTFT computed from 1001 samples of the
PCM
Matlab Commands
sample1=pcm(100:1100);
[X,w]=DTFT(sample1,0);
plot(w,abs(X));
ylabel('Magnitude')
xlabel('(rad)')
[Xmax,Imax]=max(abs(X));
theta= w(Imax);
theta =-2.4728;
figure(2)
plot(w,abs(X));
ylabel('Magnitude')
xlabel('(rad)')
Digital Filter Design
plot of the magnitude of the DTFT for ! in the range [_ 0.02, _ + 0.02].
figure(2)
plot(w,abs(X));
ylabel('Magnitude')
xlabel('(rad)')
xlim([theta-0.02 theta+0.02])
title('DTFT in the range of theta 0.02, theta+ 0.02')
Digital Filter Design
Calculation of theta
y(n)+ ary(n-1)+ r2y(n-2) = (1-r)x(n)
(Difference equation)
The pcm signal is modulated at 3146Hz and sampled at 8kHz
from sampling theorem that a frequency of 2pi corresponds to the sampling frequency.)
hence we have theta =2Pi/[(8000/3146)]=2.4743
code for the IIRfilter filtering function
function [ y ] = IIRfilter( x,theta,r )
%UNTITLED Summary of this function goes here
% y(n)+ ary(n-1)+ r2y(n-2) = (1-r)x(n)(Difference equation)
a=-2*cos(theta);
y(1)=(1-r)*x(1);
y(2)=-a*r*y(1)+(1-r)*x(2);
for n=3:length(x)
y(n)=-a*r*y(n-1)-r^2*y(n-2) +(1-r)*x(n);
end;
end
%%Matlab commands for filtering PCM
load pcm
r=0.995;
theta=2.4743;
filtered_pcm=IIRfilter(pcm,theta,r);
sound(filtered_pcm)
Digital Filter Design
time domain plot of the signal for 101 points of filtered PCM
y=filtered_pcm;
sample=y(100:200);
t=100:200;
figure(1)
plot(t,sample)
title('101 sample values of fitered_pcm')
plot of the magnitude of the DTFT computed from 1001 samples of the
filtered PCM
sample1=y(100:1100);
[X,w]=DTFT(sample1,0);
figure(2)
plot(w,abs(X));
ylabel('Magnitude')
xlabel('(rad)')
title('Magnitude spectrum of fittered_pcm')
Digital Filter Design
5 Lowpass Filter Design Parameters
load nspeech2;
sound(nspeech2)
x=nspeech2;
%%%Matlab function that computes the truncated and shifted impulse
response of size N for a low pass filter with a cutoff frequency of wc = 2.0.
function [ h ] = LPFtrunc(N)
wc=2;
for n=1:N
h(n) = (wc/pi) * sinc( (wc/pi) *( (n-1) -(N-1)/2 ) );
end;
end
plots of the magnitude response for the two filters (not in decibels).
Matlab commands
Digital Filter Design
f1= LPFtrunc(21)
f2 =LPFtrunc(101)
[X1,w1]=DTFT(f1,512);
[X2,w2]=DTFT(f2,512);
figure(1)
plot(w1,(abs(X1))); %to plot the graph
title('fir with N=21')
figure(2)
plot(w2,(abs(X2)));
title('fir with N=101')%to plot the graph
plots of the magnitude response in decibels for the two filters
Matlab Commands
[X1,w1]=DTFT(f1,512);
[X2,w2]=DTFT(f2,512);
figure(1)
plot(w1,20*log10(abs(X1))); %to plot the graph
title('fir with N=21')
figure(2)
plot(w2,20*log10(abs(X2)));
title('fir with N=101')%to plot the graph
Digital Filter Design
%%Filtering and quality of the filtered signals
Filt_1=conv(f1,x);
%%filtering the nspeech2 with N=21
Filt_2=conv(f2,x);
%%filtering the nspeech2 with N=101
sound(Filt_1)
sound(Filt_2)