You are on page 1of 9

Digital Signal Processing

Experiment 1

Name: Rooshan Khan


Roll number: 2021-EE-067
Code1:
t = 0:0.01:1;
N = length(t);
xt = zeros(1,N);
for n = 1:N
temp = 0;
for k = 1:2:10
temp = temp + (1/k)*sin(2*pi*k*t(n));
end
xt(n) = temp;
end
plot(t, xt, 'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of x(t)'); % Set title for the plot
Code Description:
Generates a time vector t from 0 to 1 with a step of 0.01. Computes and plots a Fourier series
approximation of a function using odd sine terms. Labels the axes ('t in sec' for x-axis and 'x(t)'
for y-axis) and sets the plot title ('Plot of x(t)').

Graph:

Figure Description:
● This graph resembles a square wave.
● If we change array k from “1:2:5” to “1:2:100” we will get a better square wave.

Code2:
t = 0:0.01:1; xt = zeros(1,length(t));
for k = 1:2:5
xt = xt + (1/k)*sin(2*pi*k*t);
end
plot(t, xt, 'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of x(t)'); % Set title for the plot

Graph:
Description:
This code aims to achieve the same result as the previous code.However, these two codes have
some differences in terms of efficiency and readability. Code2 is shorter and may be more
readable to users familiar with MATLAB's vectorized operations. Code1 is more explicit in its
nested loop structure, which may aid understanding for those less familiar with vectorization.
Efficiency: Code2 is likely to be more computationally efficient due to vectorization, especially
for larger datasets. Code1 uses nested loops, which can be less efficient than vectorized
operations in MATLAB.

Code3:
t = 0:0.01:1; k = 1:2:5;
xt = (1./k)*sin(2*pi*k'*t);
plot(t, xt, 'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of x(t)'); % Set title for the plot
Graph:
Description:
The provided code is the most vectorized and concise, making use of MATLAB's strengths in
matrix operations. Due to its extensive vectorization, this code is likely the most efficient. The
provided code is concise and may be more readable to users familiar with MATLAB's
vectorization.

Code4:
% Script file to implement example 1
t = 0:0.01:1;
k = 1:2:5;
ck = 1./(k.*k);
xt = ck * sin(2*pi*k'*t);
plot(t, xt, 'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of x(t)'); % Set title for the plot
Graph:
Figure Description:
The shape of this graph resembles the sine graph. The maximum value is between 0.8 and 1.0.
The minimum value is between -1.0 and –0.8. The zeros occur at t= 0s, 0.5s and 1s.

Code5:
t = 0:0.01:2; % sample points from 0 to 2 in steps of 0.01
xt = sin(2*pi*t); % Evaluate sin(2πt)
plot(t, xt, 'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of sin(2\pi t)'); % Set title for the plot
Graph:
Figure Description:
The above code provides the sine graph with 2 cycles. Period of the wave is 1 s. Maximum value
of 1 occurs at t=0.25s and t=1.25s. Minimum value of -1 occurs at t=0.75s and t=1.75s. Zeros
occur at t=0s,0.5s,1s,1.5s and 2s.
Code6:
n = 0:1:40; % Sample index from 0 to 40
xn = sin(0.1*pi*n); % Evaluate sin(0.1πn)
stem(n, xn, 'b', 'filled', 'markersize', 4); % Stem plot
xlabel('n'); ylabel('x(n)'); % Label axes
title('Stem Plot of sin(0.1\pi n)'); % Set title for the plot
Graph:
Figure Description:
This code provides the sampled version of the sine graph with period N=20. Two cycles are
shown. The maximum value of 1 occurs at n=5 and n=25. The maximum value of -1 occurs at
n=-15 and n=-35. The zeros occur at n= 0, 10,20,30 and 40.
Code7:
plot(t, xt, 'b'); hold on; % Create plot with blue line
Hs = stem(n*0.05, xn, 'b', 'filled'); % Stem plot with handle Hs
set(Hs, 'markersize', 4); hold off; % Change size of the marker
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of sin(2\pi t)'); % Set title for the plot
Code Description:
plot(t, xt, 'b');: Plots the variable xt against t with a blue line. hold on;: Holds the current plot so
that subsequent plots are overlaid.
Hs = stem(n*0.05, xn, 'b', 'filled');: Creates a stem plot of the variables xn against n*0.05 with
filled blue markers. The handle Hs is assigned to the stem plot.
set(Hs, 'markersize', 4);: Sets the marker size of the stem plot to 4. hold off;: Releases the hold
on the current plot.
xlabel('t in sec'); ylabel('x(t)');: Labels the x-axis as 't in sec' and the y-axis as 'x(t)'.
title('Plot of sin(2\pi t)');: Sets the title of the plot as 'Plot of sin(2πt)'.
Graph:
Figure Description:
The code generates a single figure with two overlaid plots. The first plot is a continuous plot of
sin(2πt) represented by a blue line. The second plot is a stem plot of sin(0.1/πn) with blue filled
markers. The marker size for the stem plot is adjusted to 4. The x-axis is labeled as 't in sec,' the
y-axis as 'x(t),' and the plot is titled 'Plot of sin(2πt).'
Code8:
subplot(2,1,1); % Two rows, one column, first plot
plot(t, xt, 'b'); % Create plot with blue line
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of sin(2\pi t)'); % Set title for the plot
subplot(2,1,2); % Two rows, one column, second plot
Hs = stem(n, xn, 'b', 'filled', 'markersize', 4); % Stem plot
xlabel('t in sec'); ylabel('x(t)'); % Label axes
title('Plot of sin(0.1/pi n)'); % Set title for the plot
Code Description:
subplot(2,1,1);: This command creates a grid of 2 rows and 1 column of subplots and activates
the first subplot for further plotting.
plot(t, xt, 'b');: This line plots the variable xt against the variable t using a blue line ('b').
xlabel('t in sec'); ylabel('x(t)');: These commands label the x-axis as 't in sec' and the y-axis as
'x(t)'.
title('Plot of sin(2\pi t)');: This sets the title of the first subplot as 'Plot of sin(2πt)'.
subplot(2,1,2);: This command activates the second subplot.
Hs = stem(n, xn, 'b', 'filled', 'markersize', 4);: This line creates a stem plot (stem) of the
variables xn against n. The stems are filled with blue color ('b'), and the marker size is set to 4.
xlabel('t in sec'); ylabel('x(t)');: These commands label the x-axis as 't in sec' and the y-axis as
'x(t)' for the second subplot.
title('Plot of sin(0.1/pi n)');: This sets the title of the second subplot as 'Plot of sin(0.1/πn)'.
Graph:

Figure Description:
The code overall creates a figure with two subplots, each containing a different plot. The first
subplot shows a continuous plot of sin(2πt), and the second subplot shows a discrete stem plot of
sin(0.1/πn).

Conclusion:
These codes tell the different ways to achieve the same result. They tell how continuous time
graphs and discrete time graphs can be made. I understood the parameters of different functions.

You might also like