dsplab
🧩 Syntax:
SAMPLING THEOREM
clc;
clear all;
fm=input('Enter the Maximum Frequnecy- ');
fs=input('Enter the Sampling Frequency- ');
t=0:0.001:10/fm;
x=cos(2*pi*fm*t);
subplot(3,1,1)
plot(t,x);
title('Continous Sampling Signal');
xlabel('Time(s)');
ylabel ('Amplitude');
n=0:1/fs:10/fm;
y=cos(2*pi*fm*n);
subplot(3,1,2)
stem(n,y);
title('Sampled Signal');
xlabel('N');
ylabel ('Amplitude');
X=interp(y,1);
subplot(3,1,3)
plot(n,X)
title('Regenerated Signal');
xlabel('Time(s)');
ylabel ('Amplitude');
L=length(X);
X1=fft(X);
X2=abs(X1)/L;
X3=2*X2(1:L/2);
f=fs*(0:L/2-1)/L;
figure;
plot(f,X3);
title('Single Sides Frequency Domain Signal')
xlabel('f')
ylabel('|X3(f)|');
IMPULSE
clc;
clear all;
N=input("steps");
b=input(" numerator coeffient");
a=input("denominator coeffient");
n=0:N;
x=[1 zeros(1,N)];
hfilter=filter(b,a,x);
htf=impz(b,a,n);
hsoln=(3*(1/2).^n)+(-2*(1/3).^n);
subplot(3,1,1)
stem(n,hfilter);
xlabel('n');
ylabel("x(n)");
title("hfilter");
subplot(3,1,2);
stem(n,htf);
xlabel('n');
ylabel("x(n)");
title("tranfer func");
subplot(3,1,3);
stem(n,hsoln);
xlabel('n');
ylabel("x(n)");
title("hsoln");
DIFFRENCE
clc;
clear all;
N=input("number of steps");
n=0:N;
x=(0.5).^n;
y(1 :2)=[3 7];
for k=3:N+1
y(k)=3*x(k-1)+5*x(k-2)+5*y(k-1)-6*y(k-2);
end
ysoln=(26/15)*(0.5).^n+(-7/3)*(2).^n+(18/5)*(3).^n;
subplot(2,1,1);
stem(n,y)
title("soln by matlab");
subplot(2,1,2);
stem(n,ysoln);
title("soln by solving")
xlabel("n");
ylabel("x(n");
disp(y);
disp(ysoln);
ODD EVEN FOLDING
clc;
clear all;
x=input('Enter the original sequnce= ')
L=length(x);
n=-(L-1):(L-1);
x11=zeros(1,2*L-1);
x22=zeros(1,2*L-1);
k=L;
for i=1:L;
x11(k)=x(i);
k=k+1;
end
k=L;
for i=L:2*L-1
x22(k)=x11(i)
k=k-1;
end
disp(x22)
subplot(4,1,1)
stem(n,x11)
subplot(4,1,2)
stem(n,x22)
y=(x11+x22)/2;
z=(x11-x22)/2;
disp(y);
disp(z);
subplot(4,1,3)
stem(n,y)
subplot(4,1,4)
stem(n,z)
COMPUTATION OF DFT
clc;
clear all;
x=input('Enter the given sequence= ');
N=length(x);
X=zeros(1,N);
for k=1:N
for n=1:N
X(k)=X(k) + x(n)*exp(-1i*2*pi*(k-1)*(n-1)/N);
end
end
mag=abs(X);
for k=1:N
phase(k)=atan(imag(X(k))/real(X(k)));
end
X1=fft(x,N);
disp(X);
disp(X1);
n=0:N-1;
subplot(2,1,1)
stem(n,mag);
title('Magnitude Plot')
xlabel('n')
ylabel('Magnitude')
subplot(2,1,2)
stem(n,phase);
title('Phase Plot')
xlabel('n')
ylabel('Phase of (x)')
COMPUTATION OF IDFT
clc;
clear all;
X=input('Enter the given sequence= ');
N=length(X);
x=zeros(1,N);
for n=1:N
for k=1:N
x(n)=x(n)+ X(k)*exp(1i*2*pi*(k-1)*(n-1)/N);
end
end
x=x/N;
x1=ifft(X,N);
disp(x);
disp(x1);
CIRCULAR CONV
SUMMATION
clc;
clear all;
x1=input('Enter the 1st Sequence= ');
x2=input('Enter the 2nd Sequence= ');
L=length(x1);
M=length(x2);
N=max(L,M);
if L<N
x1=[x1 zeros(1,N-L)];
else
x2=[x2 zeros(1,N-M)];
end
for i=1:N
if i==1
x3(i)=x2(i);
else
x3(i)=x2(N-i+2);
end
end
for n=1:N
y(n)=sum(x1'.*circshift(x3',n-1));
end
y1=cconv(x1,x2,N);
disp(y);
disp(y1);
MATRIX
clc;
clear all;
x1=input('Enter the 1st Sequence= ');
x2=input('Enter the 2nd Sequence= ');
L=length(x1);
M=length(x2);
N=max(L,M);
if L<N
x1=[x1 zeros(1,N-L)];
else
x2=[x2 zeros(1,N-M)];
end
for i=1:N
A(:,i)=circshift(x1',i-1);
end
y=A*x2';
disp(y);
DFT IDFT
clc;
clear all;
x1=input('Enter the 1st Sequence= ');
x2=input('Enter the 2nd Sequence= ');
L=length(x1);
M=length(x2);
N=max(L,M);
if L<N
x1=[x1 zeros(1,N-L)];
else
x2=[x2 zeros(1,N-M)];
end
X1=fft(x1,N);
X2=fft(x2,N);
Y=X1.*X2;
y=ifft(Y,N);
disp(y)
LINEAR CONV
SUMMATION
clc;
clear all;
x1=input('Enter the 1st Sequence= ');
x2=input('Enter the 2nd Sequence= ');
L=length(x1);
M=length(x2);
N=L+M-1;
x11=zeros(1, 2*L-1);
x22=zeros(1, 2*L-1);
K=L;
for i=1:L
x11(K)=x1(i);
K=K+1;
end
K=L;
for i=1:M
x22(K)=x2(i);
K=K-1;
end
for n=1:N
y(n)=sum(x11'.*circshift(x22',n-1));
end
y1=conv(x1,x2);
disp(y);
disp(y1);
ZERO PADDING
clc;
clear all;
x1=input('enter the first sequence');
x2=input('enter the second sequence');
L=length(x1);
M=length(x2);
N=L+M-1;
x1=[x1,zeros(1,N-L)];
x2=[x2,zeros(1,N-M)];
for i=1:N
A(:,i)=circshift(x1',i-1);
end
y=A*x2';
disp(y);
y1=cconv(x1,x2,N);
disp(y1);
DFT AND IFFT
clc;
clear all;
x1=input('Enter the 1st Sequence= ');
x2=input('Enter the 2nd Sequence= ');
L=length(x1);
M=length(x2);
N=L+M-1;
x1=[x1 zeros(1,(N-L))];
x2=[x2 zeros(1,(N-M))];
X1=fft(x1,N);
X2=fft(x2,N);
Y=X1.*X2;
y=ifft(Y,N);
disp(y)
LOW PASS INVARIANCE BUTTER
clc;
clear all;
wp=0.25*pi;
ws=0.5*pi;
ap=0.5;
as=15;
t=1;
op=wp/t;
os=ws/t;
[N,oc]=buttord(op,os,ap,as,'s');
[b,a]=butter(N,oc,'s');
[bz,az]=impinvar(b,a,1/t);
[h,w]=freqz(bz,az);
plot(w/pi,mag2db(abs(h)));
ylim([-30,5]);
grid on;
title('Low Pass BUTTERWORTH IMPLUSE INVARIANCE TECHNIQUE');
xlabel('Frequency');
ylabel('Gain');
LOW PASS CHEBY BILLI
clc;
clear all;
wp=0.25*pi;
ws=0.55*pi;
ap=1;
as=15;
t=1;
op=(2/t)*tan(wp/2);
os=(2/t)*tan(ws/2);
[N,oc]=cheb1ord(op,os,ap,as,'s');
[b,a]=cheby1(N,ap,op,'s');
[bz,az]=bilinear(b,a,1/t);
[h,w]=freqz(bz,az);
plot(w/pi,mag2db(abs(h)));
ylim([-30,5]);
grid on;
title('Low Pass CHEBYSHEV BILLINEAR TRANSFORMATION TECHNIQUE');
xlabel('Frequency');
ylabel('Gain');
HIGH PASS CHEBY BILLI
clc;
clear all;
f=2000;
fp=700;
fs=500;
wp=(2*pi*fp)/f;
ws=(2*pi*fs)/f;
ap=1;
as=32;
op=(2*f)*tan(wp/2);
os=(2*f)*tan(ws/2);
op1=1;
os1=op/os;
[N,oc]=cheb1ord(op1,os1,ap,as,'s');
[b,a]=cheby1(N,ap,op1,'s');
[bt,at]=lp2hp(b,a,op*op1);
[bz,az]=bilinear(bt,at,f);
[h,w]=freqz(bz,az);
plot((w*f)/(2*pi),mag2db(abs(h)));
ylim([-40,5]);
grid on;
title('High Pass CHEBYSHEV BILLINEAR TRANSFORMATION TECHNIQUE');
xlabel('Frequency');
ylabel('Gain');
BAND PASS BUTTER BILLI
clc;
clear all;
wp1=0.4*pi;
ws1=0.3*pi;
wp2=0.6*pi;
ws2=0.7*pi;
t=2;
op1=(2/t)*tan(wp1/2);
os1=(2/t)*tan(ws1/2);
op2=(2/t)*tan(wp2/2);
os2=(2/t)*tan(ws2/2);
ap=1;
as=40;
bw=op2-op1;
o0=op1*op2;
os11=(op1*op2)/os2;
op=1;
os=(((o0)^2-(os11)^2))/(os11*bw);
[N,oc]=buttord(op,os,ap,as,'s');
[b,a]=butter(N,oc,'s');
[bt,at]=lp2bp(b,a,o0,bw);
[bz,az]=bilinear(bt,at,1/t);
[h,w]=freqz(bz,az);
plot((w)/(pi),mag2db(abs(h)));
ylim([-40,5]);
grid on;
title('Band Pass BILLINEAR TRANSFORMATION TECHNIQUE');
xlabel('Frequency');
ylabel('Gain');
BAND STOP BUTTER BILLI
clc;
clear all;
wp1=0.3*pi;
ws1=0.4*pi;
wp2=0.7*pi;
ws2=0.6*pi;
t=2;
op1=(2/t)*tan(wp1/2);
os1=(2/t)*tan(ws1/2);
op2=(2/t)*tan(wp2/2);
os2=(2/t)*tan(ws2/2);
ap=1;
as=40;
bw=os2-os1;
o0=sqrt(os1*os2);
os=1;
op11=(os1*os2)/op2;
op=(op11*bw)/(((o0)^2-(op11)^2));
[N,oc]=buttord(op,os,ap,as,'s');
[b,a]=butter(N,oc,'s');
[bt,at]=lp2bs(b,a,o0,bw);
[bz,az]=bilinear(bt,at,1/t);
[h,w]=freqz(bz,az);
plot((w)/(pi),mag2db(abs(h)));
ylim([-50,5]);
grid on;
title('Band Stop BILLINEAR TRANSFORMATION TECHNIQUE');
xlabel('Frequency');
ylabel('Gain');
REACTANGULAR WINDOW
clc;
clear all;
N=40;
M=N+1;
wc=0.5;
win=ones(1,M);
b=fir1(N,wc,win);
[H,w]=freqz(b,1);
plot(w/pi,mag2db(abs(H)));
grid on;
title('magnitude response of low pass FIR filter');
xlabel('frequency');
ylabel('magnitude');
FREQUNECY SAMPLING LOW PASS
clc;
clear all;
N=40;
M=N+1;
wc=0.5;0
win=ones(1,M);
f=[0 wc wc 1];
m=[1 1 0 0];
subplot(2,1,1);
plot(f,m);
title('Ideal Frequency Response of Low Pass Filter');
xlabel('Frequency');
ylabel('Magnitude');
b=fir2(N,f,m,win);
[H,w]=freqz(b,1);
subplot(2,1,2);
plot(w/pi,mag2db(abs(H)));
grid on;
title('Frequency Response of Low Pass Filter');
xlabel('Frequency');
ylabel('Magnitude');
HIGPASS WINDOW HANNING
clc;
clear all;
N=34;
M=N+1;
wc=0.48;
n=0:M-1;
win=0.5-0.5*cos(2*pi*n/(M-1));
b=fir1(N,wc,'high',win);
[H,w]=freqz(b,1);
plot(w/pi,mag2db(abs(H)));
grid on;
title('Magnitude response of HIGH pass FIR filter');
xlabel('Frequency');
ylabel('Magnitude');
FREQUENCY SAMPLING HIGH PASS
clc;
clear all;
N=34;
M=N+1;
wc=0.48;
n=0:M-1;
win=0.5-0.5*cos(2*pi*n/(M-1));
f=[0 wc wc 1];
m=[0 0 1 1];
subplot(2,1,1);
plot(f,m);
title('Ideal frequency response of HIGH pass filter')
xlabel('Frequency');
ylabel('Magnitude');
b=fir2(N,f,m,win);
[H,W]=freqz(b,1);
subplot(2,1,2);
plot(W/pi,mag2db(abs(H)));
grid on;
title('Frequency response of given HIGH pass filter');
xlabel('Frequency');
ylabel('Magnitude');
BAND PASS HAMMING WINDOW
clc;
clear all;
N=48;
M=N+1;
wc1=0.35;
wc2=0.65;
n=0:M-1;
win=0.5-0.46*cos(2*pi*n/(M-1));
b=fir1(N,[wc1 wc2],win);
[H,w]=freqz(b,1);
plot(w/pi,mag2db(abs(H)));
grid on;
title('magnitude response of band pass FIR filter');
xlabel('frequency');
ylabel('magnitude');
FREQUNECY SAMPLING BAND PASS
clc;
clear all;
N=48;
M=N+1;
wc1=0.35;
wc2=0.65;
n=0:M-1;
win=0.5-0.46*cos(2*pi*n/(M-1));
f=[0 wc1 wc1 wc2 wc2 1];
m=[0 0 1 1 0 0];
subplot(2,1,1);
plot(f,m);
title('ideal frequency response of band pass filter')
xlabel('frequency');
ylabel('magnitude');
b=fir2(N,f,m,win);
[H,w]=freqz(b,1);
subplot(2,1,2);
plot(w/pi,mag2db(abs(H)));
grid on;
title('frequency response of given band pass filter');
xlabel('frequency');
ylabel('magnitude');
BAND STOP BLACKMAN
clc;
clear all;
N=46;
M=N+1;
wc1=0.42;
wc2=0.75;
n=0:M-1;
win=0.42-0.5*cos(2*pi*n/(M-1))+0.08*cos(4*pi*n/(M-1));
b=fir1(N,[wc1 wc2],'stop',win);
[H,w]=freqz(b,1);
plot(w/pi,mag2db(abs(H)));
grid on;
title('magnitude response of band stop FIR filter');
xlabel('frequency');
ylabel('magnitude');
FREQUNCY SAMPLING BAND STOP
clc;
clear all;
N=46;
M=N+1;
wc1=0.42;
wc2=0.75;
n=0:M-1;
win=0.42-0.5*cos(2*pi*n/(M-1))+0.08*cos(4*pi*n/(M-1));
f=[0 wc1 wc1 wc2 wc2 1];
m=[ 1 1 0 0 1 1];
subplot(2,1,1);
plot(f,m);
title('ideal frequency response of band reject filter')
xlabel('frequency');
ylabel('magnitude');
b=fir2(N,f,m,win);
[H,w]=freqz(b,1);
subplot(2,1,2);
plot(w/pi,mag2db(abs(H)));
grid on;
title('frequency response of given band pass filter');
xlabel('frequency');
ylabel('magnitude');
REALIZzATION CRITICAL FREQUNECY
FIR
clc;
clear all;
b=[-0.0408,0.1103,0.2115,0.2925,0.2925,0.2115,0.1103,-0.0408];
a=[1];
freqz(b,a);
ylim([-30,5]);
IIR
clc;
clear all;
b=[0.0662,0.1986,0.1986,0.0662];
a=[1,-0.9356,0.5617,-0.1016];
freqz(b,a);
ylim([-30,5]);