You are on page 1of 19

1

DSP LAB PROGRAMMES


GENERATION OF UNIT STEP SEQUENCE
clear all; clc; close all
%generate unit step sequence for N=20.
N=20;
xn=[ones(1,N) zeros(1,N)];
n=0:1:N*2-1;
subplot(2,1,1),stem(n,xn);
subplot(2,1,2),plot(n,xn);
xlabel('n');ylabel('xn');
title('Unit Step Sequence');
GENERATION OF EXPONENTIALLY INCREASING AND DECREASING SIGNALS
clear all; clc; close all
% Plot an exponential sequence (0.7)^n
N=30;
n=0:N-1;
e=exp(j*pi);
a=0.7*e;
xn=a.^n;
subplot(2,2,1),stem(n,xn);
subplot(2,2,2),plot(n,xn);
xlabel('n');ylabel('xn');
title('Exponential Decreasing Sequence');
a=-0.7*e;
xn=a.^n;
xn=a.^n;
subplot(2,2,3),stem(n,xn);
subplot(2,2,4),plot(n,xn);
xlabel('n');
ylabel('xn');
title('Exponential Decreasing Sequence');
figure
a=1.7*e;
xn=a.^n;
xn=a.^n;
subplot(2,2,1),stem(n,xn);
subplot(2,2,2),plot(n,xn);
xlabel('n');ylabel('xn');
title('Exponential Increasing Sequence');
a=-1.7*e;
xn=a.^n;
xn=a.^n;
subplot(2,2,3),stem(n,xn);
subplot(2,2,4),plot(n,xn);
xlabel('n');ylabel('xn');
title('Exponential Increasing Sequence');
GENERATION OF COSINE AND SINE WAVES OF 100HZ WITH 2KHZ SAMPLING
clear all; clc; close all
% Plot an sinusoidal sequence
fs=2000;%Sampling freq of 2KHz
n=0:1/fs:1;
xn=cos(2*pi*100*n);
subplot(2,2,1),stem(n(1:50),xn(1:50));
subplot(2,2,2),plot(n(1:50),xn(1:50));
xlabel('n');
ylabel('xn');
title('Sinusoidal Sequence');
xn=sin(2*pi*100*n);
subplot(2,2,3),stem(n(1:50),xn(1:50));
subplot(2,2,4),plot(n(1:50),xn(1:50));
xlabel('n');ylabel('xn');title('Sinusoidal Sequence');

PLOTTING OF TRAINGULAR, SAWTOOTH AND SQUARE WAVES WITHOUT USING MATLAB IN BULIT
COMMANDS TO GET ACCUSTOMED TO PLOT FEATURES AND FOR LOOP TECHNIQUES
clear all; clc; close all
%TRAINGUALAR WAVE PLOT
y=0:0.5:2;
for j=0:3
x=(4*j)+y;
plot(x,y)
hold on
end
for k=0:3;
x=(4*k)-y
plot(x,y)
hold on
end
hold off
%SAWTOOTH WAVE PLOT
figure
y=0:.5:2
for j=0:8
a=(2*j)+y
plot(a,y,'b')
hold on
end
x=2:2:18
for k=0:.01:2;
b=k;
plot(x,b,'b')
hold on
end
hold off
%SQUARE WAVE PLOT
figure
y=0:.001:2;
for j=0:2:12;
x=y;
plot(j,x,'r');
hold on;
end
for k=0:4:12;
x=k+y;
m=2;
plot(x,m,'r')
hold on
end
for k=2:4:12;
x=k+y;
m=0;
plot(x,m,'r');
hold on;
end
hold off
axis([0 12 -0.5 2.5])
GENERATION OF DIFFERENT SINOSOIDAL SIGNALS TO SHOW ALISING EFFECT IN DSP
clear all; clc; close all
% Plot an sinusoidal sequence and compare aliasing
fs=2000;%Sampling freq of 2KHz can work for frequencies upto 1KHz only
n=0:1/fs:1;
f1=100; % f1=100Hz
f2=300; % f2=300Hz
xn1=cos(2*pi*f1*n);
xn2=cos(2*pi*f2*n);
xn=xn1+xn2;
subplot(2,2,1),stem(n(1:50),xn(1:50));

subplot(2,2,2),plot(n(1:50),xn(1:50),'b', n(1:50),xn1(1:50),'g',
n(1:50),xn2(1:50),'r');
xlabel('n');
ylabel('xn');
title('Sinusoidal Sequence');
f1a=2000-100; % f2=1900Hz
f2a=2000-300; % f2=1700Hz
xn1a=cos(2*pi*f1a*n);
xn2a=cos(2*pi*f2a*n);
xna=xn1a+xn2a;
subplot(2,2,3),stem(n(1:50),xna(1:50));
subplot(2,2,4),plot(n(1:50),xna(1:50),'b', n(1:50),xn1a(1:50),'g',
n(1:50),xn2a(1:50),'r');
xlabel('n');
ylabel('xn');
title('Sinusoidal Sequence');
figure
fre1=300; % fre1=300Hz
a1=cos(2*pi*fre1*n);
fre2=2000-300; % fre2=1700Hz
a2=cos(2*pi*fre2*n);
fre3=2000+300; % fre1=2300Hz
a3=cos(2*pi*fre3*n);
subplot(3,1,1),stem(n(1:50),a1(1:50));
subplot(3,1,2),stem(n(1:50),a2(1:50));
subplot(3,1,3),stem(n(1:50),a3(1:50));
STABILITY CHECK OF AN LTI SYSTEM WITH IF-ELSE FOR INF=106
clear all; clc; close all
num=[1 0.8];
den=[1 1.5 .9];
%den=[1 1.5 0.9];% stable
%den=[1 1.5 9];% unstable
N=200;
h=impz(num,den,N+1);
sum=0;
n=0:N;
for k=1:N+1
sum=sum+h(k);
if abs(sum)>10^6;
disp('UNSTABLE LTI SYSTEM');
break
end
if abs(h(k))<10^(-6);
disp('STABLE LTI SYSTEM');
break
end
if k==N+1;
disp('STABLE LTI SYSTEM');
end
end
stem(n,h); grid;
disp('Total Sum of impulses ='),
disp(sum)
LINEARITY PROPERY OF A LTI SYSTEM
clear all; clc; close all
%Linearity property of 2 sequences
n=0:40; a=2; b=-3;
x1=cos(2*pi*0.1*n);
x2=cos(2*pi*0.4*n);
x=a*x1+b*x2;
ic=[0 0];% initially relaxed
%ic=[0 2];% initially not relaxed
num=[2.2403 2.4908 2.2403];
den=[1 -0.4 0.75];

y1=filter(num,den,x1,ic);
y2=filter(num,den,x2,ic);
y=filter(num,den,x,ic);
yt=a*y1+b*y2;
d=y-yt;
if abs(sum(sum(d)))<10^(-3)
disp('LTI SYSTEM IS LINEAR')
else
disp('LTI SYSTEM IS NON-LINEAR')
end
subplot(3,1,1), stem(n,y); grid
subplot(3,1,2), stem(n,yt); grid
subplot(3,1,3), stem(n,d); grid
TIME SHIFT-INVARIANCE PROPERY OF A LTI SYSTEM
clear all; clc; close all
%Linearity property of 2 sequences
n=0:40;D=10;
x=3*cos(2*pi*0.1*n)-2*cos(2*pi*0.4*n);
xd=[zeros(1,D) x];
num=[2.2403 2.4908 2.2403];
den=[1 -0.4 0.75];
ic=[0 0];% initially relaxed
%ic=[0 2];% initially not relaxed
y=filter(num,den,x,ic);
yd=filter(num,den,xd,ic);
d=y-yd(1+D:41+D);
subplot(3,1,1),stem(y),grid;
subplot(3,1,2),stem(yd),grid;
subplot(3,1,3),stem(d),grid;
if abs(sum(sum(d)))<10^(-3)
disp('LTI SYSTEM IS TIME-SHIFT INVARIANT')
else
disp('LTI SYSTEM IS TIME-SHIFT VARIANT')
end
CONVOLUTION PROCESS CLARIFICATION USING FOR
clear all; clc; close all
% comparison of convolution without command
% 'conv' command to clarify the convolution
x=[1 4 2 4 1 1];
h=[1 2 3 4 5];
len1=length(x);
len2=length(h);
len=len1+len2-1;
a=fliplr(h);
for i=1:len
c(i)=0;
for j=1:len1
if j>i
continue;
end
if(len2-i+j)<=0
continue;
end
c(i)=c(i)+(x(j)*a(len2-i+j));
end
end
k=1:len;
conv_op1=c(1:len)
subplot(2,1,1),stem(k,conv_op1);
title('Without using "conv" command')
conv_op2=conv(x,h)% uses the filter command
subplot(2,1,2),stem(k,conv_op2)

LOOP AND IFELSE CASES


with convolution using the
process

internally

AUTO AND CROSS- CORRELATION OF SIGNAL, NOISE AND NOISY SIGNAL


clear all; clc; close all
%Auto correlation and cross correlation fumctions
N = 96;
n = 1 : N;
x = cos(0.25 *pi *n);
rx = conv(x,fliplr(x));
disp('ACF of X=')
min(min(corrcoef(x,x)))
k = -28 : 28;
subplot(3,1,1);
stem(k,rx(68 : 124));
xlabel(' log index');ylabel(' Amplitude');title(' clean signal ACF');
w=rand(1,N)-0.5;
y=x+w;
ry=conv(y,fliplr(y));
disp('ACF of Y=')
min(min(corrcoef(y,y)))
subplot(3,1,2);
stem (k,ry(68 : 124));
xlabel(' log index');ylabel(' Amplitude');title (' clean signal ACF');
rw=conv(w,fliplr(w));
disp('ACF of W=')
min(min(corrcoef(w,w)))
subplot(3,1,3);
stem(k,rw(68 : 124));
xlabel(' log index');ylabel(' Amplitude');title(' clean signal ACF');
rxw=conv(x,fliplr(w));
disp('CCF of X with W=')
min(min(corrcoef(x,w)))
figure
subplot (3,1,1);
stem (k,rxw(68 : 124));
xlabel(' log index');ylabel(' Amplitude');title (' X with W');
rxy=conv(x,fliplr(y));
disp('CCF of X with Y=')
min(min(corrcoef(x,y)))
subplot (3,1,2);
stem (k,rxy(68 : 124));
xlabel(' log index');ylabel(' Amplitude');title (' X with Y');
ryw=conv(y,fliplr(w));
disp('ACF of Y with W=')
min(min(corrcoef(y,w)))
subplot (3,1,3);
stem (k,rxy(68 : 124));
xlabel(' log index');ylabel(' Amplitude');title (' Y with W');
LTI SYSTEM PLOT USING ZERO-POLE-GAIN TRANSFER FUNCTION
clear all; clc; close all
% for IIR butterworth LPF of order 3 and .5 cut off
[z,p,k] = butter(3,.5);
h=zpk(z,p,k); tf(h)
[num,den] = tfdata(h,'v')
% [b,a] = butter(3,.5)
[h,w]=freqz(num,den,2^8);% considering 8 bits
plot(w/(2*pi),20*log10(h))
title('LTI response plot')
figure
subplot(2,1,1), plot(abs(h))
title('magnitude')
axis tight
subplot(2,1,2), plot(w*360/(2*pi))
title('Phase converted to degree')
axis tight

DISCRETE FOURIER TRANSFORM


clear all; clc; close all
%Simple DFT without using the FFT algo of MATLAB
N = 256;
n = 1: N;
xn = cos(0.2 *pi *n);
figure(1);
plot(n,xn);
wn = exp(-j* pi/N);
g= 1;
for k = 1: N,
n= 1:N;
xk(g,1) =xn * (wn * ones(N,1)).^(n' * k);
g = g +1;
end
X1 = abs(xk);figure(2);
plot(n/(2*N),X1/max(X1));
grid; axis([0,0.5,0,1]);
title('DFT');xlabel('Frequency in pi unit');ylabel('Mag');
COMPARISON OF THE dft USING THE EQUATION AND USING MATLABS IN BUILT FFT BASED DFT
clear all; clc; close all
%comparing DFT with the in built FFT of MATLAB
N = 256;
n = 1 : N;
xn = cos(0.2 *pi *n);
figure(1);plot(n,xn);
M=N;
m=0:(M-1);
y = fft(xn,M);
magy=abs(y);
wn = exp(-j* pi/N);
g= 1;
for k = 1: N,
n= 1:N;
xk(g,1) =xn*(wn*ones(N,1)).^(n'*k);
g = g +1;
end
X1 = abs(xk);
figure(2);
subplot(2,1,1);
plot(n/(2*N),X1/max(X1));
grid;
axis([0,0.5,0,1]);
title('DFT');xlabel('Frequency in pi unit');ylabel('Mag');
subplot(2,1,2);
plot(m/M,magy/max(magy));
grid;
axis([0,0.5,0,1]);
title('DFT with fft function');xlabel('Frequency in pi unit');ylabel('Mag');
COMPARISON OF MULTIPLICATION IN FREQUENCY DOMAIN WITH CONVOLUTION IN TIME DOMAIN
clear all; clc; close all
%showing multiplication in frequency is convolutionin time
x=[1 2 3 4];%1st sequence
h=[1 2 1 2];%2nd sequence
l=length(x)+length(h)-1;%length of convolution o/p
xe=fft(x,l);%fft of 1st squence using zero padding
he=fft(h,l);%fft of 2nd squence using zero padding
y1=ifft(xe.*he);%ifft of the prodct of two sequences
y2=conv(x,h);% normal covoluton o/p
error=y1-y2;%error between the two convolutions
k=0:l-1;
subplot(3,1,1),stem(k,y1);
subplot(3,1,2),stem(k,y2);
subplot(3,1,3),stem(k,abs(error));

PROGRAMS FOR FILTERING LONG DATA SEQUENCES


%Program for MATLAB 7 users where circular convolution function is not in built
%if present the present program (a function) to b saved as ECEE.mcan be replaced
by 'cconv' command
function [c]=ECEE(a,b,N)
% a=[1 2 3 4];%input
% b=[1 2 -1 -2];%input
% N=6;%number of points
A=fft(a,N);
B=fft(b,N);
C=A.*B;
c=ifft(C,N);
OVERLAP SAVE METHOD
%Filtering long data sequence using OVERLAP SAVE METHOD
clc
clear all
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=[1 2 -1 2 3 -2 -3 -1 1 1 2 -1];%input
h=[1 2 1 1];%system
N=4;%length of each block
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yy=conv(x,h);
subplot 311, stem(yy)
title('Using LONG linear filtering')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (N
error('N must be >=length(h)')
end
Nx=length(x);
M=length(h);
M1=M-1;
L=N-M1;
x=[zeros(1,M-1), x, zeros(1,N-1)];
h=[h zeros(1,N-M)];
K=floor((Nx+M1-1)/L);%no of blocks
y=zeros(K+1,N);
%dividing sequence in k blocks
for k=0:K
xk=x(k*L+1:k*L+N);
Y(k+1,:)=ECEE(xk,h,N);
end
Y=Y(:,M:N)';
y=(Y(:))';
subplot 312, stem(y)
title('Using Overlap SAVE method')
d=yy-y(1:length(yy));
subplot 313, stem(d)
title('Difference')
OVERLAP ADD METHOD
%Filtering long data sequence using OVERLAP ADD METHOD
clc
clear all
close all
x=[1 2 -1 2 3 -2 -3 -1 1 1 2 -1];%input
h=[1 2 1 1];%system
L=4;%length of each block
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yy=conv(x,h);
subplot 311, stem(yy)
title('Using LONG linear filtering')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Nx=length(x);
M=length(h);
M1=M-1;
R=rem(Nx,L);
N=L+M1;
x=[x zeros(1,L-R)];
h=[h zeros(1,N-M)];
K=floor(Nx/L); % number of blocks
y=zeros(K+1,N);
z=zeros(1,M1);
%dividing to K blocks
for k=0:K
xp=x(L*k+1:L*k+L);
xk=[xp z];
y(k+1,:)=ECEE(xk,h,N);
end
yp=y';
[x,y]=size(yp);
for i=L+1:x;
for j=1:y-1
temp1=i-L;
temp2=j+1;
temp3=yp(temp1,temp2)+yp(i,j);
yp(temp1,temp2)=temp3;
end
end
z=1;
for j=1:y
for i=1:x
if ((i<=L & j<=y-1)|(j==y))
ypnew(z)=yp(i,j);
z=z+1;
end
end
end
y=ypnew;
subplot 312, stem(y),title('Using Overlap Add method')
d=yy-y(1:length(yy));
subplot 313, stem(d)title('Difference')

2N point DFT of real Sequence using a single N-point DFT


Clc, clear, v=[1 2 2 2 0 1 1 1];
l=length(v);
for k=1:l/2;
h(k)=v(2*k);
g(k)=v(2*(k-1)+1);
end
x=g+i*h;
X=fft(x);
Xc=conj(X);
Xci=[Xc(1) fliplr(Xc(2:4))];
G=(1/2)*(X+Xci);
H=(-i/2)*(X-Xci);
Wn=exp(-i*2*pi/l);
for p=1:l
if p>l/2
k=p-l/2;
else
k=p;
end
V(p)=G(k)+((Wn^(p-1))*H(k));
end
error=V-fft(v);
stem(error)

FFT SPEECH SIGNAL


clc;
clear;
close all
fs=20000
t=0:1/fs:.04;
x=4*sin(2*pi*2000*t)+4*sin(2*pi*1500*t)+10*sin(2*pi*6000*t)+6*sin(2*pi*200*t);
y=fft(x);
l=length(y);
Y=y(1:(l-1)/2);
r=1/((l-1)/2);
n=0:r:1-r;
plot(n,abs(Y)), xlabel('Normalized frq- 0 to corresponding for sampling rate 20KHz &
1rad=10KHz')
% speech_dft.wav
% toilet.wav
%% ACTUAL PROGRAM%%
clc
clear
[Y,FS,NBITS]=wavread('speech_dft.wav');
x=fft(Y);
l=length(x);
X=x(1:(l-1)/2);
r=1/((l-1)/2);
n=0:r:1-r;
subplot 211, plot(Y); subplot 212, plot(n,abs(X))
pause
% of FS Hz corresponds to normalized 1 rad
%To remove 200Hz to 300Hz freq range % 2KHz is (2/FS)*200 rad % & 3KHz is (2/FS)*300
rad
rad_start=(2/FS)*200;
rad_stop=(2/FS)*300;
p_start=0;
for j=1:length(n)
if rad_start>=n(j)
p_start=j;
end
end
p_stop=0;
for j=1:length(n)
if rad_stop>=n(j)
p_stop=j;
end
end
Z=x;
for j=p_start:p_stop
Z(j)=0;
end
for j=l-p_stop:l-p_start
Z(j)=0;
end
subplot 211, plot(abs(x)); subplot 212, plot(abs(Z))
pause
y=ifft(Z);
subplot 211, plot(abs(Y));subplot 212, plot(abs(y))
wavwrite(y,FS,16,'processed_signal.wav');
wavwrite(y,FS,16,'original_signal.wav');

IIR BUTTERWORTH
% LPF
clc;
clear;
close all;
%LPF of cut off 500Hz

10

fp=500;
fs=600;
samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=buttord(wp,ws,ap,as);
[b,a]=butter(N,wn);
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
% HPF
clc;
clear;
close all;
%HPF of cut off 500Hz
fs=500;
fp=600;
samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=buttord(wp,ws,ap,as);
[b,a]=butter(N,wn,'high');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
%BPF
clc;
clear;
close all;
%BPF of pass 400-600Hz
fs1=300;
fp1=400;
fp2=600;
fs2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=buttord(wp,ws,ap,as);
[b,a]=butter(N,wn,'bandpass');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))

11

%BSF
clc;
clear;
close all;
%BSF of stop 400-600Hz
fp1=300;
fs1=400;
fs2=600;
fp2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=buttord(wp,ws,ap,as);
[b,a]=butter(N,wn,'stop');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))

IIR CHEBYSHEV TYPE I


% LPF
clc;
clear;
close all;
%LPF of cut off 500Hz
fp=500;
fs=600;
samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=cheb1ord(wp,ws,ap,as);
[b,a]=cheby1(N,ap,wn);
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
% HPF
clc;
clear;
close all;
%HPF of cut off 500Hz
fs=500;
fp=600;
samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=cheb1ord(wp,ws,ap,as);
[b,a]=cheby1(N,ap,wn,'high');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))

12

n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
%BPF
clc;
clear;
close all;
%BPF of pass 400-600Hz
fs1=300;
fp1=400;
fp2=600;
fs2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=cheb1ord(wp,ws,ap,as);
[b,a]=cheby1(N,ap,wn,'bandpass');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
%BSF
clc;
clear;
close all;
%BSF of stop 400-600Hz
fp1=300;
fs1=400;
fs2=600;
fp2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=cheb1ord(wp,ws,ap,as);
[b,a]=cheby1(N,ap,wn,'stop');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))

IIR CHEBYSHEV TYPE II


% LPF
clc;
clear;
close all;
%LPF of cut off 500Hz
fp=500;
fs=600;

13

samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=cheb2ord(wp,ws,ap,as);
[b,a]=cheby2(N,as,wn);
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
% HPF
clc;
clear;
close all;
%HPF of cut off 500Hz
fs=500;
fp=600;
samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=cheb2ord(wp,ws,ap,as);
[b,a]=cheby2(N,as,wn,'high');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
%BPF
clc;
clear;
close all;
%BPF of pass 400-600Hz
fs1=300;
fp1=400;
fp2=600;
fs2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=cheb2ord(wp,ws,ap,as);
[b,a]=cheby2(N,as,wn,'bandpass');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))

14

%BSF
clc;
clear;
close all;
%BSF of stop 400-600Hz
fp1=300;
fs1=400;
fs2=600;
fp2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=cheb2ord(wp,ws,ap,as);
[b,a]=cheby2(N,as,wn,'stop');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))

IIR ELLIPTIC
% LPF
clc;
clear;
close all;
%LPF of cut off 500Hz
fp=500;
fs=600;
samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=ellipord(wp,ws,ap,as);
[b,a]=ellip(N,ap, as,wn);
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
% HPF
clc;
clear;
close all;
%HPF of cut off 500Hz
fs=500;
fp=600;
samp=2000;
ap=.5;
as=40;
wp=fp/(samp/2);
ws=fs/(samp/2);
[N,wn]=ellipord(wp,ws,ap,as);
[b,a]=ellip(N,ap, as,wn,'high');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))

15

n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*700*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
%BPF
clc;
clear;
close all;
%BPF of pass 400-600Hz
fs1=300;
fp1=400;
fp2=600;
fs2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=ellipord(wp,ws,ap,as);
[b,a]=ellip(N,ap, as,wn,'bandpass');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))
%BSF
clc;
clear;
close all;
%BSF of stop 400-600Hz
fp1=300;
fs1=400;
fs2=600;
fp2=700;
samp=2000;
ap=.5;
as=40;
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
[N,wn]=ellipord(wp,ws,ap,as);
[b,a]=ellip(N,ap, as,wn,'stop');
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H)))
n=0:1/samp:1;
x=cos(2*pi*200*n)+cos(2*pi*500*n)+cos(2*pi*800*n);
figure
subplot 211, plot(n,abs(fft(x)))
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)))

FIR EQIRIPPLE (OPTIMAL METHOD)


%LPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);

16

ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);
ds=10^(-as/20);
[n,fo,mo,w] = firpmord( [300 400], [1 0], [dp ds], samp );
b = firpm(n,fo,mo,w);
[h,o]=freqz(b,1,256);
figure
plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%HPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);
ds=10^(-as/20);
[n,fo,mo,w] = firpmord( [300 400], [0 1], [ds dp], samp );
b = firpm(n,fo,mo,w);
[h,o]=freqz(b,1,256);
figure
plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%BPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);
ds=10^(-as/20);
[n,fo,mo,w] = firpmord( [300 400 550 650], [0 1 0], [ds dp ds], samp );
b = firpm(n,fo,mo,w);
[h,o]=freqz(b,1,256);
figure
plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%BSF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);

17

ds=10^(-as/20);
[n,fo,mo,w] = firpmord( [300 400 550 650], [1 0 1], [dp ds dp], samp );
b = firpm(n,fo,mo,w);
[h,o]=freqz(b,1,256);
figure
plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));

FIR KAISER WINDOW


%LPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);
ds=10^(-as/20);
[N,W,bta,filtype] = kaiserord( [300 400], [1 0], [dp ds], samp );
w = window(@kaiser,N+1,bta);
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, filtype, kaiser(N+1,bta), 'noscale');
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%HPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);
ds=10^(-as/20);
[N,W,bta,filtype] = kaiserord( [300 400], [0 1], [ds dp], samp );
w = window(@kaiser,N+1,bta);
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, filtype, kaiser(N+1,bta), 'noscale');
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%BPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);

18

ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);
ds=10^(-as/20);
[N,W,bta,filtype] = kaiserord( [300 450 550 650 ], [0 1 0], [ds dp ds], samp );
w = window(@kaiser,N+1,bta);
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, filtype, kaiser(N+1,bta), 'noscale');
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%BSF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
ap=0.5;%ap=-20*log10(1-dp)
as=80;%as=-20*log10(ds)
dp=1-10^(-ap/20);
ds=10^(-as/20);
[N,W,bta,filtype] = kaiserord( [300 450 550 650 ], [1 0 1], [dp ds dp], samp );
w = window(@kaiser,N+1,bta);
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, filtype, kaiser(N+1,bta), 'noscale');
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));

FIR WINDOW based (different windows)


%LPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
N=66;
W=.4;
w=window(@blackmanharris,N+1);%blackman harris window
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, 'low',w);
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%HPF
clc
clear
close all
samp=2000;

19

n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
N=66;
W=.4;
w=window(@hanning,N+1);%Hanning window
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, 'high',w);
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%BPF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
N=66;
W=[.4 .6];
w=window(@gausswin,N+1);%Gaussian window
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, 'bandpass',w);
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));
%BSF
clc
clear
close all
samp=2000;
n=0:1/samp:1;
x=cos(2*pi*100*n)+cos(2*pi*500*n)+cos(2*pi*700*n);
N=66;
W=[.4 .6];
w=window(@Hamming,N+1);%Hamming window
figure
subplot 121, plot(1:N+1,w);
b = fir1(N, W, 'stop');%if nothing is given, the DEFAULT WINDOW=Hamming
[h,o]=freqz(b,1,256);
subplot 122, plot(o/pi, 20*log10(abs(h)));
y=filter(b,1,x);
figure
subplot 211, plot(abs(fft(x)));
subplot 212, plot(abs(fft(y)));

You might also like