Signal Processing Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 123

UNIVERSITY OF CALCUTTA

PAPER B363P – SIGNAL PROCESSING LAB


NAME: - SOURABH KUMAR BARIK
COLLEGE ROLL NO: – OOECU-304
UNIVERSITY ROLL NO: – T91/OOE/196004
REGISTRATION NO: - 012-1111-1397-17
SEMESTER: - 6TH SEMESTER
STREAM: – B. TECH OPTICS AND OPTOELECTRONICS
SESSION: - 2019-2023
INDEX

MODULE TITLE OF THE PAGE


PROBLEM NO REMARKS
NO. MODULE NO

A) Data Type 1,2

B) Sinusoidal Function 3-6

1. MODULE 1: RECAP C) Arrays, Logical Expression 7-14

D) Use Of Plot Function 15-28

E) Generation Of Signals 29-42

1. Discrete rectangular function. 43-50


MODULE 2: LINEAR
2. CONVOLUTION AND 2.Convolution with Delta Function 51-58
CORRELATION
3. Correlation 59-61

1. Fourier transform(FT) Common 62-87


functions using’fft’ and ‘fftshift’ in
MATLAB
MODULE 3: 2. Discrete Fourier Transform 88-92
3. DISCRETE FOURIER 3.Find out the DFT of a sequence
given by 𝑥[𝑛]=𝑎𝑛𝑢[𝑛] (notebook). 93-95
TRANSFORM
Write a MATLAB code for the same
for 25 elements (i.e. n=25).
4. Compute the twiddle factor of an
96-97
8-point DFT [or N = 8 ].

1. Circular Convolution. 98-109


MODULE4:
CIRCULAR 2. Overlap- save method and overlap 110-115
CONVOLUTION, – add method.
4.
OVERLAP -ADD AND
OVERLAP – SAVE
METHODS

1.Find an efficient design solution to 116-117


MODULE5: FILTER
5. the problem given below.
DESIGN 2. Find an efficient design solution to 118-121
the problem given below.
Page |1

MODULE 1: RECAP
A) Data Type:-
1.Store a = pi and explain b = unit8(a). Try this with a complex
number 'i' and 'j'.

➢ uint8 Convert to unsigned 8-bit integer.


➢ I = uint8(X) converts the elements of the array X into unsigned 8-
bit integers. X can be any numeric object, such as a DOUBLE.
➢ The values of a uint8 range from 0 to 255, or INTMIN('uint8') to
INTMAX('uint8').
➢ Values outside this range saturate on overflow, namely they are
mapped to 0 or 255 if they are outside the range. If X is already
unsigned 8-bit integer array, then uint8 has no effect.
➢ DOUBLE and SINGLE values are rounded to the nearest uint8
value on conversion.
MATLAB CODE:-
a = pi;
b = uint8(a);

a = 3i;
b = uint8(a);

OUTPUT:-

a =
3.1416
b =
uint8
3
a =
0.0 + 3.0000i
b =
uint8

0 + 3i
DISCUSSION:-

Uint8 denotes 8-bit unsigned integer arrays. It means that all variables
are stored as unsigned integers irrespective of the variable be a float or
an integer. In the output, it is shown that the variable a is converted to an
integer a shown under the variable b. The data class of ‘a’ is double
whereas the data class of ‘b’ is uint8.
Page |2

2:- Check what are maximum and minimum numbers that can be
represented in MATLAB. What will happen if you use a number
more than the maximum number.
MATLAB CODE:-

f = realmax;
g= realmin;
Sq = realmax.*2

OUTPUT:-

f =

1.7977e+308

g =

2.2251e-308

Sq =

inf

DISCUSSION

➢ The maximum number that can be represented in MATLAB is


1.7977 x 10308.

➢ The minimum number that can be represented in MATLAB is


2.2251 x 10-308.

➢ If we were to use a number more than the maximum number, it will


be permitted unless the place value of the MSB of the variable
holding the number does not change.
Page |3

B) Sinusoidal Function

1. Check the values of sin, cos and tan for angles(theta) 0 to 2*pi
with an interval of pi/4. Check exp(j(theta)) for the same values.
MATLAB CODE:-
t = [0:pi/4:2*pi]
y1 = sin(t)
plot(t,y1)
xlabel("t")
ylabel("y1")
hold on
y2 = cos(t)
plot(t,y2)
xlabel("t")
ylabel("y2")
OUTPUT:-
t =
0 0.7854 1.5708 2.3562 3.1416
3.9270 4.7124 5.4978 6.2832
y1 =
0 0.7071 1.0000 0.7071 0.0000
-0.7071 -1.0000 -0.7071 -0.0000
y2 =
1.0000 0.7071 0.0000 -0.7071 -1.0000
-0.7071 -0.0000 0.7071 1.0000
Page |4

Alternate Method
MATLAB CODE:-
t = linspace(-pi,pi);
y1 = sin(t);
plot(t,y1)
hold on
y2 = cos(t);
plot(t,y2)
hold off

OUTPUT:-
Page |5

2. How would you calculate the values directly from


degrees?(sind,cosd)

MATLAB CODE:-

t = [0:45:360]
y1 = sind(t)
plot(t,y1)
xlabel("t")
ylabel("y")
hold on
y2 = cosd(t)
plot(t,y2)
xlabel("t")
ylabel("y")
legend('y1','y2')

OUTPUT:-
Page |6

3. Display the angle and the sine values.('disp')

MATLAB CODE:-

disp(' Angles sine')


A = [ t' y1'];
disp(A)

OUTPUT:-

Angles sine
0 0
0.7854 0.7071
1.5708 1.0000
2.3562 0.7071
3.1416 0.0000
3.9270 -0.7071
4.7124 -1.0000
5.4978 -0.7071
6.2832 -0.0000

0 0
0.7854 0.7071
1.5708 1.0000
2.3562 0.7071
3.1416 0.0000
3.9270 -0.7071
4.7124 -1.0000
5.4978 -0.7071
6.2832 -0.0000
Page |7

C. Arrays, Logical Expression

1. Input two vectors A = [ 8 5 -4 30] and B = [3 2 4 -5]

MATLAB CODE:-

A = [ 8 5 -4 30]
B = [3 2 4 -5]

OUTPUT:-

A =

8 5 -4 30

B =

3 2 4 -5
Page |8

a. Perform addition, subtraction array , multiplication, and array


division(A*pinv(B)).

➢ pinv(A) returns the Moore-Penrose Pseudoinverse of matrix


A.
➢ pinv(A,tol) specifies a value for the tolerance. pinv treats
singular values of A that are smaller than the tolerance as
zero.

MATLAB CODE:-
disp("Array addition");
add = A+B
disp("Array subtraction");
sub = A-B
disp("Array multiplication");
mul = A.*B % array multiplying each element wise
disp("Array division");
div = A./B % array multiplying each element wise
pseudo_inv = A*pinv(B))
OUTPUT:-
Array addition
add =

11 7 0 25

Array subtraction

sub =

5 3 -8 35

Array multiplication

mul =

24 10 -16 -150

Array division

div =
2.6667 2.5000 -1.0000 -6.0000

pseudo_inv =
-2.4444
Page |9

b. C = [A B] and D = [A;B]

MATLAB CODE:-

C = [A B]

D = [A;B]

OUTPUT:-

C =

8 5 -4 30 3 2 4 -5
D =

8 5 -4 30

3 2 4 -5

c. C = A./B-9*A+A.^B

MATLAB CODE:-

C = A./B-9*A+A.^B

OUTPUT:-

C =

442.6667 -17.5000 291.0000 -276.0000

d. C = (B-1A-1)-3*B/5.*A

MATLAB CODE:-

C = (B-A-1)-3*B/5.*A

OUTPUT:-

C =

-20.4000 -10.0000 16.6000 54.0000


P a g e | 10

e. Use the logical expression A>B, A<B,A~=B

MATLAB CODE WITH OUTPUT:-

A>B

ans =
1×4 logical array

1 1 0 1

A<B
ans =
1×4 logical array

0 0 1 0

A~=B
ans =

1×4 logical array

1 1 1 1

% Alternate Method using loop

MATLAB CODE:-

if(A>B)
disp("element of A is greater")
elseif (B>A)
disp("B is greater")
elseif( A~=B)
disp("A is not equal to B")
else
disp("A is equal to B")
end

OUTPUT:-

A is not equal to B
P a g e | 11

2. Create array X with 10 elements starting from 1 to 19 with an


interval of 2

MATLAB CODE:-

x = 1:2:19

OUTPUT:-

x =

1 3 5 7 9 11 13 15 17 19

a. Another array y = 1./x and w = y'(transpose), u = x*y'

MATLAB CODE:-

y = 1./x

w = y'

u = x*y'

OUTPUT:-
y =

1.0000 0.3333 0.2000 0.1429 0.1111


0.0909 0.0769 0.0667 0.0588 0.0526
w =

1.0000
0.3333
0.2000
0.1429
0.1111
0.0909
0.0769
0.0667
0.0588
0.0526
u =
10
P a g e | 12

b. Create another array with 𝒆𝒙 and present the element and set of
data side by side.

MATLAB CODE:-

z = exp(x)

disp ('x y')


t = [x' z'];
disp(t);

OUTPUT:-

z =
1.0e+08 *

0.0000 0.0000 0.0000 0.0000 0.0001


0.0006 0.0044 0.0327 0.2415 1.7848

x y
1.0e+08 *

0.0000 0.0000
0.0000 0.0000
0.0000 0.0000
0.0000 0.0000
0.0000 0.0001
0.0000 0.0006
0.0000 0.0044
0.0000 0.0327
0.0000 0.2415
0.0000 1.7848
P a g e | 13

3. Matrix D contains the following values

𝟓 𝟔 𝟐
D =[−𝟏𝟎 𝟑 𝟐𝟏]
𝟒 𝟏𝟎 𝟏

Write down the values you would expect the matrix E to after
executing the following statements.

MATLAB CODE:-

D = [5 6 2; -10 3 21; 4 10 1]

OUTPUT:-
D =
5 6 2
-10 3 21
4 10 1

a. E = D(1:2,2:3)

MATLAB CODE:-

E = D(1:2,2:3)

OUTPUT:-
E =
6 2
3 21

b. E = D(1:2:3,3:-1:2)

MATLAB CODE:-

E = D(1:2:3,3:-1:2)

OUTPUT:-

E =
2 6
1 10
P a g e | 14

c. Write down the MATLAB Commands you would use to extract


the following elements from D

(i) Row 1

MATLAB CODE:-

F = D(1,:)

OUTPUT:-

F =

5 6 2

𝟓 𝟐
(ii) The Sub array [ ]
−𝟏𝟎 𝟐𝟏
MATLAB CODE:-

G = D(1:2,1:2:3)

OUTPUT:-

G =

5 2
-10 21
P a g e | 15

D) Use OF Plot Function

1. Plot angle vs sine, cosine and tangent of angle theta where


0<=theta<=2*𝝅. Plot real and imaginary values of exp(𝜽) in a
separate figure.

MATLAB CODE:-
theta = 0:10:360
sine = sind(theta);
plot(theta,sine,'-o')
hold on
cosine = cosd(theta);
plot(theta,cosine,'-b')
hold on
tang = tand(theta);
plot(theta,tang,'*k')
OUTPUT:-
P a g e | 16

%Plotting real and imaginary part of exp(jtheta)

MATLAB CODE:-

rad = theta*pi/180;
exp_func = exp(j*rad);
figure(2)
plot(rad,real(exp_func),'rd-.')
hold on
plot(rad,imag(exp_func),'ks-')
OUTPUT:-
P a g e | 17

% ALTERNATE METHOD USING LINSPACE

MATLAB CODE:-
theta= linspace(0,360,19);
s = sind(theta);
c = cosd(theta);
t = tand(theta);
% Converting theta to radians for exp(theta)
calculation.

i = exp(1i*theta*pi/180);
figure(1);
plot(theta,s,theta,c,theta,t);

OUTPUT:-
P a g e | 18

% Plotting real and imaginary part of exp(jtheta) in


alternate method

MATLAB CODE:-

re = real(i);
im = imag(i);
grid on;
figure(2);
plot(theta, re)
hold on;
grid on ;
plot(theta, im)
grid on;

OUTPUT:-
P a g e | 19

a. Label X and Y axis and put a suitable title of the graph using
'xlabel', "ylabel" and "Title" commands. Use "axis" command to
limit the X
and Y axis of the graph

MATLAB CODE:-

axis([-10 380 -6 6])


title('Circular functions ','fontsize',36)
legend('sinusoidal','cosinusoidal','tangential')
xlabel('\theta (\circ)','fontsize',20)
ylabel('\it sin(\theta),cos(\theta) and
tan(\theta)(au)','fontsize',20)

axis([0 7 -1.5 1.5])


title('Real And Imaginary value of exp(j(\theta))
curve');
legend("Real part", "Img part")
xlabel("radians")
ylabel("Amplitude")

%For Alternate method

axis([-20 370 -6 6])


title('Circular functions ','fontsize',36)
legend('sinusoidal','cosinusoidal','tangential')
xlabel('\theta (\circ)','fontsize',20)
ylabel('\it sin(\theta),cos(\theta) and
tan(\theta)(au)','fontsize',20)

axis([0 370 -1 1])


xlabel('Angles in Radians');
ylabel('real and imaginary values of
exp(j(\theta))(a.u)');
title('Real And Imaginary value of exp(j(\theta))
curve');
OUTPUT:- Outputs are shown with titles, legend,
xlabel, ylabel in next question.
P a g e | 20

b. Insert all the above commands from apart from "axis" from the
figure window.

MATLAB CODE:-

theta = 0:10:360
sine = sind(theta);
figure(1);
plot(theta,sine,'-o')
hold on
cosine = cosd(theta);
plot(theta,cosine,'-b')
hold on
tang = tand(theta);
plot(theta,tang,'*k')
title('Circular functions ','fontsize',36)
legend('sinusoidal','cosinusoidal','tangential')
xlabel('\theta (\circ)','fontsize',20)
ylabel('\it sin(\theta),cos(\theta) and
tan(\theta)(au)','fontsize',20)

OUTPUT:-
P a g e | 21

%Plotting real and imaginary part of exp(jtheta)

MATLAB CODE:-

rad = theta*pi/180;
exp_func = exp(j*rad);
figure(2)
plot(rad,real(exp_func),'rd-.')
hold on
plot(rad,imag(exp_func),'ks-')
title('Real And Imaginary value of exp(j(\theta))
curve');
legend("Real part", "Img part")
xlabel("radians")
ylabel("Amplitude")

OUTPUT:-
P a g e | 22

%FOR ALTERNATE METHOD

MATLAB CODE:-

theta= linspace(0,360,19);
s = sind(theta);
c = cosd(theta);
t = tand(theta);
i = exp(1i*theta*pi/180);
figure(1);
plot(theta,s,theta,c,theta,t);
title('Circular functions ','fontsize',36)
legend('sinusoidal','cosinusoidal','tangential')
xlabel('\theta (\circ)','fontsize',20)
ylabel('\it sin(\theta),cos(\theta) and
tan(\theta)(au)','fontsize',20)
OUTPUT:-
P a g e | 23

% Plot real and imaginary values of exp(jtheta) in a


separate figure

MATLAB CODE:-

re = real(i);
im = imag(i);
grid on;
figure(2);
plot(theta, re)
hold on;
grid on ;
plot(theta, im)
grid on;
xlabel('Angles in Radians');
ylabel('real and imaginary values of
exp(j(\theta))(a.u)');
title('Real And Imaginary value of exp(j(\theta))
curve');
OUTPUT:-
P a g e | 24

2. Create an array of time(t) using 'linspace' function. Plot the


parametric function x = cos(t), y =sin(t) and z =t using 'plot3'
function. Plot different projections of the parametric function along
XY,YZ and ZX. Plane using 'view' functions.

MATLAB CODE:-

t = linspace(0,1000,500)
t = t*pi/180
x = cos(t)
y = sin(t)
z = t
plot3(x,y,z)
grid on

OUTPUT:-
P a g e | 25

MATLAB CODE:-

t = linspace(0,2000,500)
t = t*pi/180
x = cos(t)
y = sin(t)
z = t
plot3(x,y,z)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
view(0,45)

OUTPUT:-
P a g e | 26

MATLAB CODE:-
t = linspace(0,2000,500)
t = t*pi/180
x = cos(t)
y = sin(t)
z = t
plot3(x,y,z)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
view(45,0)

OUTPUT:-
P a g e | 27

MATLAB CODE:-

t = linspace(0,2000,500)
t = t*pi/180
x = cos(t)
y = sin(t)
z = t
plot3(x,y,z)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
view(0,90)

OUTPUT:-
P a g e | 28

MATLAB CODE:-
t = linspace(0,2000,500)
t = t*pi/180
x = cos(t)
y = sin(t)
z = t
plot3(x,y,z)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
view(90,0)

OUTPUT:-
P a g e | 29

E) Generation of signals(write mathematical expression for each of


the function and describe in words of the summary of what you
have done for each of the problem).

1. Unit Step and sinusoidal functions

a. Create an array of 100 elements using "ones" and "zeros".


Create an array "unit step" by making first 50 elements to be 0 and
next 50 to be 1 . Use "stem" to plot the graph with suitable title, X
and Y labels.

MATLAB CODE:-

unit_step = [zeros(10); ones(10)]


z = zeros(1,50)
o = ones(1,50)
n = -50:49;
unit_step_func = [z o]
figure(1),
stem(n,unit_step_func);
xlabel('n'),ylabel('Unit Step')
title('Discrete Unit Step Function')
OUTPUT:-
unit_step =

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
P a g e | 30

z =

Columns 1 through 34

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0

Columns 35 through 50

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0

o =

Columns 1 through 34

1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1

Columns 35 through 50

1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1

unit_step_func =

Columns 1 through 34
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0

Columns 35 through 68
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1

Columns 69 through 100


1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1
P a g e | 31

DISCUSSION

➢ The expression of unit step function is:


u(t) = 0 for t<0 u(t) = 1 for t>0 Here, an array of 50 elements have been
taken with the value 0 and similarly 50 elements with the value 1 and
both are appended together to get an Unit Step Function
P a g e | 32

b. Create an array of t where −2𝜋 ≤ 𝑡 ≤ 2𝜋 using "linspace". Create a


sinusoidal function and plot it with respect to time.

MATLAB CODE:-
t = linspace(-2*pi,2*pi)
plot(t,sin(t))
grid on
xlabel('time')
ylabel('Sinusiodal')
hold on
OUTPUT:-

t =
Columns 1 through 20
-6.2832 -6.1563 -6.0293 -5.9024 -5.7755 -
5.6485 -5.5216 -5.3947 -5.2677 -5.1408 -
5.0139 -4.8869 -4.7600 -4.6331 -4.5061 -
4.3792 -4.2523 -4.1253 -3.9984 -3.8715
Columns 21 through 40
-3.7445 -3.6176 -3.4907 -3.3637 -3.2368 -
3.1099 -2.9829 -2.8560 -2.7291 -2.6021 -
2.4752 -2.3483 -2.2213 -2.0944 -1.9675 -
1.8405 -1.7136 -1.5867 -1.4597 -1.3328
Columns 41 through 60
-1.2059 -1.0789 -0.9520 -0.8251 -0.6981 -
0.5712 -0.4443 -0.3173 -0.1904 -0.0635
0.0635 0.1904 0.3173 0.4443 0.5712
0.6981 0.8251 0.9520 1.0789 1.2059
Columns 61 through 80
1.3328 1.4597 1.5867 1.7136 1.8405
1.9675 2.0944 2.2213 2.3483 2.4752
2.6021 2.7291 2.8560 2.9829 3.1099
3.2368 3.3637 3.4907 3.6176 3.7445
Columns 81 through 100
3.8715 3.9984 4.1253 4.2523 4.3792
4.5061 4.6331 4.7600 4.8869 5.0139
5.1408 5.2677 5.3947 5.5216 5.6485
5.7755 5.9024 6.0293 6.1563 6.2832
P a g e | 33

DISCUSSION

1. Here, an array from −2𝜋 ≤ 𝑡 ≤ 2𝜋 is taken and the sin of the array is
calculated and plotted with respect to time.
P a g e | 34

c. Multiply sin(t) and unit step function and plot the entire graph in
the same window.

MATLAB CODE:-

plot(sin(t).*(unit_step_func))
xlabel('Time')
ylabel('Sinusiodal*Unit_step')
grid on

OUTPUT:-

Discussion

Here, a unit step function and a sine function is multiplied and the result
is plotted with respect to time
P a g e | 35

2. Plot a ramp u(t) with same time axis .Provide a shift of 2 sec along t-
axis. provide a shift of 2 in t-axis

MATLAB CODE:-

figure
t = linspace(-2*pi,2*pi);
unit_step = (t);
ut = (t>=0).*unit_step;
plot(t,ut,'o-')
grid on;
xlabel('Time(sec)')
ylabel('Relative amplitude')
title('Ramp function ur(t)')
axis([-10 10 0 8])

OUTPUT:-
P a g e | 36

% providing a shift of 2 in t-axis

MATLAB CODE:-
figure;
t = linspace(-2*pi,2*pi);
unit_step = (t-2);
ut = unit_step.* (t-2>=0);
plot(t,ut,'o-')
grid on;
xlabel('Time(sec)')
ylabel('Relative amplitude')
title('Ramp function ur(t)')
axis([-10 10 0 8])

OUTPUT:-

DISCUSSION

➢ The expression for a ramp function is given as: ur(t) = 0 for t<0
ur(t) = t for t≥0
P a g e | 37

MATLAB CODE:-

% for u(t)
unit_step = (t);
ut = (t>=0).*unit_step;
stem(t,ut,'o-');
xlabel ('Time(sec)')
ylabel('Relative amplitude')
title('Ramp function ur(t)')

OUTPUT:-
P a g e | 38

MATLAB CODE:-

%for ur(t)
unit_step = (t-2);
ut = unit_step.* (t-2>=0);
stem(t,ut,'o-')
xlabel ('Time(sec)')
ylabel('Relative amplitude')
title('Ramp function ur(t-2)')

OUTPUT:-
P a g e | 39

3.Create a delta function where t is closest to pi(since "linspace"


command is used for creating "t" therefore, they may not be an
element where t = pi. First find out the absolute difference("abs")
between t and pi. After that find out the position and value of the
minimum of the above difference using "min". Select the values of
sin(t),u(t) and ur(t) by using delta function.

MATLAB CODE:-
t= [-10:1:10];
ad= abs(t-pi);
n=1:length(ad);
marker = find(ad(n)==min(ad));
disp(["Position","Value"]);
disp([marker,min(ad)])
ptr = t-t(marker);
figure (1);
stem(t,logical(dirac(ptr)));
xlabel('Angles in Radians');
ylabel('Delta function magnitude (A.U.))');
title('Delta function - Angle at t~pi');
grid;
axis([-7 7 -0.25 1.25]);
OUTPUT:-
P a g e | 40

% for u(t)
MATLAB CODE:-
figure(2);
stem(t,heaviside(ptr))
xlabel('Angles in Radians');
ylabel('Unit Step Function Magnitude (A.U.))');
title('Unit step function - Angle at t~pi');
grid;
axis([-7 7 -0.25 1.25]);

OUTPUT:-
P a g e | 41

MATLAB CODE:-

% for ur(t)
figure (3);
stem(t,(ptr.*(ptr>0)));
xlabel('Angles in Radians');
ylabel('Ramp function magnitude of (A.U.))');
title('Ramp function - Angle at t~pi');
grid;
axis([-11 11 -0.25 8]);

OUTPUT:-
P a g e | 42

MATLAB CODE:-

% for sine wave

figure (4);
stem(t,sin(ptr));
xlabel('Angles in Radians');
ylabel('Sine Wave Magnitude (A.U.))');
title('Sine Wave - Angle at t~pi');
grid;
axis([-10 10 -1.25 1.25]);

OUTPUT:-
P a g e | 43

MODULE 2: LINEAR CONVOLUTION AND


CORRELATION
1. Discrete rectangular function
a. Find out the convolution of x[n] with itself
numerically and by matrix method in your notebook
P a g e | 44
P a g e | 45
P a g e | 46
P a g e | 47

MATLAB CODE:-

x = [1 1 1 1 1];
stem(x);

OUTPUT:-
P a g e | 48

MATLAB CODE:-

hold on
z = conv(x,x)
stem(z)

OUTPUT:-
P a g e | 49

MATLAB CODE:-

s = conv(x,dirac(x))
stem(s)

OUTPUT:-
P a g e | 50

b. Create a discrete rectangular pulse as shown x[n] = [1 1 1 1 1].


Where the arrowed element represents n = 0. Perform the operation
in MATLAB using 'conv' function and plot x[n] and the convolution
in the same graph.

MATLAB CODE:-

n=-10:1:10;
x=heaviside(n+2.5)-heaviside(n-2.5);
c=conv(x,x,'same');
hold on
stem(n,x,'r');
stem(n,c,'k');
hold off
xlabel('Discrete samples n (A.U)');
ylabel('RelativeAmplitude')
title('x[n] and convolution of x[n] with itself');
grid on

OUTPUT:-
P a g e | 51

2. Convolution with delta function


a. Deduce the output function for an input x[n] convolved with
discrete delta function delta[n] in notebook.
P a g e | 52

b. Create a discrete Gaussian function and a shifted discrete delta


function and perform convolution operation in MATLAB(Hint:
‘gaussmf’)

MATLAB CODE:-
n = 0:1:10
l = length(n)
n1 = 0:(2*l-2);
g = gaussmf(n,[2 5]);
d = double(n==3);
plot(n,d);
hold on
c = conv(g,d);
hold on
plot(n1,c)
xlabel('n');
ylabel(' Amplitude');
title('Convolution of gaussian function with delta
function')
OUTPUT:-
P a g e | 53

ALTERNATE METHOD:-
MATLAB CODE:-
n=-5:1:5;
g=gaussmf(n,[1,0]);
d=logical(dirac(n-2));
c=conv(g,d,'same');
subplot(2,2,1);
stem(n,g);
xlabel('Discrete time samples [n] (a.u)');
ylabel('x[n] (a.u)');
title('Discrete Gaussian Function');
grid on
subplot(2,2,2);
stem(n,d,'k');
xlabel('Discrete time samples [n] (a.u)');
ylabel('x[n] (a.u)');
title('Shifted Discrete Delta Function');
grid on
subplot(2,2,[3,4]);
stem(n,c,'r');
xlabel('Discrete time samples [n] (a.u)');
ylabel('Amplitude (a.u)')
title('Convolution of Discrete Gaussian and Shifted
Discrete Delta Function');
grid on

OUTPUT:-
P a g e | 54
P a g e | 55

b. Create a discrete pulse train=(comb function) and find out the


convolution of it with the gaussian. Use 'pulstran' to create
pulse train

MATLAB CODE:-

n = 0:1:10
g = gaussmf(n,[2 5]);
pt = double(n==n);
stem(pt);
hold on
c = conv(g,pt);
stem(c,'o');
xlabel('n');
ylabel('x[n]');
title('Convolution of Gaussian function with pulse
train function')

OUTPUT:-
P a g e | 56

% ALTERNATE METHOD 1

MATLAB CODE:-
m = -10:10;
p = zeros(1,21);
p(1:7:21) = 1;
g = gaussmf(m,[1 0]);
c = conv(p,g);
%pt = double(m==m);
stem(m,p);
hold on;
stem (m,g);
hold off
stem(c)
xlabel('n');
ylabel('x[n]');
title('Convolution of Gaussian function with pulse
train function')

OUTPUT:-
P a g e | 57

%ALTERNATE METHOD 2 USING “pulsetran”

MATLAB CODE:-

t = 0:2:60;
d = 0:2:60;
y = abs(pulstran(t,d,'rectpuls'));
n=-10:1:10;
g=gaussmf(n,[3,0]);
c=conv(y,g,'same');
subplot(2,2,1);
stem(t,y>0)
xlabel('Discrete time samples[n](s)');
ylabel('Waveform Magnitude (a.u.)');
title('Discrete pulse train (comb function)');
grid on
subplot(2,2,2);
stem(n,g,'k');
xlabel('Discrete time samples [n]');
ylabel('x[n](a.u.)');
title('Discrete Gaussian Function');
grid on
subplot(2,2,[3,4]);
stem(t,c,'r');
xlabel('Discrete time samples[n](s)');
ylabel('Amplitude (a.u.)')
title('convolution of comb function with gaussian
function');
grid on
P a g e | 58

OUTPUT:-
P a g e | 59

3. Correlation
a. Create two 1D vectors[1 2 3 4] and [4 3 2 1] and calculate their
correlation.

MATLAB CODE:-

a = [1 2 3 4]
b = [4 3 2 1]
corre = xcorr(a,b)
stem(corre)
xlabel('n');
ylabel('amplitude');
title('Correlation of two vectors ')
xlim([0 10])
ylim([0 30])

OUTPUT:-
a =
1 2 3 4
b =

4 3 2 1
corre =

1.0000 4.0000 10.0000 20.0000 25.0000


24.0000 16.0000
OUTPUT:-
P a g e | 60

b. Perform the correlation using 'xcorr' in MATLAB. How would you


be able to perform convolution using 'xcorr' function.

MATLAB CODE:-

a = [1 2 3 4]
b = [4 3 2 1]
conv = xcorr(a,flip(b))
stem(conv);
hold on
corre = conv(a,flip(b));
stem(corre);

OUTPUT:-
conv =
4.0000 11.0000 20.0000 30.0000 20.0000
11.0000 4.0000
P a g e | 61

DISCUSSION

➢ xcorr returns the cross-correlation of two discrete-time sequences,


x and y. Cross-correlation measures the similarity between x and
shifted (lagged) copies of y as a function of the lag. If x and y have
different lengths, the function appends zeros at the end of the
shorter vector, so it has the same length, N, as the other.
➢ Correlation can be done using the following modification in the
syntax of convolution:
➢ Correlation = conv(x,fliplr(y)).
➢ This is so because the convolving a vector x with the conjugate of
another vector y gives the correlation between them. fliplr(y) is the
flipped version of vector y.
➢ To obtain convolution, we just do the opposite i.e., performing
correlation between a vector and the conjugate of another vector.
Correlation of a vector x and the conjugate of another vector y
would this give the convolution between x and y.
P a g e | 62

MODULE 3:DISCRETE FOURIER TRANSFORM

1. Fourier transform (FT) of common functions using ‘fft’ and


‘fftshift’ in MATLAB

a. Find out the FT of discrete delta functions situated at 𝑛=0,1 𝑎𝑛𝑑


2, where −10≤𝑛≤10. Plot the graphs of real, imaginary, magnitude
and phase parts, separately.

CODE:-

n=linspace(-10,10,21);
ddf0=logical(dirac(n));
ddf1=logical(dirac(n-1));
ddf2=logical(dirac(n-2));
f0=fft(ddf0);
f1=fft(ddf1);
f2=fft(ddf2);
disp([' FT of del(0) ',' FT of del(1) ',' FT of
del(2) ']);
disp([f0.',f1.',f2.']);
figure(1)
subplot(2,2,1)
plot (n,real(f0));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Real part of the fourier transform (a.u.) with
samples, n')
subplot(2,2,2)
plot (n,imag(f0));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Imaginary part of the fourier transform (a.u.)
with samples, n')
subplot(2,2,3)
plot (n,abs(f0));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
P a g e | 63

title('Absolute value of the fourier transform (a.u.)


with samples, n')
subplot(2,2,4)
plot (n,(real(f0)./imag(f0)));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Phase of the fourier transform (a.u.) with
samples, n')
figure(2)
subplot(2,2,1)
plot (n,real(f1));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Real part of the fourier transform (a.u.) with
samples, n')
subplot(2,2,2)
plot (n,imag(f1));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Imaginary part of the fourier transform (a.u.)
with samples, n')
subplot(2,2,3)
plot (n,abs(f1));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Absolute value of the fourier transform (a.u.)
with samples, n')
subplot(2,2,4)
plot (n,(real(f1)./imag(f1)));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Phase of the fourier transform (a.u.) with
samples, n')
figure(3)
subplot(2,2,1)
plot (n,real(f2));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
P a g e | 64

title('Real part of the fourier transform (a.u.) with


samples, n')

subplot(2,2,2)
plot (n,imag(f2));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Imaginary part of the fourier transform (a.u.)
with samples, n')
subplot(2,2,3)
plot (n,abs(f2));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Absolute value of the fourier transform (a.u.)
with samples, n')
subplot(2,2,4)
plot (n,(real(f2)./imag(f2)));
grid on;
xlabel('Discrete Samples,n (a.u.)');
ylabel('Amplitude (a.u.)');
title('Phase of the fourier transform (a.u.) with
samples, n')

OUTPUT:-Next Page
P a g e | 65
P a g e | 66
P a g e | 67
P a g e | 68

%ALTERNATE PROOF.

MATLAB CODE:-

x=linspace(-10,10,21);
d1=logical(dirac(x));
d2=logical(dirac(x-1));
d3=logical(dirac(x-2));
f1=(fft(d1));
f2=(fft(d2));
f3=(fft(d3));
figure(1);
subplot(2,2,1);
plot(x,real(f1));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of real value of fft (dirac delta
at x=0)');
title('Real part vs Discrete sample values');

subplot(2,2,2);
plot(x,imag(f1));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft (dirac
delta at x=0)');
title('Imaginary part vs Discrete sample values');

subplot(2,2,3);
plot(x,abs(f1));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft (dirac
delta at x=0)');
title('Imaginary part vs Discrete sample values');

subplot(2,2,4);
plot(x,atand(imag(f1)./real(f1)));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of Imaginary value of fft (dirac
delta at x=0)');
title('Imaginary part vs Discrete sample values');
P a g e | 69

figure(2)
subplot(2,2,1);
plot(x,real(f2));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of real value of fft (dirac delta
at x=1)');
title('Real part vs Discrete sample values');

subplot(2,2,2);
plot(x,imag(f2));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft (dirac
delta at x=1)');
title('Imaginary part vs Discrete sample values');

subplot(2,2,3);
plot(x,abs(f2));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft (dirac
delta at x=1)');
title('Imaginary part vs Discrete sample values');

subplot(2,2,4);
plot(x,atand(imag(f2)./real(f2)));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft (dirac
delta at x=1)');
title('Imaginary part vs Discrete sample values');

figure(3)
subplot(2,2,1);
plot(x,real(f3));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of real value of fft (dirac delta
at x=2)');
title('Real part vs Discrete sample values');

subplot(2,2,2);
plot(x,imag(f3));
P a g e | 70

grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft (dirac
delta at x=2)');
title('Imaginary part vs Discrete sample values');

subplot(2,2,3);
plot(x,abs(f3));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft{dirac
delta at x=2}');
title('Imaginary part vs Discrete sample values');

subplot(2,2,4);
plot(x,atand(imag(f3)./real(f3)));
grid on;
xlabel('x (discrete values)');
ylabel('Amplitude of imaginary value of fft{dirac
delta at x=2}');
title('Imaginary part vs Discrete sample values');

OUTPUT:-Next Page
P a g e | 71
P a g e | 72
P a g e | 73
P a g e | 74

b. Find out the FT of discrete cosinusoidal and sinusoidal signals with 5


cycles over the entire array-range of 𝑛 = 100 𝑎𝑛𝑑 101. Plot the graphs of
real, imaginary, magnitude, and phase parts, separately with frequency
axis properly calculated. Check how the FT-patterns vary if the no. of
arrays are equal to 2p, where 𝑝 = 6,7,8 𝑎𝑛𝑑 16.

MATLAB CODE:-

n=linspace(1,100,100);
n6=linspace(1,65,64);
n7=linspace(1,129,128);
n8=linspace(1,257,256);
n16=linspace(1,65537,65536);
fs = fft(sin(5*2*pi.*n));
fc=fft(cos(5*2*pi.*n));
fs6 = fft(sin(5*2*pi.*n6));
fs7 = fft(sin(5*2*pi.*n7));
fs8 = fft(sin(5*2*pi.*n8));
fs16 = fft(sin(5*2*pi.*n16));
figure(1)
subplot(2,2,1);
plot(n,real(fs));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
grid on;
ylabel('Amplitude (a.u.)');
title('Real part of the fourier transform (a.u.) with
samples, n');
subplot(2,2,2);
plot(n,imag(fs));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
grid on;
ylabel('Amplitude (a.u.)');

title('Imaginary part of the fourier transform (a.u.)


with samples, n');

subplot(2,2,3);
plot(n,angle(fs));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
grid on;
ylabel('Amplitude (a.u.)');
P a g e | 75

title('Phase of the fourier transform (a.u.) with


samples, n');
subplot(2,2,4);
plot(n,abs(fs));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
grid on;
ylabel('Amplitude (a.u.)');
title('Absolute value of the fourier transform (a.u.)
with samples, n');
figure(2)
subplot(2,2,1);
plot(n6,fs6);
xlabel('Discrete Samples over a range of n =2^6
elements (a.u.)');
ylabel('Amplitude (a.u.)');
title('Fourier transform (a.u.) with samples, n');
grid on;
subplot(2,2,2);
plot(n7,fs7);
xlabel('Discrete Samples over a range of n =2^7
elements (a.u.)');
ylabel('Amplitude (a.u.)');
title('Fourier transform (a.u.) with samples, n');
grid on;
subplot(2,2,3);
plot(n8,fs8);
xlabel('Discrete Samples over a range of n =2^8
elements (a.u.)');
ylabel('Amplitude (a.u.)');
title('Fourier transform (a.u.) with samples, n');
grid on;
subplot(2,2,4);
plot(n16,fs16);
xlabel('Discrete Samples over a range of n =2^1^6
elements (a.u.)');
ylabel('Amplitude (a.u.)');
title('Fourier transform (a.u.) with samples, n');
grid on;
figure(3)
subplot(2,2,1);
plot(n,real(fc));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
P a g e | 76

grid on;
ylabel('Amplitude (a.u.)');
title('Real part of the fourier transform (a.u.) with
samples, n');
subplot(2,2,2);
plot(n,imag(fc));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
grid on;
ylabel('Amplitude (a.u.)');
title('Imaginary part of the fourier transform (a.u.)
with samples, n');
subplot(2,2,3);
plot(n,angle(fc));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
grid on;
ylabel('Amplitude (a.u.)');
title('Phase of the fourier transform (a.u.) with
samples, n');
subplot(2,2,4);
plot(n,abs(fc));
xlabel('Discrete Samples over a range of n =100
(a.u.)');
grid on;
ylabel('Amplitude (a.u.)');

title('Absolute value of the fourier transform (a.u.)


with samples, n');

OUTPUT:-Next Page
P a g e | 77
P a g e | 78
P a g e | 79
P a g e | 80

% ALTERNATE METHOD

MATLAB CODE:-
t=linspace(0,10*pi,100);
t = rad2deg(t);
y=sin(t);
fs=fft(y);
figure(1);
subplot(2,1,1);
stem(t,y);
xlabel('discrete t');
ylabel('sin t');
subplot(2,1,2);
stem(t,fs);
xlabel('discrete t');
ylabel('F.T{(sin t)} ');
%cos starts here
x=cos(t);
fc=fft(x);
figure(2);
subplot(2,1,1);
stem(t,x);
xlabel('discrete t');
ylabel('cos t');
subplot(2,1,2);
stem(t,fc);
xlabel('discrete t');
ylabel('F.T{(cos t)} ');

figure(3);
subplot(2,2,1);
stem(t,real(fs));
ylabel('Real of (F.T{sin t})');
xlabel('t');
subplot(2,2,2);
stem(t,imag(fs));
ylabel('Imaginary of (F.T{sin t})');
xlabel('t');
subplot(2,2,3);
stem(t,abs(fs));
ylabel('Magnitude of (F.T{sin t})');
xlabel('t');
subplot(2,2,4);
stem(t,imag(fs)./real(fs));
P a g e | 81

ylabel('Phase of (F.T{sin t})');


xlabel('t');

figure(4);
subplot(2,2,1);
stem(t,real(fc));
ylabel('Real of (F.T{cos t})');
xlabel('t');
subplot(2,2,2);
stem(t,imag(fc));
ylabel('Imaginary of (F.T{cos t})');
xlabel('t');
subplot(2,2,3);
stem(t,abs(fc));
ylabel('Magnitude of (F.T{cos t})');
xlabel('t');
subplot(2,2,4);
stem(t,imag(fc)./real(fc));
ylabel('Phase of (F.T{cos t})');
xlabel('t');

OUTPUT:-Next Page
P a g e | 82
P a g e | 83
P a g e | 84
P a g e | 85
P a g e | 86

c. Find out the FT of discrete rectangular pulse.

MATLAB CODE:-

t=(-10:1:10);
y = rectpuls(t,10);
disp(['Rectangular Pulse,t ', ' Fourier Transform of
t']);
disp([y.',fft(y).']);
stem(t,(abs(fftshift((fft(y))))));
xlabel('Discrete Samples, n');
ylabel('Amplitude of Fourier Transform of Discrete
Rectangular Pulse(a.u.)');
title('Absolute Fourier transform of Discrete
Rectangular Pulse vs samples');
grid on

OUTPUT:-
P a g e | 87

% ALTERNATE METHOD:-

MATLAB CODE:-

T = 1;
t = -2.5 : 0.01 : 2.5;
x = rectpuls(t,T);
subplot(2,1,1)
plot(t,x,'r');
axis([-2.5 2.5 0 2])
title({'Rectangular Pulse'})
xlabel({'Time(s)'});
ylabel('Ampltude');
f=fft(x);
subplot(2,1,2)
axis([-5 5 0 5])
plot(t,fftshift(abs(f)));
title('Fourier transform of a rectangular pulse');

OUTPUT:-
P a g e | 88

2. Discrete Fourier transform


a. Find DFT by matrix multiplication of the sequence 𝑥[𝑛]=[1 2
3 4] in your notebook.
P a g e | 89
P a g e | 90

%Using MATLAB

MATLAB CODE:-

clear
clc
x=[1; 2; 3; 4]; % create 4x4 array
a = zeros(4);
[p,m] = size(a);
for k = 1:1:p
for n = 1:1:m
s=(k-1)*(n-1);
a(k,n ) =exp((-1*1j*2*pi*s)/4);
end
end
disp(a*x);

OUTPUT:-

10.0000 + 0.0000i
-2.0000 + 2.0000i
-2.0000 - 0.0000i
-2.0000 - 2.0000i
P a g e | 91

b. Write the code in MATLAB for the same. Use the command “fft”
(algorithm for Fast Fourier transform) in MATLAB to compute the same
for N=25. Write which is faster in the discussion (use ‘tic’ and ‘toc’).

MATLAB CODE:-

clear
clc
x=linspace(1,25,25);x=x';
a = zeros(25);
[p,m] = size(a);

%ALTERNATE METHOD
x= [1 2 3 4];
n=4;
N=25;
tic
if(N>length(x))
for i=1:N-length(x)
x=[x 0];
end
end

X=[];
xx=0;

for k=0:N-1
for n=0:N-1
xx=xx+x(n+1)* exp(-j*2*pi*n*k/N);
end
X=[X xx];
xx=0;
end
toc
tic
Y = fft(x);
toc
%by Matix method
tic
for k = 1:1:p
for n = 1:1:m
s=(k-1)*(n-1);
a(k,n )=exp((-1*1*j*2*pi*s)/4);
end
P a g e | 92

end
toc
disp((a*x));
%by fft
tic
y=(fft(x));
toc;disp((y));

OUTPUT:-

Elapsed time is 0.022184 seconds.


Elapsed time is 0.004037 seconds.
Columns 1 through 7
10.0000 + 0.0000i 8.4820 - 4.6808i 4.6113 - 7.4886i
0.0966 - 7.4452i -3.1741 - 4.9045i -4.0451 - 1.3143i
-2.6003 + 1.5571i
Columns 8 through 14
-0.0208 + 2.5171i 2.1105 + 1.4966i 2.6682 - 0.5212i
1.5451 - 2.1266i -0.3758 - 2.3019i -1.7976 - 0.9771i
-1.7976 + 0.9771i
Columns 15 through 21
-0.3758 + 2.3019i 1.5451 + 2.1266i 2.6682 + 0.5212i
2.1105 - 1.4966i -0.0208 - 2.5171i -2.6003 - 1.5571i
-4.0451 + 1.3143i
Columns 22 through 25
-3.1741 + 4.9045i 0.0966 + 7.4452i 4.6113 + 7.4886i
8.4820 + 4.6808i

DISCUSSION

➢ From the above Output, we can conclude that using the command
FFT was much faster than using the Code for finding the Discrete
Fourier Transform of the sequence.
P a g e | 93

3. Find out the DFT of a sequence given by 𝒙[𝒏] = 𝒂𝒏 × 𝒖[𝒏]


(notebook). Write a MATLAB code for the same for 25
elements (i.e. n=25).
P a g e | 94

MATLAB CODE:-

ar = 1:25;
z= ar.*0;
o=ar./ar;
u=[z,o];
for n = 1:25
x = (2.^n).*u;
f = fft(x);
end
disp(f);

OUTPUT:-
1.0e+08 *
Columns 1 through 9
8.3886 + 0.0000i -0.3355 + 5.3333i 0.0000 +
0.0000i -0.3355 + 1.7590i 0.0000 - 0.0000i -
0.3355 + 1.0327i -0.0000 - 0.0000i -0.3355 +
0.7131i -0.0000 + 0.0000i
Columns 10 through 18
-0.3355 + 0.5287i 0.0000 + 0.0000i -0.3355 +
0.4056i -0.0000 - 0.0000i -0.3355 + 0.3151i
0.0000 + 0.0000i -0.3355 + 0.2438i 0.0000 -
0.0000i -0.3355 + 0.1845i
Columns 19 through 27
-0.0000 - 0.0000i -0.3355 + 0.1329i 0.0000 +
0.0000i -0.3355 + 0.0862i 0.0000 + 0.0000i -
0.3355 + 0.0424i 0.0000 + 0.0000i -0.3355 +
0.0000i 0.0000 - 0.0000i
Columns 28 through 36
-0.3355 - 0.0424i 0.0000 - 0.0000i -0.3355 -
0.0862i 0.0000 - 0.0000i -0.3355 - 0.1329i -
0.0000 + 0.0000i -0.3355 - 0.1845i 0.0000 +
0.0000i -0.3355 - 0.2438i
Columns 37 through 45
0.0000 - 0.0000i -0.3355 - 0.3151i -0.0000 +
0.0000i -0.3355 - 0.4056i 0.0000 + 0.0000i -
0.3355 - 0.5287i -0.0000 + 0.0000i -0.3355 -
0.7131i -0.0000 + 0.0000i
Columns 46 through 50
-0.3355 - 5.3333i
P a g e | 95

% Alternate Method

MATLAB CODE:-
clear;clc
% for a = 2
a=2;n = linspace(1,25,25);
y=a.^n; x = y.*heaviside(n);
x1=x';
a = zeros(25);
[p,m] = size(a);
for k = 1:1:p
for n = 1:1:m
s=(k-1)*(n-1);
a(k,n ) =exp((-1*1j*2*pi*s)/4);
end
end
disp(a*x1);
OUTPUT:-
1.0e+07 *
6.7109 + 0.0000i
2.6844 + 1.3422i
2.2370 + 0.0000i
2.6844 - 1.3422i
6.7109 + 0.0000i
2.6844 + 1.3422i
2.2370 + 0.0000i
2.6844 - 1.3422i
6.7109 + 0.0000i
2.6844 + 1.3422i
2.2370 + 0.0000i
2.6844 - 1.3422i
6.7109 + 0.0000i
2.6844 + 1.3422i
2.2370 - 0.0000i
2.6844 - 1.3422i
6.7109 + 0.0000i
2.6844 + 1.3422i
2.2370 + 0.0000i
2.6844 - 1.3422i
6.7109 + 0.0000i
2.6844 + 1.3422i
2.2370 + 0.0000i
2.6844 - 1.3422i
6.7109 + 0.0000i
P a g e | 96

4. Compute the twiddle factor of an 8-point DFT [or N = 8].

MATLAB CODE:-

for N = 0:7
TF = exp((-j*2*pi).*N/8);
disp(TF);
end

OUTPUT:-

0.7071 - 0.7071i

0.0000 - 1.0000i

-0.7071 - 0.7071i

-1.0000 - 0.0000i

-0.7071 + 0.7071i

-0.0000 + 1.0000i

0.7071 + 0.7071i
P a g e | 97

%Alternate Method:

MATLAB CODE:-

clear
clc

a = zeros(8);
[p,m] = size(a);
% twiddle factor

for k = 1:1:p
for n = 1:1:m
s=(k-1)*(n-1);
a(k,n ) =exp((-1*1j*2*pi*s)/4);
end
end

disp(a);

OUTPUT:-
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 - 1.0000i -1.0000 - 0.0000i
-0.0000 + 1.0000i 1.0000 + 0.0000i 0.0000 - 1.0000i
-1.0000 - 0.0000i -0.0000 + 1.0000i
1.0000 + 0.0000i -1.0000 - 0.0000i 1.0000 + 0.0000i
-1.0000 - 0.0000i 1.0000 + 0.0000i -1.0000 - 0.0000i
1.0000 + 0.0000i -1.0000 - 0.0000i
1.0000 + 0.0000i -0.0000 + 1.0000i -1.0000 - 0.0000i
0.0000 - 1.0000i 1.0000 + 0.0000i -0.0000 + 1.0000i -
1.0000 - 0.0000i -0.0000 - 1.0000i
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 - 1.0000i -1.0000 - 0.0000i
-0.0000 + 1.0000i 1.0000 + 0.0000i -0.0000 - 1.0000i
-1.0000 - 0.0000i -0.0000 + 1.0000i
1.0000 + 0.0000i -1.0000 - 0.0000i 1.0000 + 0.0000i
-1.0000 - 0.0000i 1.0000 + 0.0000i -1.0000 - 0.0000i
1.0000 + 0.0000i -1.0000 + 0.0000i
1.0000 + 0.0000i -0.0000 + 1.0000i -1.0000 - 0.0000i
-0.0000 - 1.0000i 1.0000 + 0.0000i -0.0000 + 1.0000i
-1.0000 + 0.0000i 0.0000 - 1.0000i
P a g e | 98

MODULE 4:CIRCULAR CONVOLUTION, OVERLAP -


ADD AND OVERLAP -SAVE METHODS
1. Circular convolution
a) Problems to be solved analytically (A4)
I. Find out the circular convolution of two column
vectors 𝑥[𝑛]=[1 ,−1 ,−2 ,3 ,−1] and
ℎ[𝑛]=[1,2,3] using concentric circle method.
Appropriately use zero-padding, if required.
P a g e | 99
P a g e | 100
P a g e | 101

MATLAB CODE:-

clear
clc
x=[1 -1 -2 3 -1];
x1=(x');
h=[1 2 3 0 0];
h1=[1 0 0 3 2; 2 1 0 0 3; 3 2 1 0 0; 0 3 2 1 0; 0 0 3
2 1];
y=h1*(x1);
disp(y);
fx=fft(x);
fh=fft(h);
a=ifft(fx.*fh);
disp(a);
h2=[circshift(h,1);circshift(h,2);circshift(h,3);circ
shift(h,4);circshift(h,5)];
disp(h2*x1);

OUTPUT:-

Value of y:
8
-2
-1
-4
-1

Value of a:

8.0000 -2.0000 -1.0000 -4.0000 -1.0000

Value of h2*x1:
4
1
4
-2
-7
P a g e | 102

II. Solve Prob. 1(a) using matrix method.


P a g e | 103

1.a.II) Solve Problem 1(a) using Matrix Method. Solve In Copy

MATLAB CODE:-

clear
clc
x=[1 -1 -2 3 -1];
h=[1 2 3 0 0];
a = zeros(5);
[p,m] = size(a);

for k = 1:1:p
for n = 1:1:m
s=(k-1)*(n-1);
a(k,n ) =exp((-1*1j*2*pi*s)/5);
end
end

dftx=a*x';
dfth=a*h';
disp(dftx)
disp(dfth)

OUTPUT:-

disp(dftx)

0.0000 + 0.0000i
-0.4271 + 2.9389i
2.9271 - 4.7553i
2.9271 + 4.7553i
-0.4271 - 2.9389i

disp(dfth)

6.0000 + 0.0000i
-0.8090 - 3.6655i
0.3090 + 1.6776i
0.3090 - 1.6776i
-0.8090 + 3.6655i
P a g e | 104

1.b) Problems to be solved in MATLAB (pdf)


I. Write a MATLAB program to the solution of Prob. 1(a(I))
using the command ‘cconv’ with appropriate choice of
resulting vector length. Show that without this choice use
of ‘cconv’ and ‘conv’ result in same vector.

MATLAB CODE:-

x = [1 -1 -2 3 -1];
h = [12 3];
h = [1 2 3];
y = cconv(x,h,5);

OUTPUT:-

y =

8.0000 -2.0000 -1.0000 -4.0000 -1.0000


P a g e | 105

MATLAB CODE:-

%ALTERNATE METHOD

clc
clear
x = [1 -1 -2 3 -1];
h = [1 2 3];
c = cconv(x,h,5);
l=conv(x,h);
N1=length(x);
N2=length(h);
N=max(N1,N2);
N3=N1-N2;
if(N3>0)
h=[h,zeros(1,N3)];
else
x=[x,zeros(1,-N3)];
end
for n=1:N
f(n)=0;
for i=1:N
j=n-i+1;
if(j<=0)
j=N+j;
end
f(n)=[f(n)+(x(i)*h(j))];
end
end
disp([c.', f.'])

OUTPUT :-

8.0000 8.0000
-2.0000 -2.0000
-1.0000 -1.0000
-4.0000 -4.0000
-1.0000 -1.0000
P a g e | 106

1.b.II. Write a program in MATLAB to implement circular


convolution using matrix method of prob. 1(a(II)) and show the
results in pdf-file. Hint: Use MATLAB command
"gallery('circul',vector)" and/or "circshift" to create the circulant
matrix of h[n]. check whether the circulant matrix is same as the
matrix obtained analytically.

MATLAB CODE:-

x = [1 -1 -2 3 -1];
x1 = (x');
h = [ 1 2 3 0 0]
h2 = [circshift(h,0)]
circshift(h,1);
circshift(h,2);
circshift(h,3);
circshift(h,4);
z = h2'.* x1

OUTPUT:-

h =
1 2 3 0 0

h2 =
1 2 3 0 0
z =
1
-2
-6
0
0
P a g e | 107

%ALTERNATE METHOD

MATLAB CODE:-

x1 = [1 -1 -2 3 -1];
h1 = [1 2 3];
hpad = [h1 zeros(1,5-length(h1))];
m1 = gallery('circul',x1);
m2 = transpose(hpad);
cc= m1.*m2;
ccf= cc(1,:)+cc(2,:) + cc(3,:);
disp(ccf);

OUTPUT:-

8 -2 -1 -4 -1

DISCUSSION

➢ Observation here was that the circulant matrix obtained using


MATLAB was just the transpose of the same obtained analytically.
The result obtained was also transpose of each other.
P a g e | 108

1.b.III. Write a MATLAB Program to solution of Prob. 1(a(I)) using


DFT .Hint : Y(k) = IDFT{DFT(h[n]).DFT(x[n])}

MATLAB CODE:-

x = [1 -1 -2 3 -1];
h = [1 2 3 0 0];
a = zeros(5);
[p, m ] = size(a);

%By Matrix method

for k = 1:1 :p
for n = 1:1:m
s = (k-1)*(n-1);
a(k,n) = exp((-1*1j*2*pi*s)/5);
end
end

dftx = a*x';
dfth = a*h';
disp(ifft(dftx.*dfth))

OUTPUT:-

8.0000 + 0.0000i
-2.0000 - 0.0000i
-1.0000 - 0.0000i
-4.0000 - 0.0000i
-1.0000 - 0.0000i
P a g e | 109

%ALTERNATE METHOD

MATLAB CODE:-

x1 = [1 -1 -2 3 -1];
h1 = [1 2 3];
xpad = [x1 zeros(1,5-length(x1))];
hpad = [h1 zeros(1,5-length(h1))];
ccirc = ifft(fft(xpad).*fft(hpad));
disp(ccirc);

OUTPUT:-

8.0000 -2.0000 -1.0000 -4.0000 -1.0000

1.b.IV)Comment on the circulant matrix and resultant vector


obtained from different methods.

Ans:-

The circulant matrix circulates column wise. Which means the last
element of the 1st column will be the first element of the 2nd column
followed by the elements of the 1st column in order except the last
element, and so on for other columns in the square matrix. The resultant
vector obtained by using different methods will be same.

In the different Methods used to achieve circular convolution, the


circulant matrix was similar in all cases but just it was transposed in the
analytical method as compared to what was achieved in MATLAB. The
resultant vector though was same in all methods of both the analytical
and non-analytical method. Again, it is observed that the resultant vector
was transposed in the matrix method computed analytically.

Since every method is doing the same thing which is, finding the
convolution of a given sequences.
P a g e | 110

2. Overlap -save method and overlap - add method.


2.a) Find the output y(n) of a filter whose impulse
response is h(n) = [1 1 1] and input signal x(n)= [-
3,-1,0,1,3,2,0,1,2,1] by using Overlap-save method(in
A-4)
P a g e | 111
P a g e | 112
P a g e | 113

I. Overlap - add method (in A-4).


Use MATLAB to compute the necessary circular convolutions.
P a g e | 114
P a g e | 115
P a g e | 116

MODULE 5: FILTER DESIGN

1. Find an efficient design solution to the problem given below.


The entire document has to be submitted as an e-copy with
comment on the type of filter . The code length should be
minimum.Design a 25-point low-pass FIR filter with cut-off
frequency of 0.5pi radian using .
a) Rectangular,b) Hamming and ,c) Hanning windows and plot
their frequency responses.
Hint: "freqz" command in MATLAB.

MATLAB CODE:-
cutoff_freq= 0.7*pi;N= 20;
alpha= (N-1)/2;eps= 0.001;
n= 0:1:N-1;
hd= sin(cutoff_freq*(n-alpha+eps))./(pi*(n-
alpha+eps));
wr= boxcar(N);hn= hd.*wr';
w= 0:0.01:pi;
h= freqz(hn,1,w);
subplot(2,1,1)
plot(w/pi, abs(h));
hold on
wh= hamming(N);hn= hd.*wh';
w= 0:0.01:pi;
h= freqz(hn,1,w);
plot(w/pi, abs(h), 'r');
hold on
xlabel('Normalised frequency \Omega / \pi')
ylabel('Magnitude')
title('Rectangular and Hamming window')
legend('Rectangular', 'Hamming')
hold off
whn= hanning(N);
hn= hd.*whn';
w= 0:0.01:pi;
h= freqz(hn,1,w);
subplot(2,1,2)
plot(w/pi, abs(h), 'g');
xlabel('Normalized frequency \Omega / \pi')
ylabel('Magnitude')
title('Hanning window')
P a g e | 117

OUTPUT:-
P a g e | 118

2. Find an efficient design solution to the problem given below.


The entire document has to be submitted as an e-copy with
comment on the type of filter. The code Length should be
minimum.

a) Design a Butterworth LPF with the following specifications:


Pass band frequency = 400 Hz, stop band frequency = 800 Hz,
Sampling frequency = 2000Hz, passband attenuation = 0.4dB
and stop band attenuation = 30dB. Plot The Magnitude and
phase response of the filter.

MATLAB CODE:-

alpha_pass= 0.6;
alpha_stop= 40;
fp= 400;
fs= 800;
F= 2000;
om_pass= 2*fp/F;
om_stop= 2*fs/F;
[n,wn]=
buttord(om_pass,om_stop,alpha_pass,alpha_stop);
[b,a]= butter(n,wn)
w= 0:0.01:pi;
[h,om]= freqz(b,a,w,'whole');
m= abs(h);
an= angle(h);
subplot(2,1,1); plot(om/pi, 20*log(m), 'r');
ylim([-600 200])
xlabel('Normalised frequency');
ylabel('Gain in Decibels (dB) ');
title('Butterworth Low Pass Filter')
subplot(2,1,2)
plot(om/pi, an, 'b');
xlabel('Normalised frequency');
ylabel('Phase in radians');
P a g e | 119

OUTPUT:-
b =
0.0890 0.3559 0.5339 0.3559 0.0890
a =
1.0000 -0.0674 0.4875 -0.0141 0.0178
P a g e | 120

b) Design a Butterworth HPF with the following specifications:


Pass Band Frequency = 800Hz, stop band frequency = 400Hz,
sampling frequency = 2000Hz, passband attenuation = 0.4dB and
stop band attenuation = 30dB. Plot the magnitude and phase
response of the filter.

MATLAB CODE:

alpha_pass= 0.6;
alpha_stop= 40;
fp= 400;
fs= 800;
F= 2000;
om_pass= 2*fp/F;
om_stop= 2*fs/F;
[n,wn]=
buttord(om_pass,om_stop,alpha_pass,alpha_stop);
[b,a]= butter(n,wn, 'high')
w= 0:0.01:pi;
[h,om]= freqz(b,a,w);
m= 20*log(abs(h));
an= angle(h);
subplot(2,1,1)
plot(om/pi, m, 'r');
ylim([-600 200])
xlabel('Normalised frequency');
ylabel('Gain in Decibels (dB) ');
title('Butterworth High Pass Filter')
subplot(2,1,2)
plot(om/pi, an, 'b');
xlabel('Normalised frequency');
ylabel('Phase in radians');
P a g e | 121

OUTPUT:-
b =
0.0992 -0.3967 0.5950 -0.3967 0.0992
a =
1.0000 -0.0674 0.4875 -0.0141 0.0178

You might also like