CpE 109L Digital Signal Processing                                                                                             AVG
Exercise No. 2
                                   Introduction to different types of discrete time signals and their plotting
OBJECTIVES
          1.    Illustrate how to create a function in MATLAB.
          2.    Show different types of discrete time signals and their plotting.
          3.    Show x[n] where ‘n’ is integer valued and represents discrete instances in time.
          4.    Construct a .m file function that perform finite duration sequence.
EQUIPMENT
     Quantity
        1            PC with MATLAB Software per student
BASIC
       MATLAB is a powerful programming language as well as an interactive computational environment. Files that contain code in the
MATLAB language are called M-files. You create M-files using a text editor, then use them as you would any other MATLAB function or
command.
There are two kinds of M-files:
• Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace.
• Functions, which can accept input arguments and return output arguments.
Internal variables are local t the function. If you’re a new MATLAB programmer, just create the M-files that you want to try out in the current
directory. As you develop more of your own M-files, you will want to organize them into other directories and personal toolboxes that you
can add to your MATLAB search path. If you duplicate function names, MATLAB executes the one that occurs first in the search path.
To view the contents of an M-file, for example, myfunction.m, use
type myfunction
      Scripts
           When you invoke a script, MATLAB simply executes the commands found in the file. Scripts can operate on existing data in the
workspace, or they can create new data on which to operate. Although scripts do not return output arguments, any variables that they
create remain in the workspace, to be used in subsequent computations. In addition, scripts can produce graphical output using functions
like plot.
Example 1. For example, create a file called magicrank.m that contains these MATLAB commands.
          % Investigate the rank of magic squares
          r = zeros(1,32);
          for n = 3:32
          r(n) = rank(magic(n));
          end
          r
          bar(r)
Laboratory Experiment                                                                                                                  Page 9
CpE 109L Digital Signal Processing                                                                                                 AVG
           Typing the statement
           magicrank
          causes MATLAB to execute the commands, compute the rank of the first 30 magic squares, and plot a bar graph of the result.
After execution of the file is complete, the variables n and r remain in the workspace.
Example 2. Another example, create a file called sample.m that contains these MATLAB commands.
%sampling & reconstruction
%creating "analog" signal
clear; %clears all variables
t=0:.1:20;
F1=.1;
F2=.2;
x=sin (2*pi*F1*t) +sin (2*pi*F2*t);
%plotting
figure (1);
subplot (2,1,1);
plot (t,x);
title('Original signal')
xlabel('t');
ylabel('x(t)');
subplot(2,1,2);
x_samples=x(1:10:201); %gets 21 samples of x.
stem(x_samples,'filled');
title('Sampled signal')
xlabel('n');
ylabel('x_s(n)');
axis([0 20 -2 2]);
           Typing the statement sample observe what happen. Write your observation below.
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
                      Functions
            Functions are M-files that can accept input arguments and return output arguments. The names of the M-file and of the function
should be the same. Functions operate on variables within their own workspace, separate from the workspace you access at the MATLAB
command prompt. The main feature of a function file is that it has an input and output. This means that the calculation inside the function
file is carried out using the input data, and the results of calculations are transferred out of the function file by the output. The input and the
output can be or several variables and each can be a scalar, vector, or an array of any size. Schematically, a function file can be illustrated
by:
                                       Input data                   Function                  Output
                                                                      File
Laboratory Experiment                                                                                                                     Page 10
CpE 109L Digital Signal Processing                                                                                             AVG
                                      function [output arguments] = function_name(input arguments)
           The word function                      A list of output               The name of                 A list of input
           must be the first word,                arguments typed                the function.               arguments typed
           and must be type in                    inside brackets                                            inside parenthesis.
           lower-case letters
           Input and Output Arguments
          The input and output arguments are used to transfer data into and out of the function. The input arguments are listed inside
parenthesis following the function name. Usually, there is at least one input arguments, although it is possible to have a function that has
no input arguments. If there are more than one, the input arguments are separated with commas.
           Function definition line                          Comments
           function[A] = RectArea(a,b)                       Two input arguments, one output arguments
           function A = RectArea(a,b)                        Same as above, one output argument can be typed without the brackets.
           function[V,S] = sphereVolArea(r)                  One input variable, two output variables.
           function trajectory(v,h,g)                        Three input arguments, no output arguments
         Discrete time signals are defined only at certain specific values of time. They can be represented by x[n] where ‘n’ is integer
valued and represents discrete instances in time. i.e:
X[n] = {…., x[-1], x[0] ,x[1] ,…..}
          Where the up-arrow indicates the sample at n=0. In MATLAB, we can represent a finite-duration sequence like above by a row of
vector of appropriate values. However such a vector does not have any information about sample position n. therefore a correct
representation of x[n] would require two vectors, one each for x and n. for example a signal:
X[n] = {2,1,-1,0,1,4,3}
>>n=[-3,-2,-1,0,1,2,3];
>>x=[2,1,-1,0,1,4,3];
• An arbitrary infinite duration signal cannot be represented by MATLAB due to finite memory limitations.
Basic Signals
           1.   Unit Sample Sequence
                                                                                      3.   Unit Ramp Sequence
           2.   Unit Step Sequence
                                                                                      4.   Exponential Sequence
Laboratory Experiment                                                                                                                 Page 11
CpE 109L Digital Signal Processing                                                                                          AVG
            5.   Sinusoidal sequence
                 X[n] = cos(won+θ), for all n
PROCEDURE
       1.   Make a new impseq.m file, use function below to plot unit sample sequence. Store it in a folder.
function[x, n] =impseq(n1, n2)
% Generates x[n] =delta (n) ; n1<=n<=n2
% n1 is lower limit of required sequence;n2 is upper limit of required sequence
n=n1:n2;x=(n)==0;
stem(n,x);
title(‘Unit Sample Impulse’);
xlabel(‘n’);
ylabel(‘x[n]’);
       2.   After creating the impseq.m function, create a testimpseq.m below to test the function and perform the unit sample sequence
            graph. Store impseq.m and testimpseq.m in one folder.
echo on; clc;
clc; clear all; format long;
pause % Press any key to continue.
clc;
n1 = -8;
n2 = 8;
[x,n] = impseq(n1,n2)
       3.   After doing procedure no.1&2 run the testimpseq.m and observe what happen. How the code satisfy the condition of unit sample
            sequence? Draw the output graph.
        ______________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
Laboratory Experiment                                                                                                              Page 12
CpE 109L Digital Signal Processing                                                                                            AVG
       4.   Make a new stepseq.m file, use function below to plot unit step sequence. Store it in a folder.
function[x,n] = stepseq(n1,n2)
%Generates x[n] = u(n); n1<=n<=2
%n1 is lower limit of required sequence; n2 is upper limit of required sequence
n=n1:n2;x=(n)>=0;
stem(n,x)
title('Unit Step Sequence')
xlabel('n Time index')
ylabel('x[n] Amplitude')
       5.   After creating the stepseq.m function, create a teststepseq.m below to test the function and perform the unit step sequence
            graph. Store stepseq.m and teststepseq.m in one folder.
echo on; clc;
clc; clear all; format long;
pause % Press any key to continue.
clc;
n1 = -8;
n2 = 8;
[x,n] = stepseq(n1,n2)
       6.   After doing procedure no.4&5 run the teststepseq.m and observe what happen. How the code satisfy the condition of unit step
            sequence? Draw the output graph.
        ______________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
Laboratory Experiment                                                                                                               Page 13
CpE 109L Digital Signal Processing                                                                                         AVG
       7.   Make a new Ramp.m file, use function below to plot unit step sequence. Store it in a folder.
function[x,n] = Ramp(n1,n2)
%Generates x[n] = ur(n); n1>=n1<=2
%n1 is lower limit of required sequence; n2 is upper limit of required sequence
n=n1:n2;x= n.*(n >= 0);
stem(n,x)
title('Unit Ramp Sequence')
xlabel('n Time index')
ylabel('x[n] Amplitude')
       8.   After creating the Ramp.m function, create a testRamp.m below to test the function and perform the unit ramp sequence graph.
            Store Ramp.m and testRamp.m in one folder.
echo on; clc;
clc; clear all; format long;
pause % Press any key to continue.
clc;
n1 = -15;
n2 = 15;
[x,n] = Ramp(n1,n2)
       9.
        After doing procedure no.7&8 run the testRamp.m and observe what happen. How the code satisfy the condition of unit ramp
        sequence? Draw the output graph.
        ______________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
Laboratory Experiment                                                                                                            Page 14
CpE 109L Digital Signal Processing                                                                                      AVG
                                                  Real-valued Exponential Sequence
                                                              X[n] = a^n
     10. In MATLAB command window, write
          >> n=0:10; x=(0.9).^n
     11. Write and observe the result of executing the above command.
         ______________________________________________________________________________________________________
     __________________________________________________________________________________________________________
     __________________________________________________________________________________________________________
     __________________________________________________________________________________________________________
     __________________________________________________________________________________________________________
     __________________________________________________________________________________________________________
     __________________________________________________________________________________________________________
     __________________________________________________________________________________________________________
                                                     Complex-valued Exponential Sequence
     12. MATLAB function exp is used to generate exponential sequences. For example to generate X[n]=exp[(2+3j)n],0<=n<=10,
         we can write:
          >>n=0:10;x=exp((2+3j)*n)
          MATLAB RESULT OF ABOVE COMMAND:
          x=
          1.0e+008 *
          Columns 1 through 4
          0.00 -0.00+0.00i 0.00-0.00i -0.00+0.00i
          Columns 5 through 8
          0.00-0.00i -0.0002+0.0001i 0.0011-0.0012i -0.0066+0.0101i
          Columns 9 through 11
          0.0377-0.0805i -0.1918+0.6280i 0.7484-4.7936i
EXERCISES
Exercise no.1. Modify the function of impseq.m and testimpseq.m to perform the following sequence.
                   X(n-3) - delay
                   X(n+3) - advance
Exercise no.2. Modify the function of stepseq.m and teststepseq.m to perform the following sequence.
                   X(n-3) - delay
                   X(n+3) - advance
Exercise no. 3. Modify the input argument of unit ramp signal code the Ramp.m and testRamp.m. by changing the value of upper limit and
                lower limit of the function.
Exercise no. 4 Generate .m file for this signal
Laboratory Experiment                                                                                                         Page 15
CpE 109L Digital Signal Processing                                                                                      AVG
                                                     Ramp signal
                  15
                  10
      Amplitude
                  0
                       0                      5                        10                           15
                                                     Time Index
Exercise no.5. Generate a complex exponential given below in MATLAB and also plot the output and compare it with the stem output.
              X[n] = 2e(-0.5+(π/6)j)n
Exercise no. 6. Generate a sinusoidal signal given below in MATLAB and also plot and stem the output.
              X[n] = 3cos(0.1nπ+π/3) + 2sin(0.5nπ)
Laboratory Experiment                                                                                                         Page 16
CpE 109L Digital Signal Processing                   AVG
Exercise no.1                        Exercise no.2
Laboratory Experiment                                      Page 17
CpE 109L Digital Signal Processing                   AVG
Exercise no.3                        Exercise no.4
Laboratory Experiment                                      Page 18
CpE 109L Digital Signal Processing                   AVG
Exercise no.5                        Exercise no.6
Laboratory Experiment                                      Page 19
CpE 109L Digital Signal Processing                                                                AVG
Generalization:
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
Laboratory Experiment                                                                                   Page 20