Matlab
Matlab
Matlab
Expt.No.1
Date:
AIM
To write a MATLAB program to generate various type of
signals.
ALGORITHM
1. Start the program.
4. Get the input value using the ‘input’ function for generating
various sequences.
1
PROGRAM
clc;
clear all;
close all;
2
%Generation of unit impulse signal
t4=-2:1:2;
y4=[zeros(1,2),ones(1,1),zeros(1,2)];
subplot(3,2,4);
stem(t4,y4);
ylabel('Amplitude---->');
xlabel('(d)n---->');
3
OUTPUT
Enter the length of exponential sequence: 4
Enter the value: 4
Enter the N value for unit step sequence: 6
Enter the length of ramp sequence: 5
4
RESULT
Thus the MATLAB program for generation of various types
of signals was written and the waveforms were obtained.
5
LINEAR CONVOLUTION OF TWO GIVEN SEQUENCES
Expt No.2
Date:
AIM
To write a MATLAB program to perform linear convolution.
ALGORITHM
6
PROGRAM
%linear convolution of two sequences
clc;
clear all;
close all;
x=input('Enter the first sequence : ');
h=input('Enter the second sequence : ');
y=conv(x,h);
figure;
subplot(3,2,1);
stem(x);
ylabel('Amplitude---->');
xlabel('(a)n---->');
subplot(3,2,2);
stem(h);
ylabel('Amplitude---->');
xlabel('(b)n---->');
subplot(3,2,3);
stem(y);
ylabel('Amplitude---->');
xlabel('(c)n---->');
disp('The resultant signal is:');y
7
OUTPUT
Enter the first sequence : [1 2 3 4 5]
Enter the second sequence : [1 1 -2]
The resultant signal is:
y=
1 3 3 3 3 -3 -10
8
RESULT
Thus the matlab program to perform linear convolution was
written and the output was obtained.
9
CIRCULAR CONVOLUTION OF TWO GIVEN SEQUENCES
Expt No.3
Date:
AIM
To write a MATLAB program to perform circular convolution.
ALGORITHM
10
PROGRAM
%circular convolution of two sequences
clc; clear all; close all;
g=input('Enter the first sequence: ');
h=input('Enter the second sequence: ');
N1=length(g);
N2=length(h);
N=max(N1,N2);
N3=N1-N2;
%loop for generating equal length
if(N3>=0)
h=[h,zeros(1,N3)];
else
g=[g,zeros(1,-N3)];
end
%computation of circular convolution
for n=1:N
y(n)=0;
for i=1:N
j=n-i+1;
if(j<=0)
j=N+j;
end
y(n)=y(n)+g(i)*h(j);
end
end
subplot(3,2,1);
stem(g);
ylabel('Amplitude---->');
xlabel('(a)n---->');
subplot(3,2,2);
stem(h);
ylabel('Amplitude---->');
xlabel('(b)n---->');
subplot(3,2,3);
stem(y);
ylabel('Amplitude---->');
xlabel('(c)n---->');
disp('The resultant signal is; ');y
11
OUTPUT
Enter the first sequence: [1 2 3 4 5]
Enter the second sequence: [5 4 3 2 1]
The resultant signal is;
y=
45 40 40 45 55
12
RESULT
Thus the matlab program to perform circular convolution was
written and the output was obtained.
13
DFT OF A GIVEN SEQUENCE USING FFT
Expt No.4
Date:
AIM
To write a matlab program to perform DFT of a given
sequence using FFT.
ALGORITHM
14
PROGRAM
%fft of two sequences
clc;
clear all;
close all;
x=input('Enter the sequence : ');
n=input('Enter the n value : ');
y=fft(x,n);
figure;
subplot(3,2,1);
stem(x);
ylabel('Amplitude---->');
xlabel('(a)n---->');
subplot(3,2,2);
stem(real(y));
ylabel('Amplitude---->');
xlabel('(b)n---->');
subplot(3,2,3);
stem(imag(y));
ylabel('Amplitude---->');
xlabel('(c)n---->');
disp('The resultant signal is:');y
15
OUTPUT
Enter the sequence : [.5 0 .5 0 .5 0 .5 0]
Enter the n value : 8
The resultant signal is:
y=
2 0 0 0 2 0 0 0
16
RESULT
Thus the matlab program to perform DFT of a given
sequence using FFT was written and the output was
obtained.
17
IDFT OF A GIVEN SEQUENCE USING IFFT
Expt No.5
Date:
AIM
To write a matlab program to perform IDFT of a given
sequence using IFFT.
ALGORITHM
18
PROGRAM
%ifft of two sequences
clc; clear all; close all;
x=input('Enter the sequence : ');
n=input('Enter the n value : ');
y=ifft(x,n);
figure;
subplot(3,2,1);
stem(real(x));
ylabel('Amplitude---->');
xlabel('(a)n---->');
subplot(3,2,2);
stem(imag(x));
ylabel('Amplitude---->');
xlabel('(b)n---->');
subplot(3,2,3);
stem(y);
ylabel('Amplitude---->');
xlabel('(c)n---->');
disp('The resultant signal is:');y
19
OUTPUT
Enter the sequence : [20 -5.8284-2.4142j 0 -.1716-.4142j 0 -.1716+.4142j 0
-5.8284+2.4142j]
Enter the n value : 8
The resultant signal is:
y=
1.0000 2.0000 3.0000 4.0000 4.0000 3.0000 2.0000 1.0000
20
RESULT
Thus the matlab program to perform IDFT of a given
sequence using IFFT was written and the output was
obtained.
21
Expt No.6
Date:
AIM
To write a matlab program to design a Butterworth lowpass
filter and to verify the output.
ALGORITHM
PROGRAM
clc;
close all;
22
clear all;
wp=input('Enter the passband frequency: ');
ws=input('Enter the stopband frequency: ');
rp=input('Enter the passband ripple : ');
rs=input('Enter the passband ripple : ');
fs=input('Enter the sampling frequency: ');
w1=2*wp/fs;
w2=2*ws/fs;
[N,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(N,wn);
w=0:.01:pi;
[h,omega]=freqz(b,a,w);
gain=20*log10(abs(h));
subplot(2,2,1);
plot(omega/pi,gain);
grid;
xlabel('\omega/pi,gain');
ylabel('Gain in db---->');
title('IIR Butterworth LPF: gain');
subplot(2,2,2);
an=angle(h);
plot(omega/pi,an);
grid;
xlabel('\omega/pi,gain');
ylabel('Phase in radians---->');
title('IIR Butterworth LPF: phase');
disp('Order of the filter is: ');N
OUTPUT
Enter the passband frequency: 20
23
Enter the stopband frequency: 30
Enter the passband ripple : 2
Enter the passband ripple : 10
Enter the sampling frequency: 100
Order of the filter is:
N=
24
RESULT
Thus the matlab program to design a Butterworth lowpass
filter was written and the output was obtained.
25
Expt No.7
Date:
AIM
To write a matlab program to design a Butterworth highpass
filter and to verify the output.
ALGORITHM
PROGRAM
clc;
close all;
clear all;
26
wp=input('Enter the passband frequency: ');
ws=input('Enter the stopband frequency: ');
rp=input('Enter the passband ripple : ');
rs=input('Enter the passband ripple : ');
fs=input('Enter the sampling frequency: ');
w1=2*wp/fs;
w2=2*ws/fs;
[N,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(N,wn,'high');
w=0:.01:pi;
[h,omega]=freqz(b,a,w);
gain=20*log10(abs(h));
subplot(2,2,1);
plot(omega/pi,gain);
grid;
xlabel('\omega/pi,gain');
ylabel('Gain in db---->');
title('IIR Butterworth HPF: gain');
subplot(2,2,2);
an=angle(h);
plot(omega/pi,an);
grid;
xlabel('\omega/pi,gain');
ylabel('Phase in radians---->');
title('IIR Butterworth HPF: phase');
disp('Order of the filter is: ');N
OUTPUT
Enter the passband frequency: 30
Enter the stopband frequency: 20
27
Enter the passband ripple : 2
Enter the passband ripple : 10
Enter the sampling frequency: 100
Order of the filter is:
N=
28
RESULT
Thus the matlab program to design a Butterworth highpass
filter was written and the output was obtained.
Expt No.8
29
Date:
AIM
To write a matlab program to design a Butterworth bandpass
filter and to verify the output.
ALGORITHM
PROGRAM
clc;
close all;
clear all;
wp=input('Enter the passband frequency: ');
30
ws=input('Enter the stopband frequency: ');
rp=input('Enter the passband ripple : ');
rs=input('Enter the passband ripple : ');
fs=input('Enter the sampling frequency: ');
w1=2*wp/fs;
w2=2*ws/fs;
[N,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(N,wn);
w=0:.01:pi;
[h,omega]=freqz(b,a,w);
gain=20*log10(abs(h));
subplot(2,2,1);
plot(omega/pi,gain);
grid;
xlabel('\omega/pi,gain');
ylabel('Gain in db---->');
title('IIR Butterworth BPF: gain');
subplot(2,2,2);
an=angle(h);
plot(omega/pi,an);
grid;
xlabel('\omega/pi,gain');
ylabel('Phase in radians---->');
title('IIR Butterworth BPF: phase');
disp('Order of the filter is: ');N
OUTPUT
Enter the passband frequency: [40 65]
Enter the stopband frequency: [30 75]
Enter the passband ripple : 1
Enter the passband ripple : 40
31
Enter the sampling frequency: 200
Order of the filter is:
N=
32
RESULT
Thus the matlab program to design a Butterworth bandpass
filter was written and the output was obtained.
Expt No.9
33
Date:
AIM
To write a matlab program to design a Butterworth bandstop
filter and to verify the output.
ALGORITHM
PROGRAM
clc;
close all;
clear all;
wp=input('Enter the passband frequency: ');
ws=input('Enter the stopband frequency: ');
34
rp=input('Enter the passband ripple : ');
rs=input('Enter the passband ripple : ');
fs=input('Enter the sampling frequency: ');
w1=2*wp/fs;
w2=2*ws/fs;
[N,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(N,wn,'stop');
w=0:.01:pi;
[h,omega]=freqz(b,a,w);
gain=20*log10(abs(h));
subplot(2,2,1);
plot(omega/pi,gain);
grid;
xlabel('\omega/pi,gain');
ylabel('Gain in db---->');
title('IIR Butterworth BSF: gain');
subplot(2,2,2);
an=angle(h);
plot(omega/pi,an);
grid;
xlabel('\omega/pi,gain');
ylabel('Phase in radians---->');
title('IIR Butterworth BSF: phase');
disp('Order of the filter is: ');N
OUTPUT
Enter the passband frequency: [30 75]
Enter the stopband frequency: [45 65]
Enter the passband ripple : 1
Enter the passband ripple : 40
35
Enter the sampling frequency: 200
Order of the filter is:
N=
36
RESULT
Thus the matlab program to design a Butterworth bandstop
filter was written and the output was obtained.
Expt No.10
37
Date:
AIM
To write a matlab program to design a chebyshev-I lowpass
filter and to verify the output.
ALGORITHM
PROGRAM
clc;
close all;
clear all;
wp=input('Enter the passband frequency: ');
38
ws=input('Enter the stopband frequency: ');
rp=input('Enter the passband ripple : ');
rs=input('Enter the stopband ripple : ');
fs=input('Enter the sampling frequency: ');
w1=2*wp/fs;
w2=2*ws/fs;
[N,wn]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(N,rp,wn);
w=0:.01:pi;
[h,omega]=freqz(b,a,w);
gain=20*log10(abs(h));
subplot(2,2,1);
plot(omega/pi,gain);
grid;
xlabel('\omega/pi,gain');
ylabel('Gain in db---->');
title('IIR Chebyshev-I LPF: gain');
subplot(2,2,2);
an=angle(h);
plot(omega/pi,an);
grid;
xlabel('\omega/pi,gain');
ylabel('Phase in radians---->');
title('IIR Chebyshev LPF: phase');
disp('Order of the filter is: ');N
OUTPUT
Enter the passband frequency: 30
Enter the stopband frequency: 80
Enter the passband ripple : 20
Enter the stopband ripple : 300
Enter the sampling frequency: 200
Order of the filter is:
39
N=
14
40
RESULT
Thus the matlab program to design a chbyshev-I lowpass
filter was written and the output was obtained.
Expt No.11
41
Date:
AIM
To write a matlab program to design a chebyshev-II lowpass
filter and to verify the output.
ALGORITHM
PROGRAM
clc;
close all;
clear all;
wp=input('Enter the passband frequency: ');
ws=input('Enter the stopband frequency: ');
42
rp=input('Enter the passband ripple : ');
rs=input('Enter the stopband ripple : ');
fs=input('Enter the sampling frequency: ');
w1=2*wp/fs;
w2=2*ws/fs;
[N,wn]=cheb2ord(w1,w2,rp,rs);
[b,a]=cheby2(N,rp,wn);
w=0:.01:pi;
[h,omega]=freqz(b,a,w);
gain=20*log10(abs(h));
subplot(2,2,1);
plot(omega/pi,gain);
grid;
xlabel('\omega/pi,gain');
ylabel('Gain in db---->');
title('IIR Chebyshev LPF: gain');
subplot(2,2,2);
an=angle(h);
plot(omega/pi,an);
grid;
xlabel('\omega/pi,gain');
ylabel('Phase in radians---->');
title('IIR Chebyshev LPF: phase');
disp('Order of the filter is: ');N
OUTPUT
Enter the passband frequency: 90
Enter the stopband frequency: 60
Enter the passband ripple : 100
Enter the passband ripple : 250
Enter the sampling frequency: 200
Order of the filter is:
43
N=
44
RESULT
Thus the matlab program to design a chebyshev-II lowpass
filter was written and the output was obtained.
Expt No. 12
Date:
45
AIM
To write a matlab program to design a FIR filter using
rectangular window and to verify the output.
ALGORITHM
PROGRAM
%lowpass filter
b=fir1(n,wp,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
%highpass filter
b=fir1(n,wp,'high',y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
%bandpass filter
wn=[wp ws];
b=fir1(n,wn,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);
plot(om/pi,m);
ylabel('Gain in dB---->');
47
xlabel('Frequency in rad/sec---->');
%bandstop filter
b=fir1(n,wn,'stop',y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
OUTPUT
Enter the passband ripple: .05
Enter the stopband ripple: .03
Enter the passband frequency: 1300
Enter the stopband frequency: 1600
Enter the sampling frequency: 7400
The order is:
48
n=
26
49
RESULT
Thus the matlab program to design a FIR filter using
rectangular window was written and the output was verified.
Expt No. 13
Date:
50
AIM
To write a matlab program to design a FIR filter using
hamming window and to verify the output.
ALGORITHM
PROGRAM
%lowpass filter
b=fir1(n,wp,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
%highpass filter
b=fir1(n,wp,'high',y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
%bandpass filter
wn=[wp ws];
b=fir1(n,wn,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,3);
plot(om/pi,m);
ylabel('Gain in dB---->');
52
xlabel('Frequency in rad/sec---->');
%bandstop filter
b=fir1(n,wn,'stop',y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
OUTPUT
Enter the passband ripple: .05
Enter the stopband ripple: .03
Enter the passband frequency: 1300
Enter the stopband frequency: 1600
Enter the sampling frequency: 7400
The order is:
53
n=
26
54
RESULT
Thus the matlab program to design a FIR filter using
hamming window was written and the output was verified.
Expt No. 14
Date:
55
AIM
To write a matlab program to design a FIR filter using
Kaiser window and to verify the output.
ALGORITHM
PROGRAM
clc;
close all;
clear all;
rp=input('Enter the passband ripple: ');
56
rs=input('Enter the stopband ripple: ');
fp=input('Enter the passband frequency: ');
fs=input('Enter the stopband frequency: ');
f=input('Enter the sampling frequency: ');
beta=input('Enter the beta value: ');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
disp('The order is:');n
y=kaiser(n1,beta);
%lowpass filter
b=fir1(n,wp,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
%highpass filter
b=fir1(n,wp,'high',y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
%bandpass filter
wn=[wp ws];
b=fir1(n,wn,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
57
subplot(2,2,3);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
%bandstop filter
b=fir1(n,wn,'stop',y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,4);
plot(om/pi,m);
ylabel('Gain in dB---->');
xlabel('Frequency in rad/sec---->');
OUTPUT
Enter the passband ripple: .05
Enter the stopband ripple: .03
Enter the passband frequency: 1300
Enter the stopband frequency: 1600
Enter the sampling frequency: 7400
Enter the beta value: 7
The order is:
58
n=
26
59
RESULT
Thus the matlab program to design a FIR filter using Kaiser
window was written and the output was verified.
Expt No. 15
Date:
60
AIM
To write a matlab program to design an IIR filter using
bilinear transformation technique and to verify the output.
ALGORITHM
PROGRAM
%bilinear transform
clc;
close all;
clear all;
61
pin=input('Enter the poles: ');
zin=input('Enter the zeros: ');
t=input('Enter the sampling interval:');
[z,p]=bilinear(zin,pin,1/t)
OUTPUT
62
z=
p=
RESULT
Thus the matlab program for design of IIR filter using bilinear
transformation was written and the output was verified.
Expt No. 16
63
Date:
AIM
To write a matlab program to design an IIR filter using
impulse invariance technique and to verify the output.
ALGORITHM
PROGRAM
64
%impulse invariance
clc;
close all;
clear all;
pin=input('Enter the poles: ');
zin=input('Enter the zeros: ');
t=input('Enter the sampling interval:');
[z,p]=impinvar(zin,pin,1/t)
OUTPUT
65
Enter the sampling interval:1
z=
0 0.4651
p=
RESULT
Thus the matlab program for design of IIR filter using impulse
invariance was written and the output was verified.
66