0% found this document useful (0 votes)
54 views

% % Program Name - Prob8261a.m (Modified Problem 8.26 (1), p553) % Comparison of Characteristic Features of Discrete-Time Butterworth, % Chebyshev 1, Chebyshev 2 and Elliptic Lowpass Filters

The document describes the design and comparison of different types of lowpass filters, including Butterworth, Chebyshev 1, Chebyshev 2, and elliptic filters. It calculates the filter coefficients for each type based on specified passband and stopband frequencies and ripple values. It then plots the magnitude and phase frequency responses of each filter to compare their characteristic features.

Uploaded by

Mehul Patel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

% % Program Name - Prob8261a.m (Modified Problem 8.26 (1), p553) % Comparison of Characteristic Features of Discrete-Time Butterworth, % Chebyshev 1, Chebyshev 2 and Elliptic Lowpass Filters

The document describes the design and comparison of different types of lowpass filters, including Butterworth, Chebyshev 1, Chebyshev 2, and elliptic filters. It calculates the filter coefficients for each type based on specified passband and stopband frequencies and ripple values. It then plots the magnitude and phase frequency responses of each filter to compare their characteristic features.

Uploaded by

Mehul Patel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

% % Program name - prob8261a.m (modified problem 8.

26(1), p553)
% Comparison of characteristic features of discrete-time Butterworth,
% Chebyshev 1,Chebyshev 2 and elliptic lowpass filters.
%
Fs=8000;
fp=500/4000;
fs=2000/5000;
Ap=0.1; As=60;
%
% Calculate filter coefficients
[N1, wc1]=buttord(fp, fs, Ap, As); % filter order
[N2, wc2]=cheb1ord(fp, fs, Ap, As);
[N3, wc3]=cheb2ord(fp, fs, Ap, As);
[N4, wc4]=ellipord(fp, fs, Ap, As);
[b1, a1]=butter(4, fp); % Butterworth filter
[b2, a2]=cheby1(4, Ap, fp);
[b3, a3]=cheby2(4, As, fs);
[b4, a4]=ellip(4, Ap, As, fp);
%
% Calculate frequency responses
%
[H1, f1]=freqz(b1, a1, 512, Fs);
[H2, f2]=freqz(b2, a2, 512, Fs);
[H3, f3]=freqz(b3, a3, 512, Fs);
[H4, f4]=freqz(b4, a4, 512, Fs);
%
% Plot magnitude frequency responses
%
figure (1);
plot(f1, 20*log10(abs(H1)), 'r:') % Plot Butterworth magnitude response
hold on
figure (1);
plot(f2, 20*log10(abs(H2)), 'b--') % Plot Cheby1 magnitude response
hold on
figure (1);
plot(f3, 20*log10(abs(H3)), 'g-.') % Plot Cheby2 magnitude response
hold on
figure (1);
plot(f4, 20*log10(abs(H4)), 'k-') % Plot elliptic magnitude response
hold on
legend('Butworth', 'Cheby1', 'Cheby2', 'Ellip');
axis([0 4000 -100 10])
ylabel('Magnitude (dB)')
xlabel('Frequency (Hz)')
title('Filter magnitude responses')
hold off;
%
% Plot phase responses
%
figure (2);
plot(f1, angle(H1)*180/pi, 'r:') % Plot BZT phase response
hold on
figure (2);
plot(f2, angle(H2)*180/pi, 'b--') % Plot Cheby 1 phase response
hold on
figure (2);
plot(f3, angle(H3)*180/pi, 'g-.') % Plot Cheby 2 phase response
hold on
figure (2);
plot(f4, angle(H4)*180/pi, 'k-') % Plot elliptic phase response
hold on
figure(2);
legend('Butter', 'Cheby1','Cheby2', 'Ellip');
axis([0 4000 -360 360])
ylabel('Phase (Degrees)')
xlabel('Frequency (Hz)')
title('Filter Phase Responses')
hold off
%
% Plot pole-zero diagrams
%
figure (3);
zplane(b1, a1)
title('Pole-zero diagram - Butterworth filter')
figure (4);
zplane(b2, a2)
title('Pole-zero diagram - Chebyshev 1 filter')
figure (5);
zplane(b3, a3)
title('Pole-zero diagram - Chebyshev 2 filter')
figure (6);
zplane(b4, a4)
title('Pole-zero diagram - Elliptic filter')
N1
N2
N3
N4
function LMSADF
%Program to illustrate adaptive filtering using the LMS algorithms

% X delayed input data vector


% Y measured signal
% W coefficient vector
% E enhanced signal

N=30; % filter length


M=0; % delay
w0=1; % initial value for adaptive filter coefficients
SF=2048; % factor for reducing the data samples - 11 bit ADC assumed
mu=0.04;
X = zeros(N,1);
delay = zeros(1,M+1);
W = w0*ones(N,1);
in = fopen('ADF.dat','r'); %read input data from specified data file
Y = fscanf(in,'%g',inf)/SF;
fclose(in);
if w0==0
sf = SF; % scaling factor for display
else
sf = SF/N/w0;
end
for i=1:length(Y)
if M>0
delay(2:M+1) = delay(1:M); % shift data for delay
end
delay(1) = Y(i);
X(2:N) = X(1:N-1); % update buffer
X(1) = delay(M+1);
E(i) = Y(i)-W'*X; % the enhanced signal
W = W + 2*mu*E(i)*X; % update the weights
end
subplot(2,1,1),plot(1:length(Y),Y*SF); title('Input Signal');
subplot(2,1,2),plot(1:length(E),E*sf); title('Enhanced Signal');

====================================================

function UDUADF
% program to illustrate adaptive filtering using
% the RLS algorithm via the UDU factorization

% X delayed input data vector


% Y measured signal
% W coefficient vector
% E enhanced signal

clear all;

N = 30; % filter length


M = 1; % delay
npt = N*(N+1)/2;
SF = 2048; % 12-bit ADC scaling
p0 = 0.05;
w0 = 1;
gamma = 0.98;
RemoveMean = 0; % 1 - remove the mean from the data, 0 - otherwise

delay = zeros(1,M);
U=zeros(1,npt);
U(1)=p0;
W = w0*ones(N,1);
X = zeros(N,1);
for i=1:N-1
ik=(i*(i+1)-2)/2+1;
U(ik)=p0;
end

if w0==0
sf = SF; % scaling factor for display
else
sf = SF/N/w0;
end

in = fopen('ADF.dat','r'); %read input data from specified data file


Y = fscanf(in,'%g',inf)/SF;
fclose(in);

if RemoveMean % remove the mean from the data if required


Y = Y - sum(Y)/length(Y);
end

for i=1:length(Y)
if M>0
delay(2:M+1) = delay(1:M); % shift input data in delay registers
end
delay(1) = Y(i);
X(2:N) = X(1:N-1); % update buffer
X(1) = delay(M+1);
E(i) = Y(i) - X'*W; % the enhanced signal
W = uduflt(W,X,U,E(i),gamma ,N);
end
subplot(2,1,1),plot(1:length(Y),Y*SF); title('Input Signal');
subplot(2,1,2),plot(1:length(E),E*sf); title('Enhanced Signal');

==========================================
function w=uduflt(w,x,u,ek,gamma,N)
% udu algorithm - a numerically stable form of
% the recursive least squares algorithm
%
% inputs:
% x() input vector
% dn latest input data value
% w() coefficient vector
% u() vector containing elements of U and D
%
% outputs:
% en error signal
% yn digital filter output
% w() updated coefficient vector
% u() updated elements of U and D
%

sf = 1/gamma;

m=1; % update the UD elements


v=zeros(1,N);
v(1)=x(1);
for j=2:N
v(j)=x(j);
for k=1:j-1
m=m+1;
v(j)=v(j)+u(m)*x(k);
end
m=m+1;
b(j)=u(m)*v(j);
end
b(1)=u(1)*x(1);
alpha=gamma+b(1)*v(1);
delta=1/alpha;
u(1)=u(1)*delta;

m=1;
for j=2:N
beta1=alpha;
alpha=alpha+b(j)*v(j);
p=-v(j)*delta;
delta=1/alpha;
for k=1:j-1
m=m+1;
beta=u(m);
u(m)=beta+b(k)*p;
b(k)=b(k)+b(j)*beta;
end
m=m+1;
u(m)=u(m)*beta1*delta*sf;
end
perr=ek/alpha;
for j=1:N % update the weights
w(j)=w(j)+b(j)*perr;
end

============================================
function SQRTADF
% program to illustrate adaptive filtering using
% the square root RLS algorithm

% X delayed input data vector


% Y measured signal
% W coefficient vector
% E enhanced signal

N = 30; % filter length


M = 1; % delay
npt = N*(N+1)/2;
SF = 2048; % 12-bit ADC scaling
p0 = 0.05;
w0 = 1;
gamma = 0.98;
RemoveMean = 0; % 1 - remove the mean from the data, 0 - otherwise

delay = zeros(1,M);
W = w0*ones(N,1);
X = zeros(N,1);
S = zeros(1,npt);
S(1)=p0;
for i=1:N-1
ik=(i*(i+1)-2)/2+1;
S(ik)=p0;
end

if w0==0
sf = SF; % scaling factor for display
else
sf = SF/N/w0;
end

in = fopen('ADF.dat','r'); %read input data from specified data file


Y = fscanf(in,'%g',inf)/SF;
fclose(in);

if RemoveMean % remove the mean from the data if required


Y = Y - sum(Y)/length(Y);
end

for i=1:length(Y)
if M>0
delay(2:M+1) = delay(1:M); % shift input data in delay registers
end
delay(1) = Y(i);
X(2:N) = X(1:N-1); % update buffer
X(1) = delay(M+1);
E(i) = Y(i) - X'*W; % the enhanced signal

W = sqrtflt(W,X,E(i),S,gamma,N);
end
subplot(2,1,1),plot(1:length(Y),Y*SF); title('Input Signal');
subplot(2,1,2),plot(1:length(E),E*sf); title('Enhanced Signal');

==================================================

function w=sqrtflt(w,x,perr,s,gamma,N)

% A simple square root RLS adaptive filter


% For details, see:
% Digital Signal Processing: A Practical Approach
% E C Ifeachor and B W Jervis, Pearson, 2002

forgt=sqrt(gamma);
sig=forgt;
sigsq=forgt*forgt;
ij=1; ji=1;
for j=2:N
fj=0.0;
for i=1:j-1
ji=ji+1;
fj=fj+s(ji)*x(i);
end
a=sig/forgt;
b=fj/sigsq;
sigsq=sigsq+fj*fj;
sig=sqrt(sigsq);
a=a/sig;
g(j)=s(ji)*fj;
s(ji)=a*s(ji);
for i=1:j-1
ij=ij+1;
sqp=s(ij);
s(ij)=a*(sqp-b*g(i));
g(i)=g(i)+sqp*fj;
end
ij=ij+1;
end
w = w + g'*perr/sigsq;

=============================
function RLSadf
% program to illustrate adaptive filtering using
% the RLS algorithm

% X delayed input signal


% Y measured signal
% W coefficient vector
% E enhanced signal

N = 30; % filter length


M = 1; % stages of delay
SF = 2048; % 12-bit ADC scaling
p0 = 0.05;
w0 = 100;
gamma = 0.98;
RemoveMean = 0; % 1 to remove the mean, 0 otherwise
W = w0*ones(N,1); % adaptive filter weights
X = zeros(N,1);
delay = zeros(1,M+1);
P = p0*diag(ones(1,N),0);
if w0==0
sf = SF; % scaling factor for display
else
sf = SF/N/w0;
end

in = fopen('ADF.dat','r'); %read input data from specified data file


Y = fscanf(in,'%g',inf)/SF;
fclose(in);
if RemoveMean % remove the mean from the data if required
Y = Y - sum(Y)/length(Y);
end

for i=1:length(Y)
if M>0
delay(2:M+1) = delay(1:M); % shift input data in delay registers
end
delay(1) = Y(i);
X(2:N) = X(1:N-1); % update buffer
X(1) = delay(M+1);
E(i) = Y(i) - X'*W; % the enhanced signal
G = P*X/(gamma + X'*P*X);
P = (P - G*X'*P)/gamma;
W = W + G*E(i); % update the weights
end
subplot(2,1,1),plot(1:length(Y),Y*SF); title('Input Signal');
subplot(2,1,2),plot(1:length(E),E*sf); title('Enhanced Signal');

You might also like