You are on page 1of 22

MATLAB EXPERIMENTS

LAB FILE

EC 453

Name Siddharth Singh


Roll 02516412813
B.Tech (ECE) 7th Sem

INDEX
TOPIC

SR.
1.

Butterworth Analog Low Pass Filter

2.

Butterworth Analog Band Stop Filter

Butterworth Analog Band Pass Filter

4.

Butterworth Analog High Pass Filter

5.

FIR Filters Using Kaiser Window

6.

Linear Convolution

7.

Circular Convolution

8.

DFT & IDFT

9.

DTFT

SIGN

02516412813

1. Butterworth Analog Low Pass Filter Using MATLAB Function Program


Program
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n,wn] = buttord(w1,w2,rp,rs,'s');
[z,p,k] = butter(n,wn);
[b,a] = zp2tf(z,p,k);
[b,a] = butter(n,wn,'s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('Amplitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised frequency ---->');
ylabel('Phase in radians ---->');
grid on;

02516412813

3
Output:
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the

passband
stopband
passband
stopband
sampling

ripple = 0.15
ripple = 60
frequency = 1500
frequency = 3000
frequency = 7000

Waveform:

02516412813

2. Butterworth Analog Band Stop Filter Using MATLAB Function

Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n] = buttord(w1,w2,rp,rs,'s');
wn = [w1 w2];
[b,a] = butter(n,wn,'stop','s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
title('Magnitude Response');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
subplot(2,1,2);
plot(om/pi,an);
title('Phase Response');
xlabel('Normalised Frequency ---->');
ylabel('Phase in radians ---->');
grid on;

02516412813

5
Output:
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the

passband
stopband
passband
stopband
sampling

ripple = 0.28
ripple = 28
frequency = 1000
frequency = 1400
frequency = 5000

Waveform:

02516412813

3. Butterworth Analog Band Pass Filter Using MATLAB Function

Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n] = buttord(w1,w2,rp,rs);
wn = [w1 w2];
[b,a] = butter(n,wn,'bandpass','s');
w = 0:.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
title('Magnitude Response');
grid on;
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised Frequency ---->');
ylabel('Phase in Radians ---->');
title('Phase Response');
grid on;

02516412813

7
Output:
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the

passband
stopband
passband
stopband
sampling

ripple = 0.36
ripple = 36
frequency = 1500
frequency = 2000
frequency = 6000

Waveform:

02516412813

4. Butterworth Analog High Pass Filter Using MATLAB Function


Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
rs = input('Enter the stopband ripple = ');
wp = input('Enter the passband frequency = ');
ws = input('Enter the stopband frequency = ');
fs = input('Enter the sampling frequency = ');
w1 = 2*wp/fs;
w2 = 2*ws/fs;
[n,wn] = buttord(w1,w2,rp,rs,'s');
[b,a] = butter(n,wn,'high','s');
w = 0:0.01:pi;
[h,om] = freqs(b,a,w);
m = 20*log10(abs(h));
an = angle(h);
subplot(2,1,1);
plot(om/pi,m);
ylabel('Gain in dB ---->');
xlabel('Normalised frequency ---->');
title('Amplitude Response');
grid on;
subplot(2,1,2);
plot(om/pi,an);
xlabel('Normalised frequency ---->');
ylabel('Phase in radians ---->');
title('Phase Response');
grid on;

02516412813

9
Output:
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the

passband
stopband
passband
stopband
sampling

ripple = 0.2
ripple = 40
frequency = 2000
frequency = 3500
frequency = 8000

Waveform:

02516412813

10

5. FIR Filters Using Kaiser Window


Program:
clc;
clear all;
rp = input('Enter the passband ripple = ');
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
y = kaiser(n1,beta);
% low-pass filter
b = fir1(n,wp,y);
[h,o] = freqz(b,1,256);
m = 20*log10(abs(h));
subplot(2,2,1);
plot(o/pi,m);
title('Magnitude Response of LPF');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
% high-pass filter
b = fir1(n,wp,'high',y);
[h,o] = freqz(b,1,256);
m = 20*log10(abs(h));
subplot(2,2,2);
plot(o/pi,m);
title('Magnitude Response of HPF');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;
% band pass filter
wn = [wp ws];
b = fir1(n,wn,y);
[h,o] = freqz(b,1,256);
m = 20*log10(abs(h));
subplot(2,2,3);
plot(o/pi,m);
title('Magnitude Response of BPF');
ylabel('Gain in dB ---->');
02516412813

11
xlabel('Normalised Frequency ---->');
grid on;
% band stop filter
b = fir1(n,wn,'stop',y);
[h,o] = freqz(b,1,256);
m = 20*log10(abs(h));
subplot(2,2,4);
plot(o/pi,m);
title('Magnitude Response of BSF');
ylabel('Gain in dB ---->');
xlabel('Normalised Frequency ---->');
grid on;

Output:
Enter
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the
the

passband ripple = 0.02


stopband ripple = 0.01
passband frequency = 1000
stopband frequency = 1500
sampling frequency = 10000
beta value = 5.8

Waveform:

02516412813

12

6. Linear Convolution Using MATLAB Function


Program
clc;
clear all;
x1 = input('Enter the first sequence x1(n) = ');
t1 = input('Enter the starting time of first sequence t1 = ');
x2 = input('Enter the second sequence x2(n) = ');
t2 = input('Enter the starting time of second sequence t2 = ');
l1 = length(x1);
l2 = length(x2);
ln = l1+l2-1;
y = conv(x1,x2);
a = t1+l1-1;
t = t1:a;
subplot(3,1,1);
stem(t,x1);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('First Sequence');
a = t2+l2-1;
t = t2:a;
subplot(3,1,2);
stem(t,x2);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Second Sequence');
tn = t1+t2;
a = tn+ln-1;
t = tn:a;
subplot(3,1,3);
disp('Convolved Sequence = ');
disp(y);
stem(t,y);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Convolved Output');

02516412813

13
Output:
Enter the
Enter the
Enter the
Enter the
Convolved
1

first sequence x1(n) = [1 2 1 0]


starting time of first sequence t1 = 0
second sequence x2(n) = [1 0 1]
starting time of second sequence t2 = 0
Sequence =
2
2
2
1
0

Waveforms:

02516412813

14

7. Circular Convolution
Program:
clc;
clear all;
x1 = input('Enter the first sequence = ');
x2 = input('Enter the second sequence = ');
l1 = length(x1);
l2 = length(x2);
n1 = max(l1,l2);
l = l1-l2;
if(l>=0)
x2 = [x2,zeros(1,l)];
else
x1 = [x1,zeros(1,-l)];
end
%circular shifting and convolution
for n = 1:n1
y(n) = 0;
for i = 1:n1
j = n-i+1;
if(j<=0)
j = n1+j;
end
y(n) = [y(n)+x1(i)*x2(j)];
end
end
%plot the inputs and output
n = 0:n1-1;
subplot(3,1,1);
stem(n,x1);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('First Sequence');
subplot(3,1,2);
stem(n,x2);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Second Sequence');
subplot(3,1,3);
disp('Convolved Sequence = ');
disp(y);
stem(n,y);
grid on;
xlabel('Time--->');
ylabel('Amplitude--->');
title('Convolved Output');

02516412813

15

Output:
Enter the
Enter the
Convolved
18

first sequence = [1 2 3 4]
second sequence = [1 2 3]
Sequence =
16
10
16

Waveforms:

02516412813

16

8. DFT & IDFT using MATLAB Function


Program:
clc;
close all;
clear all;
x=input('Please enter the sequence x(n)=');
N=input('Please enter the length of the DFT N=');
X=fft(x,N);
n=0:length(x)-1;
subplot(311);
stem(n,x);
title('Input Sequence');
subplot(323);
n=0:length(X)-1;
stem(n,X);
disp('DFT of input sequence is ');
disp(X);
title('DFT');
subplot(324);
stem(n,abs(X));
title('Magnitude spectrum');
subplot(325);
stem(n,angle(X));
title('Phase spectrum');
xr=ifft(x,N);
subplot(326);
stem(n,abs(xr));
title('IDFT');
disp('IDFT of input sequence is ');
disp(xr);

02516412813

17
Output:
Please enter the sequence x(n)=[1 2 3 4 5 6 7 8 9]
Please enter the length of the DFT N=6
DFT of input sequence is
Columns 1 through 4
21.0000 + 0.0000i

-3.0000 + 5.1962i

-3.0000 + 1.7321i

-3.0000 + 0.0000i

Columns 5 through 6
-3.0000 - 1.7321i

-3.0000 - 5.1962i

IDFT of input sequence is


Columns 1 through 4
3.5000 + 0.0000i

-0.5000 - 0.8660i

-0.5000 - 0.2887i

-0.5000 + 0.0000i

Columns 5 through 6
-0.5000 + 0.2887i

-0.5000 + 0.8660i

Waveforms:

02516412813

18

9. DTFT of a given sequence.


Program:

x=input('Enter the sequence : ')


N=length(x)
n=0:N-1
K=0:N-1
wn=exp(-j*2*pi/N)
nK=n'*K
wNnK=wn.^nK
df=x*wNnK
subplot(3,1,1)
stem(x,abs(df))
title('DTFT')
xlabel('K')
ylabel('Magnitude')
subplot(3,1,2)
stem(x,angle(df))
title('DTFT')
xlabel('K')
ylabel('phase')
wn=exp(j*2*pi/N)
nK=n'*K
wNnK=wn.^nK
inversdf=(x*wNnK)/N
subplot(3,1,3)
stem(x,abs(inversdf))
title('Inverse DTFT')
xlabel('N')
ylabel('Magnitude')

02516412813

19

Output:
Enter the sequence : [1 3 5 6 7 2 4 9 8]
x =

N =

n =

K =

1
0

wn =

2
1

3
2

4
3

7
6

8
7

0.7660 - 0.6428i

nK =

10

12

14

16

12

15

18

21

24

12

16

20

24

28

32

10

15

20

25

30

35

40

12

18

24

30

36

42

48

14

21

28

35

42

49

56

16

24

32

40

48

56

64

wNnK =
Columns 1 through 7
1.0000

1.0000

1.0000

1.0000

0.7660 - 0.6428i

0.1736 - 0.9848i

-0.5000 - 0.8660i

1.0000

-0.9397 - 0.3420i

1.0000

-0.9397 + 0.3420i

1.0000

-0.5000 + 0.8660i

1.0000

1.0000

0.1736 - 0.9848i

-0.9397 - 0.3420i

-0.5000 + 0.8660i

0.7660 + 0.6428i

0.7660 - 0.6428i

-0.5000 - 0.8660i

1.0000

-0.5000 - 0.8660i

-0.5000 + 0.8660i

1.0000 + 0.0000i

-0.5000 - 0.8660i

-0.5000 + 0.8660i

1.0000 + 0.0000i

1.0000

-0.9397 - 0.3420i

0.7660 + 0.6428i

-0.5000 - 0.8660i

0.1736 + 0.9848i

0.1736 - 0.9848i

-0.5000 + 0.8660i

1.0000

-0.9397 + 0.3420i

0.7660 - 0.6428i

-0.5000 + 0.8660i

0.1736 - 0.9848i

0.1736 + 0.9848i

-0.5000 - 0.8660i

1.0000

-0.5000 + 0.8660i

-0.5000 - 0.8660i

1.0000 + 0.0000i

-0.5000 + 0.8660i

-0.5000 - 0.8660i

1.0000 + 0.0000i

1.0000

0.1736 + 0.9848i

-0.9397 + 0.3420i

-0.5000 - 0.8660i

0.7660 - 0.6428i

0.7660 + 0.6428i

-0.5000 + 0.8660i

1.0000

0.7660 + 0.6428i

0.1736 + 0.9848i

-0.5000 + 0.8660i

-0.9397 + 0.3420i

-0.9397 - 0.3420i

-0.5000 - 0.8660i

-8.3512 +11.2381i

-6.0000 - 3.4641i

-2.0492 + 2.3309i

-2.0492 - 2.3309i

-6.0000 + 3.4641i

Columns 8 through 9
1.0000

1.0000

0.1736 + 0.9848i

0.7660 + 0.6428i

-0.9397 + 0.3420i

0.1736 + 0.9848i

-0.5000 - 0.8660i

-0.5000 + 0.8660i

0.7660 - 0.6428i

-0.9397 + 0.3420i

0.7660 + 0.6428i

-0.9397 - 0.3420i

-0.5000 + 0.8660i

-0.5000 - 0.8660i

-0.9397 - 0.3420i

0.1736 - 0.9848i

0.1736 - 0.9848i

0.7660 - 0.6428i

df =
Columns 1 through 7
45.0000

-1.5997 + 3.7110i

Columns 8 through 9
-8.3512 -11.2381i

-1.5997 - 3.7110i

02516412813

20

wn =
0.7660 + 0.6428i

nK =
0

10

12

14

16

12

15

18

21

24

12

16

20

24

28

32

10

15

20

25

30

35

40

12

18

24

30

36

42

48

14

21

28

35

42

49

56

16

24

32

40

48

56

64

wNnK =
Columns 1 through 7
1.0000

1.0000

1.0000

1.0000

1.0000

1.0000

1.0000

1.0000

0.7660 + 0.6428i

0.1736 + 0.9848i

-0.5000 + 0.8660i

-0.9397 + 0.3420i

-0.9397 - 0.3420i

-0.5000 - 0.8660i

1.0000

0.1736 + 0.9848i

-0.9397 + 0.3420i

-0.5000 - 0.8660i

0.7660 - 0.6428i

0.7660 + 0.6428i

-0.5000 + 0.8660i

1.0000

-0.5000 + 0.8660i

-0.5000 - 0.8660i

1.0000 - 0.0000i

-0.5000 + 0.8660i

-0.5000 - 0.8660i

1.0000 - 0.0000i

1.0000

-0.9397 + 0.3420i

0.7660 - 0.6428i

-0.5000 + 0.8660i

0.1736 - 0.9848i

0.1736 + 0.9848i

-0.5000 - 0.8660i

1.0000

-0.9397 - 0.3420i

0.7660 + 0.6428i

-0.5000 - 0.8660i

0.1736 + 0.9848i

0.1736 - 0.9848i

-0.5000 + 0.8660i

1.0000

-0.5000 - 0.8660i

-0.5000 + 0.8660i

1.0000 - 0.0000i

-0.5000 - 0.8660i

-0.5000 + 0.8660i

1.0000 - 0.0000i

1.0000

0.1736 - 0.9848i

-0.9397 - 0.3420i

-0.5000 + 0.8660i

0.7660 + 0.6428i

0.7660 - 0.6428i

-0.5000 - 0.8660i

1.0000

0.7660 - 0.6428i

0.1736 - 0.9848i

-0.5000 - 0.8660i

-0.9397 - 0.3420i

-0.9397 + 0.3420i

-0.5000 + 0.8660i

-0.9279 - 1.2487i

-0.6667 + 0.3849i

-0.2277 - 0.2590i

-0.2277 + 0.2590i

-0.6667 - 0.3849i

Columns 8 through 9
1.0000

1.0000

0.1736 - 0.9848i

0.7660 - 0.6428i

-0.9397 - 0.3420i

0.1736 - 0.9848i

-0.5000 + 0.8660i

-0.5000 - 0.8660i

0.7660 + 0.6428i

-0.9397 - 0.3420i

0.7660 - 0.6428i

-0.9397 + 0.3420i

-0.5000 - 0.8660i

-0.5000 + 0.8660i

-0.9397 + 0.3420i

0.1736 + 0.9848i

0.1736 + 0.9848i

0.7660 + 0.6428i

inversdf =
Columns 1 through 7
5.0000

-0.1777 - 0.4123i

Columns 8 through 9
-0.9279 + 1.2487i

-0.1777 + 0.4123i

>> abs(df)
ans =

02516412813

21
45.0000

4.0411

14.0013

6.9282

3.1036

3.1036

6.9282

14.0013

4.0411

1.9778

2.2099

-2.6180

2.2920

-2.2920

2.6180

-2.2099

-1.9778

1.5557

0.7698

0.3448

0.3448

0.7698

1.5557

0.4490

>> angle(df)
ans =
0

>> abs(inversdf)
ans =
5.0000

0.4490

>>

02516412813

You might also like