You are on page 1of 28

SHRI RAM INSTITUTE OF TECHNOLOGY JABALPUR

(DSP LAB)

Department of
ELECTRONICS & COMMUNICATION ENGINEERING
List of Experiments:

Introduction to MATLAB

1. To implement delta function, unit step function, Ramp function.

2. To explore the commutation of even and odd symmetries in a signal with algebraic
operations.

3. To explore the effect of transformation of signal parameters


(amplitude - scaling, timescaling and time-shifting).

4. To explore the time variance and time invariance property of a given system.

5. To explore causality and non-causality property of a system.

6. To demonstrate the convolution and correlation of two continuous-time signals.

7. To demonstrate the convolution and correlation of two discrete-time signals.

8. To determine Magnitude and Phase Response of Fourier Transform of given signals.

9. Consider following transfer function


H(ejω)=1/1+0.1 e-jω+0.2 e-2jω
plot its magnitude and phase plot for 128 discrete frequency points
10. Plot frequency response for the normalized LP-FIR filters using the command fir1 in conjunction
with the following windows:

a. Kaiser (β = 3.5)
b. Hanning
c. Blackman
d. Hamming
e. Triangular
f. Bartlett
The filter’s specs are N = 64 and Rp = 1.4 db

11. Consider the following continuous signal


x(t) 2 +cos(10t)
Design a low-pass digital filter to filter out the sinusoidal term. Use different windows in the design.
12. Design Butterworth LPF and plot response.

AIM: 1. To implement delta function, unit step function, Ramp function.


t=-3:.01:3;
y=exp(-t); %e^-t
subplot(231);
plot(t,y);
title('exponential');

x=-2*pi:.01:2*pi;
y=sinc(x); %sin(pi*x)/pi*x
subplot(232);
plot(x,y);
title('sinc(x)');

y = [1; zeros(99,1)]; % impulse


subplot(233);
plot(y);
title('impulse');

y = ones(100,1); %unit step


subplot(234);
plot(y);
title('unit step');

t = 0:0.01:3;
y = t; % ramp
subplot(235);
plot(t,y);
title('ramp');

t = 0:0.01:20*pi;
y = sin(t);
subplot(236);
plot(t,y);
title('sin wave');
exponential sinc(x) impulse
30 1 1

20 0.5
0.5
10 0

0 -0.5 0
-5 0 5 -10 0 10 0 50 100

unit step ramp sin wave


2 3 1

1.5 0.5
2
1 0
1
0.5 -0.5

0 0 -1
0 50 100 0 2 4 0 50 100

AIM 1b: various exponentials


% Script file: exponentials
t = -3:.1:3;
ft1 = 4*exp(-t./2);
ft2 = 4*exp(t./2);
ut_1 = [zeros(1,30) ones(1,31)]; % step with 61 elements
ft3 = ft1.*ut_1;
subplot(2,2,1);
plot(t, ft1);
axis([-3 3 -.5 18]);xlabel('t (time)')
title('f1(t)=4*exp(-t/2) vs. t');
ylabel('Amplitude [f1(t)]')
subplot(2,2,2);
plot(t, ft2); xlabel('t (time)')
axis([-3 3 0 20]);
title('f2(t) = 4*exp(t/2) vs. t');
ylabel('Amplitude [f2(t)]');
subplot(2,2,3);
plot(t, ft3);
axis([-3 3 -.5 5]);
title('f3(t) =4*exp(-t/2)*u(t) vs. t');
xlabel('t (time)')
ylabel('Amplitude[f3(t)]');
subplot(2,2,4);
ft4 =-1.*ft2.*ut_1;
plot(t, ft4);
axis([-3 3 -20 1]);
title('f4(t) = -4*exp(-t/2)*u(t) vs. t')
xlabel('t (time)'); ylabel('Amplitude[f4(t)]');
f1(t)=4*exp(-t/2) vs. t f2(t) = 4*exp(t/2) vs. t
20
15
Amplitude [f1(t)]

Amplitude [f2(t)]

15
10
10

5
5

0 0
-2 0 2 -2 0 2
t (time) t (time)
f3(t) =4*exp(-t/2)*u(t) vs. t f4(t) = -4*exp(-t/2)*u(t) vs. t
5 0
4
Amplitude[f3(t)]

Amplitude[f4(t)]

-5
3
2 -10

1 -15
0
-20
-2 0 2 -2 0 2
t (time) t (time)

AIM2: To explore the commutation of even and odd symmetries in a signal with
algebraic operations.
% Script file: even _ odd
t = -10:0.1:10; % 201points
yzero = zeros(1,201);
y1 = [zeros(1,50) 10*ones(1,50)];
y2 = [10:-.1:0];
y = [y1 y2];
figure(1)
subplot(3,1,1)
plot(t,y);axis([-10 10 -5 13]);
title('f(t) vs. t')
ylabel('Amplitude [f(t)]');xlabel('time');
flipft = fliplr(y);
feven =.5*(y+flipft);
fodd =.5*(y-flipft);
subplot(3,1,2);
plot(t,feven); axis([-10 10 -5 13]);
ylabel('Amplitude [feven(t)]');xlabel('time')
title('feven(t) vs.t')
subplot(3,1,3)
plot(t,fodd,t,yzero);
ylabel('Amplitude [fodd(t)]'); xlabel('time')
title('fodd(t) vs.t')
figure(2)
check = feven+fodd;
ysquare = y.^2;
energyft = trapz(t,ysquare);
poweraveft = (1/200)*energyft;
fevensq = feven.^2;
energyfeven = trapz(t,fevensq);
foddsq = fodd.^2;
energyfodd = trapz(t,foddsq);
disp('*********************************************')
disp('************ ENERGY ANALYSIS **************')
disp('*********************************************')
disp('The energy of f(t) =')
disp(energyft)
disp('The energy of feven(t) =')
disp(energyfeven)
disp('The energy of fodd(t) =')
disp(energyfodd)
disp(' (in joules)')
disp('*********************************************')
disp('*********************************************')
plot(t,check);
title('[feven(t) + fodd(t)] vs. t )');
axis([-10 10 -5 15]);xlabel('time');
ylabel('Amplitude [feven(t) + fodd(t)]')
[feven(t) + fodd(t)] vs. t )
15

10

Amplitude [feven(t) + fodd(t)]

-5
-10 -8 -6 -4 -2 0 2 4 6 8 10
time

f(t) vs. t
Amplitude [f(t)]

10
5
0
-5
-10 -8 -6 -4 -2 0 2 4 6 8 10
time
feven(t) vs.t
Amplitude [feven(t)]

10
5
0
-5
-10 -8 -6 -4 -2 0 2 4 6 8 10
time
fodd(t) vs.t
Amplitude [fodd(t)]

-5
-10 -8 -6 -4 -2 0 2 4 6 8 10
time
AIM3: To explore the effect of transformation of signal parameters
(amplitude - scaling, timescaling and time-shifting).
A. Time shifting
x = -10:0.1:10;
stepfun = heaviside(x);
subplot(3, 1, 1);
plot(x, stepfun)
xlabel('t (time)')
title('u(t) vs. t')
ylabel('Amplitude.')
axis([-10 10 ,0.5 1.5])
subplot(3, 1, 2);
stepfun = heaviside(x-4);
plot(x, stepfun)
axis([-10 10 ,0.5 1.5])
title('u(t-4) vs. t')
ylabel('Amplitude.');
xlabel('t (time)');
subplot(3, 1, 3);
stepfun = heaviside(x+4);
plot(x, stepfun)
axis([-10 10 ,0.5 1.5])
title('u(t+4) vs. t')
ylabel('Amplitude.');
xlabel('t (time)');

u(t) vs. t
1.5
Amplitude.

0.5
-10 -8 -6 -4 -2 0 2 4 6 8 10
t (time)
u(t-4) vs. t
1.5
Amplitude.

0.5
-10 -8 -6 -4 -2 0 2 4 6 8 10
t (time)
u(t+4) vs. t
1.5
Amplitude.

0.5
-10 -8 -6 -4 -2 0 2 4 6 8 10
t (time)
B. Time scalling
% Script file: rect_ pulses
t = -10:.1:10;
f1 = rectpuls(t);
f2 = rectpuls(2*t);
f3 = rectpuls(t/2);
f4 = rectpuls(t/4);
subplot(2,2,1)
plot (t,f1)
grid on
ylabel (' Amplitude [f1(t)]');xlabel('t (time)');
axis ([-10 10 -0.5 2])
title('Rectpuls f1')
subplot(2,2,2)
plot(t,f2)
grid on
ylabel( 'Amplitude [f2(t)=f1(2*t)]'); xlabel('t (time)');
axis([-10 10 -0.5 2])
title('Rectpuls f2')
subplot(2,2,3)
plot(t,f3)
grid on
axis([-10 10 -0.5 2])
ylabel(' Amplitude [f3(t)]');xlabel(' t (time)')
title('Rectpuls f3')
subplot(2,2,4)
plot(t,f4)
grid on
axis([-10 10 -0.5 2])
title('Rectpuls f4')
ylabel(' Amplitude [f4(t)] '); xlabel(' t (time) ')

Rectpuls f1 Rectpuls f2
2 2
Amplitude [f2(t)=f1(2*t)]

1.5 1.5
Amplitude [f1(t)]

1 1

0.5 0.5

0 0

-0.5 -0.5
-10 -5 0 5 10 -10 -5 0 5 10
t (time) t (time)
Rectpuls f3 Rectpuls f4
2 2

1.5 1.5
Amplitude [f3(t)]

Amplitude [f4(t)]

1 1

0.5 0.5

0 0

-0.5 -0.5
-10 -5 0 5 10 -10 -5 0 5 10
t (time) t (time)
AMPLITUDE SCALLING
t = -10:.1:10;
f1 = rectpuls(t);
f2 = 2*rectpuls(t);
f3 = 4*rectpuls(t);
f4 = -3*rectpuls(t);
subplot(2,2,1)
plot (t,f1)
grid on
ylabel (' Amplitude [f1(t)]');xlabel('t (time)');
axis ([-10 10 -0.5 5])
title('Rectpuls f1=rectpuls(t)')
subplot(2,2,2)
plot(t,f2)
grid on
ylabel( 'Amplitude [f2(t)=f1(2*t)]'); xlabel('t (time)');
axis([-10 10 -0.5 5])
title('Rectpuls f2=2*rectpuls(t)')
subplot(2,2,3)
plot(t,f3)
grid on
axis([-10 10 -0.5 5])
ylabel(' Amplitude [f3(t)]');xlabel(' t (time)')
title('Rectpuls f3=4*rectpuls(t)')
subplot(2,2,4)
plot(t,f4)
grid on
axis([-10 10 -5 5])
title('Rectpuls f4=-3*rectpuls(t)')
ylabel(' Amplitude [f4(t)] '); xlabel(' t (time) ')
Rectpuls f1=rectpuls(t) Rectpuls f2=2*rectpuls(t)
5 5
Amplitude [f2(t)=f1(2*t)]

4 4
Amplitude [f1(t)]

3 3
2 2
1 1
0 0
-10 -5 0 5 10 -10 -5 0 5 10
t (time) t (time)
Rectpuls f3=4*rectpuls(t) Rectpuls f4=-3*rectpuls(t)
5 5
4
Amplitude [f3(t)]

Amplitude [f4(t)]

3
2 0

1
0
-5
-10 -5 0 5 10 -10 -5 0 5 10
t (time) t (time)
AIM4: To explore the time variance and time invariance property of a given
system.

Software Required:
Mat lab software 7.0 and above
Theory:
TIME INVARIENT SYSTEMS(TI):
A system is called time invariant if its input – output characteristics do not change with time
X(t)---- input : Y(t) ---output
X(t-T) -----delay input by T seconds : Y(t-T) ------ Delayed output by T seconds
Program:
clc;
clear all;
close all;
% entering two input sequences
x = input( ' Type the samples of signal x(n) ' );
h = input( ' Type the samples of signal h(n) ' );
% original response
y = conv(x,h);
disp( ' Enter a POSITIVE number for delay ' );
d = input( ' Desired delay of the signal is ' );
% delayed input
xd = [zeros(1,d), x];
nxd = 0 : length(xd)-1;
%delayed output
yd = conv(xd,h);
nyd = 0:length(yd)-1;
disp(' Original Input Signal x(n) is ');
disp(x);
disp(' Delayed Input Signal xd(n) is ');
disp(xd);
disp(' Original Output Signal y(n) is ');
disp(y);
disp(' Delayed Output Signal yd(n) is ');
disp(yd);
xp = [x , zeros(1,d)];
subplot(2,1,1);
stem(nxd,xp);
grid;
xlabel( ' Time Index n ' );
ylabel( ' x(n) ' );
title( ' Original Input Signal x(n) ' );
subplot(2,1,2);
stem(nxd,xd);
grid;
xlabel( ' Time Index n ' );
ylabel( ' xd(n) ' );
title( ' Delayed Input Signal xd(n) ' );
yp = [y zeros(1,d)];
figure;
subplot(2,1,1);
stem(nyd,yp);
grid;
xlabel( ' Time Index n ' );
ylabel( ' y(n) ' );
title( ' Original Output Signal y(n) ' );
subplot(2,1,2);
stem(nyd,yd);
grid;
xlabel( ' Time Index n ' );
ylabel( ' yd(n) ' );
title( ' Delayed Output Signal yd(n) ' );
Result: The Time Invariance of a given Discrete System is verified.
Output:
Type the samples of signal x(n) [2 3 4 6]
Type the samples of signal h(n) [1 2 3 8]
Enter a POSITIVE number for delay
Desired delay of the signal is 5
Original Input Signal x(n) is
2346
Delayed Input Signal xd(n) is
000002346
Original Output Signal y(n) is
2 7 16 39 48 50 48
Delayed Output Signal yd(n) is
0 0 0 0 0 2 7 16 39 48 50 48
Original Input Signal x(n) Original Output Signal y(n)
6 40

30
4

x(n)

y(n)
20
2
10

0 0
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 8 9 10
Time Index n Time Index n
Delayed Input Signal xd(n) Delayed Output Signal yd(n)
6 40

4 30
xd(n)

yd(n)
20
2
10
0
0 1 2 3 4 5 6 7 0
0 1 2 3 4 5 6 7 8 9 10
Time Index n
Time Index n

AIM4[B]: Verify the Linearity of a given Discrete System.


Software Required:
Mat lab software 7.0 and above
Theory:
LINEARITY PROPERTY:
Any system is said to be linear if it satisfies the superposition principal. superposition
principal state that Response to a weighted sum of input signal equal to the corresponding
weighted sum of the outputs of the system to each of the individual input signals.
X(n)-----------input signal
Y(n) --------- output signal
Y(n)=T[x(n)]
Y1(n)=T[X1(n)] : Y2(n)=T[X2(n)]
x3=[a X1(n)] +b [X2(n) ]
Y3(n)= T [x3(n)] = T [a X1(n)] +b [X2(n) ] = a Y1(n)+ b [Y
Let a [Y1(n)]+ b [X2(n) ] =Z 3(n)
Program:
clc;
clear all;
close all;
% entering two input sequences and impulse sequence
x1 = input (' type the samples of x1 ');
x2 = input (' type the samples of x2 ');
if(length(x1)~=length(x2))
disp('error: Lengths of x1 and x2 are different');
return;
end;
h = input (' type the samples of h ');
% length of output sequence
N = length(x1) + length(h) -1;
disp('length of the output signal will be ');
disp(N);
% entering scaling factors
a1 = input (' The scale factor a1 is ');
a2 = input (' The scale factor a2 is ');
x = a1 * x1 + a2 * x2;
% response of x and x1
yo1 = conv(x,h);
y1 = conv(x1,h);
% scaled response of x1
y1s = a1 * y1;
% response of x2
y2 = conv(x2,h);
% scaled response of x2
y2s = a2 * y2;
yo2 = y1s + y2s;
disp ('Input signal x1 is '); disp(x1);
disp ('Input signal x2 is '); disp(x2);
disp ('Output Sequence yo1 is '); disp(yo1);
disp ('Output Sequence yo2 is '); disp(yo2);
if ( yo1 == yo2 )
disp(' yo1 = yo2. Hence the LTI system is LINEAR ')
end;
Result: The Linearity of a given Discrete System is verified.
Output:
Type the samples of x1 [1 5 6 7]
Type the samples of x2 [2 3 4 8]
Type the samples of h [2 6 5 4]
Length of the output signal will be
7
The scale factor a1 is 2
The scale factor a2 is 3
Input signal x1 is
1567
Input signal x2 is
2348
Output Sequence yo1 is
16 86 202 347 424 286 152
Output Sequence yo2 is
16 86 202 347 424 286 152
yo1 = yo2. Hence the LTI system is LINEAR

AIM5: To explore causality and non-causality property of a system.


AIM 6[ A] : To demonstrate the convolution and correlation of two continuous-time
signals.
Aim: Write the program for convolution between two signals and also between two
sequences.
Software Required:
Matlab software 7.0 and above.
Theory:
If x(n)=h(n) [ impulse ) then output y(n) is known as impulse response of the system.
x(n)=δ(n)
y(n)=T[x(n)]=h(n) similarly δ (n-k)= h(n-k)
x(n) cab represented as weighted sum of impulses such as
y(n)=T[x(n)] =T[δ(n)=h(n) then T[ δ (n-k)] = h(n-k)
Thus output of LTI system is given by weighted sum of time-shifted impulse responses
Linear Convolution involves the following operations.
1. Folding 2. Multiplication 3. Addition 4. Shifting
These operations can be represented by a Mathematical Expression as follows:
Program:
clc;
close all;
clear all;
%program for convolution of two sequences
x=input('enter input sequence');
h=input('enter impulse response');
y=conv(x,h);
subplot(3,1,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input signal')
subplot(3,1,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('impulse response')
subplot(3,1,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title('linear convolution')
disp('The resultant signal is');
disp(y)
%program for signal convolution
t=0:0.1:10;
x1=sin(2*pi*t);
h1=cos(2*pi*t);
y1=conv(x1,h1);
figure;
subplot(3,1,1);
plot(t,x1);
xlabel('t');
ylabel('x(t)');
title('input signal')
subplot(3,1,2);
plot(t,h1);
xlabel('t');
ylabel('h(t)');
title('impulse response')
subplot(3,1,3);
plot(y1);
xlabel('n');
ylabel('y(n)');
title('linear convolution');
RESULT: convolution between signals and sequences is computed.
Output:
enter input sequence[2 4 6 8]
enter impulse response[1 3 9 6]
The resultant signal is
2 10 36 74 102 108 48

input signal
10
x(n)

0
1 1.5 2 2.5 3 3.5 4
n
impulse response
10
h(n)

0
1 1.5 2 2.5 3 3.5 4
n
linear convolution
200
y(n)

100

0
1 2 3 4 5 6 7
n
AIM 6[ B]: Write the program to compute Auto correlation and Cross correlation
between signals and sequences.
Software Required:
Mat lab software 7.0 and above
Theory:
Correlations of sequences:
It is a measure of the degree to which two sequences are similar. Given two real-valued
sequences x(n) and y(n) of finite energy,
Convolution involves the following operations.
1. Shifting
2. Multiplication
3. Addition
These operations can be represented by a Mathematical Expression as follows:
Program:
clc;
close all;
clear all;
% two input sequences
x=input('enter input sequence');
h=input('enter the impulse suquence');
subplot(2,2,1);
stem(x);
xlabel('n');
ylabel('x(n)');
title('input sequence');
subplot(2,2,2);
stem(h);
xlabel('n');
ylabel('h(n)');
title('impulse signal');
% cross correlation between two sequences
y=xcorr(x,h);
subplot(2,2,3);
stem(y);
xlabel('n');
ylabel('y(n)');
title(' cross correlation between two sequences ');
% auto correlation of input sequence
z=xcorr(x,x);
subplot(2,2,4);
stem(z);
xlabel('n');
ylabel('z(n)');
title('auto correlation of input sequence');
% cross correlation between two signals
% generating two input signals
t=0:0.2:10;
x1=3*exp(-2*t);
h1=exp(t);
figure;
subplot(2,2,1);
plot(t,x1);
xlabel('t');
ylabel('x1(t)');
title('input signal');
subplot(2,2,2);
plot(t,h1);
xlabel('t');
ylabel('h1(t)');
title('impulse signal');
% cross correlation
subplot(2,2,3);
z1=xcorr(x1,h1);
plot(z1);
xlabel('t');
ylabel('z1(t)');
title('cross correlation ');
% auto correlation
subplot(2,2,4);
z2=xcorr(x1,x1);
plot(z2);
xlabel('t');
ylabel('z2(t)');
title('auto correlation ');
Result: Auto correlation and Cross correlation between signals and sequences is computed.
Output: enter input sequence [1 2 5 7]
Enter the impulse sequence [2 6 0 5 3]
input signal
1

x(t)
0

-1
0 1 2 3 4 5 6 7 8 9 10
t
impulse response
1
h(t)

-1
0 1 2 3 4 5 6 7 8 9 10
t
linear convolution
50
y(n)

-50
0 50 100 150 200 250
n

input sequence impulse signal


8 6

6
4
x(n)

h(n)

4
2
2

0 0
1 2 3 4 1 2 3 4 5
n n
cross correlation between two sequences auto correlation of input sequence
60 80

40 60
y(n)

z(n)

20 40

0 20

-20 0
0 5 10 0 2 4 6 8
n n
AIM 7: To determine Magnitude and Phase Response of Fourier Transform of
given signals.
AIM: To find the Fourier Transform of a given signal and plotting its magnitude and phase
spectrum.
Software Required:
Matlab software 7.0 and above.
clc;
clear all;
close all;
fs=1000;
N=1024; % length of fft sequence
t=[0:N-1]*(1/fs);
% input signal
x=0.8*cos(2*pi*100*t);
subplot(3,1,1);
plot(t,x);
axis([0 0.05 -1 1]);
grid;
xlabel('t');
ylabel('amplitude');
title('input signal');
% magnitude spectrum
x1=fft(x);
k=0:N-1;
Xmag=abs(x1);
subplot(3,1,2);
plot(k,Xmag);
grid;
xlabel('t');
ylabel('amplitude');
title('magnitude of fft signal')
%phase spectrum
Xphase=angle(x1)*(180/pi);
subplot(3,1,3);
plot(k,Xphase);
grid;
xlabel('t');
ylabel('angle in degrees');
title('phase of fft signal');
input signal
1

amplitude
0

-1
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05
t
magnitude of fft signal
400
amplitude

200

0
0 200 400 600 800 1000 1200
t
phase of fft signal
angle in degrees

200

-200
0 200 400 600 800 1000 1200
t
Experiment No.9

AIM: Consider following transfer function


H(ejω)=1/1+0.1 e-jω+0.2 e-2jω

plot its magnitude and phase plot for 128 discrete frequency points.

PROGRAM:

df = linspace(0,2*pi,128); % Range from 0 to 2pi


n = [1]
d = [1 .1 .2]
x = freqz(n, d, df)
subplot(2, 1, 1);
plot(df, abs(x));
ylabel('Magnitude plot');
subplot(2, 1, 2);
plot(df, angle(x));
ylabel('Phase plot');
xlabel('Frequency');
OUTPUT:
Experiment No. 10

AIM: Plot frequency response for the normalized LP-FIR filters using the command fir1 in
conjunction with the following windows:
a. Kaiser (β = 3.5)
b. Hanning
c. Blackman
d. Hamming
e. Triangular
f. Bartlett
The filter’s specs are N = 64 and Rp = 1.4 dB

PROGRAM:

format long
hnk = fir1(64,.4,kaiser(65,3.5));
[h1,w1] = freqz(hnk,1,256);
magk = 20*log10(abs(h1));
subplot(3,2,1)
plot(w1,magk)
title('Filter using kaiser win.')
ylabel('gain in db')

subplot(3,2,2)
hnhan = fir1(64,.4,hanning(65));
[h2,w2] = freqz(hnhan,1,256);
maghan = 20*log10(abs(h2));
plot(w2,maghan)
title('Filter using hanning win.')
ylabel('gain in db')

subplot(3,2,3)
hnbla = fir1(64,.4,blackman(65));
[h3,w3] = freqz(hnbla,1,256);
magblac=20*log10(abs(h3));
plot(w3,magblac)
title('Filter using blackman win.')
ylabel('gain in db')

subplot(3,2,4)

hnham = fir1(64,.4); % hamming window is the default


[h4,w4] = freqz(hnham,1,256);
magham = 20*log10(abs(h4));
plot(w4,magham)
title('Filter using hamming win.')
ylabel('gain in db')

subplot(3,2,5)
hntri = fir1(64,.4,triang(65));
[h5,w5] = freqz(hntri,1,256);
magtri = 20*log10(abs(h5));
plot(w5,magtri)
title('Filter using triang. win.')
xlabel('frequency in rad.')
ylabel('gain in db')
axis([0 3 -60 10]);

subplot(3,2,6)
hnbar = fir1(64,.4,bartlett(65));
[h6,w6] = freqz(hnbar,1,256);
magbar = 20*log10(abs(h6));
plot(w6,magbar)
title('Filter using bartlett win.')
xlabel('frequency in rad.')
ylabel('gain in db')
axis([0 3 -60 10])
OUTPUT:
Experiment No.11
AIM: Consider the following continuous signal
x(t) 2 +cos(10t)
Design a low-pass digital filter to filter out the sinusoidal term. Use different windows in the design.
PROGRAM:

clc;
N=11; %using N=11( filter order)
m=(N-1)/2;
thetac=.5;
n=0:N-1;
h=sin(thetac*(n-m+eps))./(pi*(n-m+eps)); %eps is used to avoid dividing by zero
h1=h.*(ones(1,N)); %we window the filter coefficients
h2=h.*(hanning(N))';
n=0:100;
Ts=.1;
xn1=2; %This signal should pass through the filter
xn2=cos(10*n*Ts); %This signal should not pass
xn=xn1+xn2;
t=0:.1:10;
xt=cos(10*t)+2;
y1=filter(h1,1,xn);
y2=filter(h2,1, xn);
subplot(3,1,1);
plot(t,xt);
title('cos(10*t)+2 before filtering');
[H1 f1]=freqz(h1,1,100);
subplot(3,1,2);
plot(f1/pi,abs(H1));
hold on;
[H2 f2]=freqz(h2,1,100);
plot(f2/pi,abs(H2),'*');
legend('Rectangular','Hanning',0);
ylabel('Filter: Order 11');
subplot(3,1,3); plot(n*Ts,y1);
title('The signal after cos(10*t) term is removed: Filter order is 11');
xlabel('Time(sec)'); hold on; plot(n*Ts, y2, '*');
legend('Rectangular','Hanning',0);
OUTPUT:
Experiment No. 12
AIM: Design Butterworth LPF and plot response.

PROGRAM:

clc;
clear;
fp=500; %LPF of cut off 500Hz
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(2);
subplot 211, plot(n,abs(fft(x)));
y=filter(b,a,x);
subplot 212, plot(n,abs(fft(y)));
OUTPUT:

You might also like