DSP Lab Report 3 Updated
DSP Lab Report 3 Updated
Class: BEE-6A
Submitted To: Sir Fawad Zaman
Task 1:
Take an exponential signal and perform scaling operation with a negative integer as given in
In-Lab Work section and plot the result.
Introduction:
The task generates two exponential signals using the exp() function, scales the second signal
using a constant factor of -2, and plots both signals using stem plots. The script first generates
an input time axis n using the : operator with start time -5 and end time 5. It then generates an
exponential signal x using the exp() function with n as the input. It also generates a scaled
exponential signal x1 using the same exp() function, but multiplies the output by a constant
factor of -2. The subplot() function is used to create two subplots in a single figure window. The
first subplot is created using subplot(1,2,1) which specifies that there will be 1 row, 2 columns
of subplots, and that the first subplot will be active. The stem() function is then used to plot the
original signal x with a red color and linewidth of 2.
Code:
%%%%%%%%%%Program for scaling operation%%%%%%%%%%
clear all
close all
n = -5:5; %input
x = exp(n);
x1 = -2*exp(n); %Scaling formula
subplot(1,2,1);
stem(n, x, 'r', 'LineWidth', 2);
title 'Exponential plot' %graph title
xlabel 'Time --->'; %labeling
ylabel 'Amplitude --->';
subplot(1,2,2);
stem(n, x1, 'r', 'LineWidth', 2);
title 'Exponential plot scaled by -2' %graph title
xlabel 'Time --->'; %labeling
ylabel 'Amplitude --->';
Figure:
Critical Analysis:
The code demonstrates the effect of scaling on a discrete-time signal. The signal is defined
using the exponential function exp(n). The scaling factor -2 is multiplied with the exponential
function to obtain the scaled version x1=-2*exp(n).
Task 2:
Write a Matlab function “sigshift” for producing a delay of ‘k’ in a given sequence ‘x[n]’
defined by arrays “x” and “n” by using the pseudo code given in In-Lab Work section. Your
function should yield y[n] = x[n-k].
function [y,n]=sigshift(x,n,k)
Introduction:
The use of the sigshift function to shift a ramp signal by a certain amount. In the first section,
the code initializes the input signal x and the range of values n that the signal will take. In the
second section, the sigshift function is called with the input signal x, range of values n, and the
amount of shift k1. The output signal y1 and the corresponding range n1 are assigned to
variables. Finally, the two signals are plotted side by side in two subplots using the subplot
function. The first subplot shows the unshifted signal, and the second subplot shows the signal
shifted by the amount specified in k1. The axis function is used to set the limits of the plot.
Function (sigshift):
function [y,n] = sigshift(x,n,k)
z1 = zeros(1,k);
if k>0
n = n(1):n(end)+k;
y = horzcat(z1,x);
elseif k<0
n = n(1):n(end)+k;
y = horzcat(x,z1);
end
end
Code:
%%%%%%%%%%Program of sigshift for producing a delay of ‘k’%%%%%%%%%%
clear all
close all
n = -5:5;
k1 = 2;
x = 0:10;
subplot(1,2,1);
stem(n, x, 'r', 'LineWidth', 2); %denoted
title 'Ramp signal (Unshifted)' %graph title
xlabel 'Time --->'; %Labeling
ylabel 'Amplitude --->';
axis ([-7 7 0 10]) %axix
subplot(1,2,2);
stem(n1, y1, 'b', 'LineWidth', 2);
title 'Ramp signal (Shifted by -2)' %graph title
xlabel 'Time --->'; %Labeling
ylabel 'Amplitude --->';
Figure:
Critical Analysis:
This code provides a clear example of how to use the sigshift function to shift signals in
MATLAB.
Task 3:
Write a Matlab function “sigfold” for folding a given sequence ‘x[n]’ defined by arrays
“x” and “n” by using the pseudo code given in In-Lab Work section.
function [y,n]=sigfold(x,n)
Introduction:
The concept of signal folding using a unit impulse function. It first creates an impulse function
with time indices shifted by -1 using the impulse() function. Then, it applies the sigfold()
function to fold the signal about t=0. In the first subplot, the original unit impulse function with
time indices [-5,5] is plotted. In the second subplot, the folded unit impulse function about t=0
is plotted with time indices [5,-5]. The two subplots show how signal folding reverses the time
axis of the original signal.
Function (sigfold):
function [y,n] = sigfold(x,n)
y = -fliplr(x);
end
Code:
n = -5:5;
x = impulse(n-1); %Impulse response
subplot(1,2,1);
stem(n, x, 'r', 'LineWidth', 2); %denoted
title 'Unfolded unit Impulse function' %graph title
xlabel 'Time --->'; %Labeling
ylabel 'Amplitude --->';
subplot(1,2,2);
stem(n1, y, 'k', 'LineWidth', 2); %denoted
title 'Folded unit Impulse function' %graph title
xlabel 'Time --->'; %Labeling
ylabel 'Amplitude --->';
Figure:
Critical Analysis:
The code is implementing signal folding operation using the sigfold function. It first generates
an impulse signal shifted by one sample to the right using the impulse function. Then, it uses
sigfold to fold the impulse signal about the y-axis. Finally, it plots both the original impulse
signal and the folded signal using the stem function. The code is correct and achieves the
desired output of demonstrating the signal folding operation. However, it would be better to
add comments to the code to explain the purpose of each step and the concept of signal
folding.
Task: 4
Write a Matlab function “sigadd” for adding two sequences x1[n] and x2[n] by using the
pseudo code given in In-Lab Work section.
function [y,n]=sigadd(x1,n1,x2,n2)
Introduction:
The code defines two input signals x1 and x2, with corresponding sample index vectors n1 and
n2, and then uses the sigadd function to add the two signals together and obtain the output
signal y and its corresponding sample index vector n. The three subplots show the input signals
x1 and x2, and the output signal y, respectively. The stem function is used to plot the signals as
discrete points, with different colors and line widths for each subplot. The title, xlabel, and
ylabel functions are used to add labels to the graphs, and the axis function is used to set the x
and y limits for each subplot. Finally, the grid on function adds a grid to the plot.
Function (sigadd):
function [y,n] = sigadd(x1,n1,x2,n2)
if n1(1)<n2(1)
z1 = zeros(1,n2(1)-n1(1));
x2 = horzcat(z1,x2);
n2(1) = n1(1);
elseif n2(1)<n1(1)
z1 = zeros(1,n1(1)-n2(1));
x1 = horzcat(z1,x1);
n1(1) = n2(1);
end
if n2(end)<n1(end)
z2 = zeros(1,n1(end)-n2(end));
x2 = horzcat(x2,z2);
n2(end) = n1(end);
elseif n1(end)<n2(end)
z2 = zeros(1,n2(end)-n1(end));
x1 = horzcat(x2,z2);
n1(end) = n2(end);
end
n = n1(1):n2(end);
y = x1 + x2;
Code:
%%%%%%%%%%Program of sigadd for adding two sequences%%%%%%%%%%
clear all
close all
n1 = 1:10;
n2 = 1:5;
x1 = ones(1,10);
x2 = ones(1,5);
subplot(2,2,1);
stem(n1, x1, 'r', 'LineWidth', 2);
title 'Step function 1' %graph title
xlabel 'Time --->'; %labeling
ylabel 'Amplitude --->';
axis ([0 11 0 2]); %axix
subplot(2,2,2);
stem(n2, x2, 'k', 'LineWidth', 2);
title 'Step function 2' %graph title
xlabel 'Time --->'; %labeling
ylabel 'Amplitude --->';
axis ([0 5 0 2]); %axix
subplot(2,2,[3 4]);
stem(n, y, 'g', 'LineWidth', 2);
title 'Addition' %graph title
xlabel 'Time --->'; %labeling
ylabel 'Amplitude --->';
axis ([0 11 0 2]); %axix
grid on;
Figure:
Critical Analysis:
In this task we came to know about the ‘sigadd’ function and the use of it. Also we saw the
addition of the two sequences(step functions) by the use of pseudo code.
Task 5:
Let x ( n )={1 , 2 ,3 , 4 , 5 ,6,7 , 6 , 5 , 4 ,3 , 2 ,1 }
Determine and plot the following sequences
a. x 1=2 x ( n−5 )−3 x (n+ 4)
b. x 2=x ( 3−n ) + x (n) x( n−2)
Introduction:
This code appears to define a sequence x using a commented out line (which is not actually
executed), and then defines two sample index vectors n, k1, and k2. It then uses the x function
to generate two sequences y1 and y2, with corresponding sample index vectors n1 and n2.
Next, the sigsub function is used to subtract 3 times y2 from 2 times y1, with corresponding
sample index vector nof, and the result is stored in x1. Finally, the stem function is used to plot
the result x1 as discrete points, with a red color and a line width of 2, and the title, xlabel, and
ylabel functions are used to label the graph.
Code:
%%%%%%%%%%Program for determination of sequences%%%%%%%%%%
clear all
close all
%x = [1 2 3 4 5 6 7 6 5 4 3 2 1];
n = 0:12;
k1 = -5;
k2 = 4;
Figure:
Critical Analysis:
In this task we studied about the subtraction of the sequences.
Inroduction:
This code appears to define a sample index vector n, as well as two other sample index vectors
na2, k1, and k2. It then uses the x function to generate three sequences y1, y2, and y3, with
corresponding sample index vectors n1, n2, and n3. Next, the sigmulti function is used to
multiply y3 by y2, with corresponding sample index vector n4, and the result is stored in y4.
Then, the sigadd function is used to add y1 and y4, with corresponding sample index vectors n1
and n4, and the result is stored in x1. Finally, the stem function is used to plot the result x1 as
discrete points, with a red color and a line width of 2, and the title, xlabel, and ylabel functions
are used to label the graph.
Code:
%%%%%%%%%%Program for determination of sequences%%%%%%%%%%
clear all
close all
%x = [1 2 3 4 5 6 7 6 5 4 3 2 1];
n = 0:12;
na2 = 12:-1:0;
k1 = 3;
k2 = -2;
[y1,n1] = x(na2,k1);
[y2,n2] = x(n,k2);
[y3,n3] = x(n,0);
Figure:
Conclusion:
Analysis have been performed on discrete time signals. The changes due to signal processing
and manipulation have been also been observed. Signal scaling done in the lab scales the value
of amplitude of the signal, shifting done on signals results in the signal being either delayed or
advanced along the x-axis. Folding a signal mirrors it about both the x, and y axes. Addition,
Multiplication and Subtraction is done using created functions.