% % 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
% % 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
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
====================================================
function UDUADF
% program to illustrate adaptive filtering using
% the RLS algorithm via the UDU factorization
clear all;
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
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;
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
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
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)
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
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');