Matlab
Matlab
1.Write a program to generate impulse sequence, step sequence, ramp sequence, sinusoidal
sequence, cosine sequence and sawtooth sequence.
Program:
clc;
clear all;
close all;
%impulse signal
disp('unit impulse function');
N= input('enter the value of N:');
n=-N:N;
y=[zeros(1,N),1,zeros(1,N)];
subplot(3,2,1);
stem(n,y);
ylabel('amplitude---->');
xlabel('time---->');
title('unit impulse signal');
%step signal
disp('unit step function');
N= input('enter the value of N:');
n=-N:1:N;
y=[zeros(1,N),ones(1,N+1)];
subplot(3,2,2);
stem(n,y);
ylabel('amplitude---->');
xlabel('time---->');
title('unit step signal');
%ramp signal
disp('unit ramp function');
N= input('enter the length of ramp sequence:');
n=0:N-1;
y=n;
subplot(3,2,3);
stem(n,y);
ylabel('amplitude---->');
xlabel('n---->');
title('Ramp sequence');
%sinusoidal signal
disp('sine function');
f= input('enter the value of frequency:');
n=-2*pi:pi/12:2*pi;
y=sin(2*pi*f*n);
subplot(3,2,4);
stem(n,y);
ylabel('amplitude---->');
xlabel('n---->');
title('sine sequence');
%cosine signal
disp('cosine function');
f= input('enter the value of frequency:');
n=-2*pi:pi/12:2*pi;
y=cos(2*pi*f*n);
subplot(3,2,6);
stem(n,y);
ylabel('amplitude---->');
xlabel('n---->');
title('cosine sequence');
%sawtooth function
disp('sawtooth function');
f=input('enter the value of f:');
N= input('enter the value of N:');
n=-N:N;
y=sawtooth(2*pi*f*n);
subplot(3,2,5);
stem(n,y);
ylabel('amplitude---->');
xlabel('n---->');
title('sawtooth sequence');
Command Window:
unit impulse function
enter the value of N:5
unit step function
enter the value of N:8
unit ramp function
enter the length of ramp sequence:9
sine function
enter the value of frequency:0.1
cosine function
enter the value of frequency:0.1
sawtooth function
enter the value of f:0.1
enter the value of N:5
Program-2
2. Write a program to generate real exponential sequence and complex exponential sequence.
Program:
%Exponential function
Program:
clc;
close all;
clear all;
n=0:50;A=2;B=3;
x1=cos(2*pi*(0.1)*n);
x2=cos(2*pi*(0.4)*n);
b=[1 1/2 1/4];
a=[1];
y1=filter(b,a,x1);
y2=filter(b,a,x2);
y31=(A*y1)+(B*y2);
x3=(A*x1)+(B*x2);
y3=filter(b,a,x3);
y=round(y3)-round(y31);
if y==0
display('The given system is linear');
else
display('System is Non-linear');
end
Command Window:
4. Write a program to check whether the given system time invariant or not
Y[n]=x[n]+1/2x[n-2]+1/4x[n-4]
Program:
clc;
close all;
clear all;
n=0:50;
x=cos(2*pi*0.1*n);
b=[1 1/2 1/4];
a=[1];
y=filter(b,a,x);
k=10;
nk=n-10;
xnk=cos(2*pi*0.1*nk);
ynk=filter(b,a,xnk);
ys=y;
if round(ynk)==round(ys)
display('Given system is Time invariant');
else
display('Given system is Time variant');
end
Command Window:
Given system is Time invariant
Program-5
5.Write a program to find the impulse response of the system defined by the difference equation
Y[n]+y[n-1]-0.06y[n-2]=x[n]+2x[n-1]
Program:
clc;
close all;
clear all;
N=input('Enter length of sequence:');
n=0:N-1;
b=input('Enter the x coefficients:');
a=input('Enter the y coefficients:');
x=[1,zeros(1,N-1)];
y=filter(b,a,x);
subplot(2,1,1);
stem(n,y);
xlabel('n---->');
ylabel('Amplitude---->');
title('Impluse Response');
%in built function
i=impz(b,a,N);
subplot(2,1,2);
stem(n,i);
xlabel('n---->');
ylabel('Amplitude---->');
title('Impluse Response');
Command Window:
Enter length of sequence:50
6.Write a program to find the frequency response of the given system defined by the difference
equation
Y[n]+y[n-1]-0.06y[n-2]=x[n]+2x[n-1]
Program:
clc;
clear all;
close all;
N= input('enter the length of sequence:');
b= input('enter the coefficients of x:');
a= input('enter the coefficients of y:');
[h,w]= freqz(b,a,N);
m= abs(h);
mdb= 20.*log10(m);
p= angle(h);
subplot(2,2,1);
plot(w/pi,mdb);
xlabel('amplitude------->');
ylabel('n------>');
title('magnitude response');
subplot(2,2,2);
plot(w/pi,p);
xlabel('amplitude------->');
ylabel('n------>');
title('phase response');
Command Window:
enter the length of sequence: 50
enter the coefficients of x: [1 2]
7.Write a program to find the frequency response of the system defined by difference equation
Y[n]+y[n-1]-0.06y[n-2]=x[n]+2x[n-1]
Program:
clc;
close all;
clear all;
x1=input('enter the x1 seq:');
x2=input('enter the x2 seq:');
s1=input('enter the starting index of x1 seq:');
s2=input('enter the starting index of x2 seq:');
l1=length(x1);
l2=length(x2);
e1=s1+l1-1;
e2=s2+l2-1;
n1=s1:e1;
n2=s2:e2;
subplot(2,2,1);
stem(n1,x1);
ylabel('amplitude---->');
xlabel('n----->');
title("x1 sequence");
subplot(2,2,2);
stem(n2,x2);
ylabel('amplitude---->');
xlabel('n----->');
title("x2 sequence");
p=[x1,zeros(1,l2-1)];
q=[x2,zeros(1,l1-1)];
l=l2+l1-1;
s=s1+s2;
e=s+l-1;
n=s:e;
for i=1:l
y(i)=0;
for j=1:l;
if(i-j+1>0)
y(i)=y(i)+p(j)*q(i-j+1);
end
end
end
disp(y(i));
subplot(2,2,3);
stem(n,y);
ylabel('amplitude---->');
xlabel('n----->');
title('convoluted sequence');
%convolution using function
y1=conv(x2,x1);
disp(y1);
subplot(2,2,4);
stem(n,y1);
ylabel('amplitude---->');
xlabel('n----->');
title('convoluted sequence using function');
Command Window:
3 8 8 12 9 4 4
Program-8
8.Write a program to find the circular convolution of two discrete sequences using for loop and
MATLAB inbuilt function.
Program:
clc;
close all;
clear all;
x1=input('Enter the first sequence:');
x2=input('Enter the second sequence:');
s1=input('Enter the starting index of x1:');
s2=input('Enter the starting index of x2:');
l1=length(x1);
l2=length(x2);
e1=s1+l1-1;
e2=s2+l2-1;
n1=s1:e1;
n2=s2:e2;
subplot(2,2,1);
stem(n1,x1);
ylabel('Amplitude');
xlabel('n');
title('x1 sequence');
subplot(2,2,2);
stem(n2,x2);
ylabel('Amplitude');
xlabel('n');
title('x2 sequence');
N=max(l1,l2);
x1=[x1,zeros(1,N-l1)];
x2=[x2,zeros(1,N-l2)];
s=s1+s2;
e=s+N-1;
n=s:e;
for i=1:N
y(i)=0;
for j=1:N
k=i-j+1;
if(k<=0)
k=N+k;
end
y(i)=y(i)++x1(j)*x2(k);
end;
end;
subplot(2,2,3);
stem(n,y);
ylabel('Amplitude');
xlabel('n');
title('circular convoluted sequence');
c=cconv(x1,x2,N);
subplot(2,2,4);
stem(n,c);
ylabel('Amplitude');
xlabel('n');
title('circular convolution sequencecusing inbuilt function');
Command Window:
9.Write a program to find the 8-Point Discrete Fourier Transform and Inverse Discrete Fourier
Transform of a sequence.
Program:
clc;
close all;
clear all;
N=input("Enter the value of N:");
x=input('Enter the input sequence:');
x=[x,zeros(1,N-length(x))];
for i=1:N
for j=1:N
w(i,j)=exp(-1i*2*pi*(i-1)*(j-1)-(1/N));
end;
end;
%Dft of sequence X=w,x
XC=w*transpose(x);
XR=transpose(XC);
display(XR);
%idft of sequence
xc=(1/N)*transpose(conj(w))*XC;
xr=transpose(xc);
display(xr);
%inbuilt function
X=fft(x,N);
ix=ifft(X,N);
display(X);
display(ix);
Command Window:
Enter the value of N:8
Enter the input sequence:[1 2 3 4]
XR =
Columns 1 through 4
Columns 5 through 8
8.8250 + 0.0000i 8.8250 + 0.0000i 8.8250 + 0.0000i 8.8250 + 0.0000i
X=
Columns 1 through 4
Columns 5 through 8
ix =
xr =
Columns 1 through 4
Columns 5 through 8
Command Window:
Enter the sampling frequency in Hz:10000
Enter the passband frequency in Hz:2000
Enter the stopband frequency in Hz:4000
Enter the passband attenuation in db:3
Enter the stopband attenuation in db:6
Program-11
11.Write a program to plot frequency response of a High Pass Filter to give response of 3dB or
less frequencies upto 4KHz and attenuation of 6dB or more beyond 2KHz and sampled at 10KHz
for butterworth, Chebyshev type-1,2 filters.
Program:
clc;
close all;
clear all;
fsam=input('Enter the sampling frequency in Hz:');
fp=input('Enter the passband frequency in Hz:');
fs=input('Enter the stopband frequency in Hz:');
Rp=input('Enter the passband attenuation in db:');
Rs=input('Enter the stopband attenuation in db:');
% Normalized frequencies
wp=(2*fp)/fsam;
ws=(2*fs)/fsam;
% Order and Cutoff frequencies of filters
[n1,w1]=buttord(wp,ws,Rp,Rs);
[n2,w2]=cheb1ord(wp,ws,Rp,Rs);
[n3,w3]=cheb2ord(wp,ws,Rp,Rs);
% High pass filter transfer function
[b1,a1]=butter(n1,w1,'high');
[b2,a2]=cheby1(n2,Rp,wp,'high');
[b3,a3]=cheby2(n3,Rs,ws,'high');
% Frequency response of high pass filter
% butterworth filter
[h1,w1]=freqz(b1,a1);
mdb1=20*log10(abs(h1));
p1=angle(h1);
subplot(3,2,1);
plot(w1/pi,mdb1);
ylabel('Magnitude');
xlabel('w');
title('Magnitude response of high pass butterworth filter');
subplot(3,2,2);
plot(w1/pi,p1);
ylabel('Phase');
xlabel('w');
title('Phase response of high pass butterworth filter');
% Chebyshev type-1 filter
[h2,w2]=freqz(b2,a2);
mdb2=20*log10(abs(h2));
p2=angle(h2);
subplot(3,2,3);
plot(w2/pi,mdb2);
ylabel('Magnitude');
xlabel('w');
title('Magnitude response of high pass chebyshev type-1 filter');
subplot(3,2,4);
plot(w2/pi,p2);
ylabel('Phase');
xlabel('w');
title('Phase response of high pass chebyshev type-1 filter');
% Chebyshev type-2 filter
[h3,w3]=freqz(b3,a3);
mdb3=20*log10(abs(h3));
p3=angle(h3);
subplot(3,2,5);
plot(w3/pi,mdb3);
ylabel('Magnitude');
xlabel('w');
title('Magnitude response of high pass chebyshev type-2 filter');
subplot(3,2,6);
plot(w3/pi,p3);
ylabel('Phase');
xlabel('w');
title('Phase response of high pass chebyshev type-2 filter');
Command Window:
(a)Rectangular (b)Hamming
Program:
clc;
close all;
clear all;
l=input('Enter the length of the filter:');
n=l-1;
wn=input('enter the cut off frequency in hz:');
%rectangular window
wr=rectwin(l);
b=fir1(n,wn,'low',wr);
[h1,w1]=freqz(b,1);
mdb1=20*log10(abs(h1));
p1=angle(h1);
subplot(5,2,1);
plot(w1/pi,mdb1);
ylabel('magnitude--->');
xlabel('w--->');
title('magnitude response of lpf using rectangular window');
subplot(5,2,2);
plot(w1/pi,p1);
ylabel('magnitude--->');
xlabel('w--->');
title('phase response of lpf using rectangular window');
% hamming window
wh=hamming(l);
b=fir1(n,wn,'low',wh);
[h3,w3]=freqz(b,1);
mdb3=20*log10(abs(h3));
p3=angle(h3);
subplot(5,2,5);
plot(w3/pi,mdb3);
ylabel('magnitude--->');
xlabel('w--->');
title('magnitude response of lpf using hamming window');
subplot(5,2,6);
plot(w3/pi,p3);
ylabel('magnitude--->');
xlabel('w--->');
title('phase response of lpf using hamming window');
Command Window:
Enter the length of the filter:25
enter the cut off frequency in hz:0.5
Program-13
13. Write a program to design a 25 tap high pass filter with cut off frequency 0.5 pi radians
using
(a)Rectangular (b)Hamming
Program:
clc;
close all;
clear all;
l=input('Enter the length of the filter:');
n=l-1;
wn=input('enter the cut off frequency in hz:');
%rectangular window
wr=rectwin(l);
b=fir1(n,wn,'high',wr);
[h1,w1]=freqz(b,1);
mdb1=20*log10(abs(h1));
p1=angle(h1);
subplot(5,2,1);
plot(w1/pi,mdb1);
ylabel('magnitude--->');
xlabel('w--->');
title('magnitude response of hpf using rectangular window');
subplot(5,2,2);
plot(w1/pi,p1);
ylabel('magnitude--->');
xlabel('w--->');
title('phase response of hpf using rectangular window');
% hamming window
wh=hamming(l);
b=fir1(n,wn,'high',wh);
[h3,w3]=freqz(b,1);
mdb3=20*log10(abs(h3));
p3=angle(h3);
subplot(5,2,5);
plot(w3/pi,mdb3);
ylabel('magnitude--->');
xlabel('w--->');
title('magnitude response of hpf using hamming window');
subplot(5,2,6);
plot(w3/pi,p3);
ylabel('magnitude--->');
xlabel('w--->');
title('phase response of hpf using hamming window');
Command Window:
14. Write a program to design a FIR low pass filter with a pass band cut off frequency of 1500Hz,
a stop band cut off frequency of 2000Hz, pass band ripple of 0.01dB, stop band ripple of 0.1 and
a sampling frequency of 8000Hz.
Program:
clc;
close all;
clear all;
fs=input('enter the sampling frequency in Hz:');
f=input('enter the cutoff frequency in Hz:');
a=input('enter the magnitute values:');
dev=input('enter the ripples');
%low pass filter using kaiser window
[n,wn,beta,ftype]=kaiserord(f,a,dev,fs);
wk=kaiser(n+1,beta);
b=fir1(n,wn,ftype,wk,'noscale');
[h,w]=freqz(b,1);
mdb=20*log10(abs(h));
p=angle(h);
subplot(2,1,1);
plot(w/pi,mdb);
ylabel('magnitude');
xlabel('w');
title('magnitude response of low pass filter using kaiser window');
subplot(2,1,2);
plot(w/pi,p);
ylabel('phase');
xlabel('w');
title('phase response of low pass filter using kaiser window');
Command Window:
Program:
clc;
close all;
clear all;
fs=input('enter the sampling frequency in Hz:');
f=input('enter the cutoff frequency in Hz:');
a=input('enter the magnitute values:');
dev=input('enter the ripples');
%high pass filter using kaiser window
[n,wn,beta,ftype]=kaiserord(f,a,dev,fs);
wk=kaiser(n+1,beta);
b=fir1(n,wn,ftype,wk,'noscale');
[h,w]=freqz(b,1);
mdb=20*log10(abs(h));
p=angle(h);
subplot(2,1,1);
plot(w/pi,mdb);
ylabel('magnitude');
xlabel('w');
title('magnitude response of low pass filter using kaiser window');
subplot(2,1,2);
plot(w/pi,p);
ylabel('phase');
xlabel('w');
title('phase response of low pass filter using kaiser window');
Command Window: