You are on page 1of 15

Experiment 4:

Aim: To perform convolution of two signals using MATLAB.


Theory:
Convolution is a mathematical operation on two functions (f and g)
that produces a third function expressing how the shape of one is
modified by the other. A convolution is an integral that expresses the
amount of overlap of one function g as it is shifted over another
function f. It therefore "blends" one function with another. It is
defined as the integral of the product of the two functions after one
is reversed and shifted.

Code:
x=[1 1 1 1];
h=[2 2 2];
l1=length(x);
l2=length(h);
X=[x zeros(1,l2)];
H=[h zeros(1,l1)];
y=[];
fori=1:l1+l2-1
y(i)=0;
for j=1:l1
if((i-j+1)>0)
y(i)=y(i)+X(j)*H(i-j+1);
end
end
end
ycal=conv(x,h);
subplot(2,2,1);
plot(x);
ylim([0 4]);
xlim([1 6]);
title('x(n)');
xlabel('n');
ylabel('x(n)');
subplot(2,2,2);
plot(h);
ylim([0 4]);
xlim([1 6]);
title('h(n)');
xlabel('n');
ylabel('h(n)');
subplot(2,2,3);
plot(y);
ylim([1 7]);
xlim([1 9]);
title('Observed');
xlabel('n');
ylabel('y(n)');
subplot(2,2,4);
plot(ycal);
ylim([1 7]);
xlim([1 9]);
title('Expected');
xlabel('n');
ylabel('ycal(n)');

Output:

Result:
Thus, we performed the convolution of two signals using MATLAB.
Experiment 5:
Aim: To exhibit linear time-invariant(LTI) system response of a signal
in MATLAB.
Theory:
Linear time-invariant systems (LTI systems) are a class of systems
that are both linear and time-invariant. Linear systems are systems
whose outputs for a linear combination of inputs are the same as a
linear combination of individual responses to those inputs. Time-
invariant systems are systems where the output does not depend
on when an input was applied.
Changing the input in a linear way will change the output in the same
linear way. So, if the input x1(t) produces the output y1(t) and the
input x2(t) produces the output y2(t), then linear combinations of
those inputs will produce linear combinations of those outputs.
In other words, for a system T over time t, composed of signals x1
(t) and x2(t) with outputs y1(t) and y2(t),

where a1 and a2 are constants.


Code:
x1=[1 1 1 0];
x2=[2 2 2 3];
h=[1 0 1];
a=2;
b=3;
y1=conv(x1,h);
y2=conv(x2,h);
x3=a*x1+b*x2;
y3obs=conv(x3,h);
y3cal=a*y1+b*y2;
subplot(2,2,1);
plot(x1,'blue');
title('Input Signals');
hold on;
plot(x2,'red');
hold off;
subplot(2,2,2);
plot(y1,'blue');
title('Responses');
hold on;
plot(y2,'red');
hold off;
subplot(2,2,3);
plot(y3obs);
title('Observed');
subplot(2,2,4);
plot(y3cal);
title('Calculated');

Output:

Result:
Thus, we saw the linear time-invariant (LTI) system response of a
signal in MATLAB.
Experiment 6:
Aim: To generate Discrete Fourier Transform (DFT) of a given signal
and plot its magnitude and phase spectra in MATLAB.
Theory:
Fourier Transform is a mathematical function that takes in a time-
based function and converts it into frequency-based function. It is
another way of representing a waveform, which makes analysis of
possible cycles in the function easier.
For continuous time signals:

For discrete time signals:

Code:
x=[2 4 0 3]
N=4
n=[0:1:N-1]
k=[0:1:N-1]
w=exp(-j*2*pi/N)
r=n'*k
W=w.^r
X=x*W
a=abs(X)
b=angle(X)*180/pi
subplot(2,2,1)
plot(n,x)
ylabel('x(n)')
xlabel('n')
subplot(2,2,2)
plot(n,X)
ylabel('X(w)')
xlabel('w')
subplot(2,2,3)
plot(n,a)
ylabel('Amplitude Spectrum')
xlabel('w')
subplot(2,2,4)
plot(n,b)
ylabel('Phase Spectrum')
xlabel('w')

Output:

Result:
Thus, we generated the Discrete Fourier Transform (DFT) of a given
signal and plotted its magnitude and phase spectra in MATLAB.
Experiment 7:
Aim: To generate Inverse Discrete Fourier Transform (IDFT) of a
given frequency domain signal in MATLAB.
Theory:
Inverse Fourier transform is used to convert a signal in frequency
domain to a signal in time domain.
The inverse DFT is given by:

Code:
X=[6,-2-2j,2,-2+2j ]
N=4
n=[0:1:N-1]
k=[0:1:N-1]
w=exp(j*2*pi/N)
r=n'*k
W=w.^r
x=(X*W)/N
subplot(1,2,1)
plot(k,X,'black')
axis([-1 5 -3 7])
ylabel('X(w)')
xlabel('w')
title('Signal in frequency domain')
subplot(1,2,2)
plot(n,x,'black')
axis([-1 5 0 4])
ylabel('x[n]')
xlabel('n')
title('Signal in Time domain')
Output:

Result:
Thus, we generated the Inverse Discrete Fourier Transform (IDFT) of
a given frequency domain signal in MATLAB.
Experiment 8:
Aim: To find Laplace Transform and Inverse Laplace Transform using
built-in MATLAB functions.
Theory:
Laplace transform is an integral transform which transforms a
function of a real variable t (often time) to a function of a complex
variable s (complex frequency). The Laplace transform is similar to
the Fourier transform. While the Fourier transform of a function is
a complex function of a real variable (frequency), the Laplace
transform of a function is a complex function of a complex variable.
Laplace transforms are usually restricted to functions of t with t ≥ 0.
The Laplace transform is invertible on a large class of functions. The
inverse Laplace transform takes a function of a complex
variable s (often frequency) and yields a function of a real
variable t (often time). Laplace transformation from the time
domain to the frequency domain transforms differential equations
into algebraic equations and convolution into multiplication.
The Laplace transform of a function f(t), defined for all real
numbers t ≥ 0, is the function F(s), which is a unilateral transform
defined by:

where s is a complex number frequency parameter

with real numbers σ and ω.


Code:
%laplace transform
symsft;
f=sin(t);
y1=laplace(f)
symsft;
f=t*t+1;
y2=laplace(f)
%inverse laplace transform
symsFs;
F=1/(s^2+1);
y3=ilaplace(F)
symsFs;
F=3/(s^4);
y4=ilaplace(F)

Output:

Result:
Thus, we found Laplace Transform and Inverse Laplace Transform
using built-in MATLAB functions.
Experiment 9:
Aim: To find Z-Transform and Inverse Z-Transform using built-in
MATLAB functions.
Theory:
Z-transform converts a discrete-time signal, which is
a sequence of real or complex numbers, into a complex frequency-
domain representation. It can be considered as a discrete-time
equivalent of the Laplace transform.
The bilateral or two-sided Z-transform of a discrete-time signal
is the formal power series defined as:

where n is an integer and z isa complex number.

Code:
%z-transform
symsfn;
f=1/(4^n);
y1=ztrans(f)
symsfn;
f=cos(n);
y2=ztrans(f)
%inverse z-transform
symsFz;
F=(4*z/(4*z-1)-z^-1+5)/(6-5*z^-1+z^-2);
y3=iztrans(F)
symsFz;
F=z^2/(z-1)^2;
y4=iztrans(F)
Output:

Result:
Thus, we found Z-Transform and Inverse Z-Transform using built-in
MATLAB functions.
Experiment 10:
Aim: To generate Finite Impulse Response Low pass and high pass
filter using in-built functions in MATLAB.
Theory:
In signal processing, a finite impulse response (FIR) filter is
a filter whose impulse response (or response to any finite length
input) is of finite duration, because it settles to zero in finite time.
This is in contrast to infinite impulse response (IIR) filters, which may
have internal feedback and may continue to respond indefinitely
(usually decaying).
The impulse response (that is, the output in response to a Kronecker
delta input) of an Nth-order discrete-time FIR filter lasts exactly N + 1
samples (from first nonzero element through last nonzero element)
before it then settles to zero.
For a causal discrete-time FIR filter of order N, each value of the
output sequence is a weighted sum of the most recent input values:

Code:
For Low pass filter:
clc
clear all
close all
N=1000;
fs=8000;
fc=2000;
wc=fc/(fs/2);
h=fir1(N, wc, 'low', hamming(N+1));
%h=fir1(N,wc,'high',hamming(N+1));
freqz(h,1);
For High pass filter:
clc
clear all
close all
N=1000;
fs=8000;
fc=2000;
wc=fc/(fs/2);
%h=fir1(N, wc, 'low', hamming(N+1));
h=fir1(N,wc,'high',hamming(N+1));
freqz(h,1);

Output:
For Low Pass Filter:

For High Pass Filter:


Result:
Thus, we generated the Finite Impulse Response(FIR) of a given
frequency domain signal in MATLAB.

You might also like