Beruflich Dokumente
Kultur Dokumente
Exercise No. 2
Introduction to different types of discrete time signals and their plotting
OBJECTIVES
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.
• 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.
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.
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.
%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:
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.
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:
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
5. Sinusoidal sequence
PROCEDURE
1. Make a new impseq.m file, use function below to plot unit sample sequence. Store it in a folder.
n=n1:n2;x=(n)==0;
stem(n,x);
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.
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.
______________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
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)
n=n1:n2;x=(n)>=0;
stem(n,x)
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.
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.
______________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
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)
stem(n,x)
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.
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.
______________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
11. Write and observe the result of executing the above command.
______________________________________________________________________________________________________
__________________________________________________________________________________________________________
__________________________________________________________________________________________________________
__________________________________________________________________________________________________________
__________________________________________________________________________________________________________
__________________________________________________________________________________________________________
__________________________________________________________________________________________________________
__________________________________________________________________________________________________________
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)
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.
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.
Generalization:
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________
______________________________________________________________________________________________________________