Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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)
internally
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')
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)))
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)))
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)))
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)));
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)));
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)));