Signal Processing Lab
Signal Processing Lab
Signal Processing Lab
MODULE 1: RECAP
A) Data Type:-
1.Store a = pi and explain b = unit8(a). Try this with a complex
number 'i' and 'j'.
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
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
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
MATLAB CODE:-
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
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
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 =
d. C = (B-1A-1)-3*B/5.*A
MATLAB CODE:-
C = (B-A-1)-3*B/5.*A
OUTPUT:-
C =
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 1 1 1
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
MATLAB CODE:-
x = 1:2:19
OUTPUT:-
x =
1 3 5 7 9 11 13 15 17 19
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
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)
OUTPUT:-
z =
1.0e+08 *
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
𝟓 𝟔 𝟐
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
(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
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
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
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
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:-
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
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
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
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
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
MATLAB CODE:-
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
DISCUSSION
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
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
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:-
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
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
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
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
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
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 =
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
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
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
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.)');
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
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.)');
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
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
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
%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:-
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
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
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
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:
Value of h2*x1:
4
1
4
-2
-7
P a g e | 102
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
MATLAB CODE:-
x = [1 -1 -2 3 -1];
h = [12 3];
h = [1 2 3];
y = cconv(x,h,5);
OUTPUT:-
y =
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
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
MATLAB CODE:-
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(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:-
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.
Since every method is doing the same thing which is, finding the
convolution of a given sequences.
P a g e | 110
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
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
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