Sunteți pe pagina 1din 9

Lab #4-5

COMSATS INSTITUE OF INFORMATION TECHNOLOGY

Basic Operations on 1-D

Marks:- 10
Objective:
By the end of this lab students will be able to do plotting of different type of basic
discrete-time sequences, arithmetic operations on sequences. Moreover they will be able
to read and display audio files/image files.

Pre-Lab Tasks:

Useful MATLAB Commands:


• Plot: Plots vector Y versus vector X.
• Stem: plots the data sequence Y as stems from the x axis terminated with
circles for the data value.
• wavread: Reads a WAVE file specified by the string FILE, returning the
sampled data
• auread: Loads a sound file specified by the string AUFILE, returning the
sampled data
• :
• plus: Adds matrices X and Y

Background Review
R1.1A discrete-time signal is represented as a sequence of numbers, called samples. A sample
value of a typical discrete-time signal or sequence{x[n]} is denoted as x[n] with the argument
n being an integer in the range−∞ and∞.For convenience, the sequence {x[n]}is often denoted
without the curly brackets.

R1.2The discrete-time signal may be a finite length or an infinite length sequence. A finite length
(also called finite duration or finite extent) sequence is defined only for a finite time interval:
N1≤ n ≤ N2,
(1.1)
where−∞ < N 1andN2< ∞ with N 2≥ N1.The length or duration N of the finite length sequence is
N = N 2− N 1+ 1 .
(1.2)
R1.3 A sequence x% [n] satisfying
x% [n + kN ] for all n,
[ n] = x% (1.3)
is called a periodic sequence with a period N where N is a positive integer and k is any integer.
Lab #4-5
Lab #4-5
Lab #4-5

MATLAB commands used:

Real-valued exponential Sequence


• The operator “.^” is required to implement a real exponential sequence. For
example, to generate x(n) = (0.9)n, 0<= n<=10, we will need the following script:

• Example tutorial: >> n = [0:10]; x = (0.9).^n;

• Create a function (M-file) to generate an exponential sequence. Use the previous


examples as your guide.

Complex-valued exponential Sequence


A matlab function “exp” is used to generate exponential sequences.
For example, to generate x(n) = exp[(2+j3)n], 0<= n<=10, we will need the following
script:
>> n = [0:10]; x = exp((2+3j)*n);

Note that you need different plot commands for plotting real and imaginary components.
Lab #4-5

Sinusoidal sequence
A matlab function “cos” (or sin) is used to generate sinusoidal sequences.
To generate x(n) = 3cos(0.1πn + π/3) + 2sin(0.5 πn),
0<= n<=10, we will need the following script:

>> n = [0:10]; x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);

Random Sequences
A random or stochastic sequences are characterised by parameters of the associated
probability density functions or their statistical moments. In matlab, 2 types of (psuedo )
random sequences are avalable: “rand(1,N)” generates a length N random sequence whos
elements are uniformly distributed between 0 and 1. “randn(1,N) generates a length N
gaussian random sequence with mean 0 and variance 1. Other random sequences can be
generated suing transformation of the above functions. (Check previous lecture slides)

Periodic sequence
A sequence is periodic if x(n) = x(n +N). To generate P periods of x(n) from one period,
we can copy x(n) P times:

>> xtilde = [x,x,x,x...,x];

An elegant approach is to use matlab’s indexing capabilites:


Generate a matrix containing P rows of x(n) values. Concatenate P rows into a long row
vector using the construct (:).
Have to use matrix transposition operator (‘) to provide the same effect on rows:

>> xtilde = x' * ones(1,P); %P columns of x; x is a row vector


>> xtilde = xtilde(:); %long coloumn vector
>> xtilde = xtilde'; %long row vector

In-Lab Tasks:

Task-1:
Create a function “impseq”, which performs following operations:
Function [x,n]=impseq(n0,n1,n2)
• Takes three parameters (n0, n1, n2) as input, where ‘n1’ and
‘n2’ are lower and upper limits of n-axis, and ‘n0’ is the delay.
• Generates a unit-impulse sequence using above mentioned three parameters.
• There should be two out put arguments [x, n] of function ‘impseq’, where ‘x’
is impulse sequence and ‘n’ is its corresponding n-axis.
• Finally, plot unit impulse ‘x’ against vector ‘n’.
Lab #4-5

• On the main window, type “impseq(0,-5,5)”


o Unit Sample Sequence
 The resulting plot looks like this
Unit Sample Sequence
2

1.8

1.6

1.4

1.2
x(n)

0.8

0.6

0.4

0.2

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n

Task-2:
Modify above function to form “stepseq” function which will out put unit-step
sequence.
Function [x,n]=stepseq(n0,n1,n2)
 Unit Step Sequence
 We can have another elegant way to produce a step function
 Alternatively, we can use the “ones” function

Unit Step Sequence


 Type “stepseq(0,-5,5)” we get:
Lab #4-5

Unit Sample Sequence


2

1.8

1.6

1.4

1.2
x(n)

0.8

0.6

0.4

0.2

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n

Signal Scaling
Each sample is multiplied by a scalar. Use the command “*” for scaling.

Signal Shifting
Each sample of x(n) is shifted by an amount k to obtain a shifted sequence y(n)
y ( n) = {x(n − k )}

Task-3:
Write a matlab function “sigshift” for producing a delay of ‘n0’ in a given
sequence ‘x’.
Function [y,n]=sigshift(x,m,n0)
Where ‘x’ is given sequence, ‘m’ is indices vector and ‘n0’ represents delay.

Folding
Each sample is of x(n) is flipped around n=0 to obtain a folded sequence y(n)
y ( n) = {− x(n)}

Implement “sigfold” by “fliplr(x)” & “-fliplr(x)”.

Sinusoidal Sequences
Lab #4-5

Another very useful class of discrete-time signals is the real sinusoidal sequence of the
form of Eq.(1.12). Such sinusoidal sequences can be generated in MATLAB using
thetrigonometric operatorscos andsin.
Program P14 is a simple example that generates a sinusoidal signal.
% Program P1_4
% Generation of a sinusoidal sequence
n = 0:40;
f = 0.1;
phase = 0;
A = 1.5;
arg = 2*pi*f*n - phase;
x = A*cos(arg);
clf;
% Clear old graph
stem(n,x);
% Plot the generated sequence
axis([0 40 -2 2]);
grid;
title(’Sinusoidal Sequence’);
xlabel(’Time index n’);
ylabel(’Amplitude’);
axis;

Post-Lab Tasks: Time Allowed:- 1 week

Task-1:

Modify Program P1_4 to generate a sinusoidal sequence of frequency0.9 and display it.
Now, modify Program P_14 to generate a sinusoidal sequence of frequency1.1 and
display it. Compare this new sequence with the one generated in P1_4. Comment on your
results.

Task-2:

Modify Program P1_4 to generate a sinusoidal sequence of length50, frequency0.08,


amplitude 2.5, and phase shift 90 degrees and display it. What is the period of this
sequence?

Task-3:
Create a function “sigadd” to add two sequences ‘x1’ and ‘x2’.
Lab #4-5

Function [y,n]=sigadd(x1,n1,x2,n2)
Where ‘x1’ and ‘x2’ are two sequences and ‘n1’ and ‘n2’ are there respective
indices vectors. Add values of ‘x1’ and ‘x2’ at corresponding indices, pad zeros if
length of two sequences are not same.

Task-4:
Create a function “play_sound” that can:
• Read and play recorded sound file
• Plot samples from 100 to 300 of that file

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. x1 (n) = 2 x(n − 5) − 3 x(n + 4)
b. x2 (n) = x(3 − n) + x(n) x(n − 2)

Tips:
• Use ‘:’ operator to generate /access an array. Type “help: “to get more detail.
• Read out help of “Function”.
• Use “stem” function for plotting ‘x’ against ‘n’.

S-ar putea să vă placă și