Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

The University of Lahore

Department of Computer Engineering

Signals and Systems (CMP-09201)

LAB NO. 6: Implementation of Signal Properties in


MATLAB

Objectives:

 The objectives of this session is to perform different operations on basic


signal sequences in MATLAB.

Equipment and Software:

 MATLAB latest Version

Introduction:
A single is classified with respect to its domain and range. Similarly operations on signals are
classified into two categories.
1. Operations on domain
2. Operations on Range
Both categories will be dealt in the current laboratory.

1. Operations on Domain:
Domain operations are those which involve time axis or
integer axis as main focus of operations. Domain operations are classified into following four
categories.
1. Time Shifting
2. Time Scaling
3. Time Reversal
4. Sampling

1. Time Shifting
Time Shift or delay operation shifts the signals to the desired delay. Given
a signal x(t) a shifted signal will be of the form y (t)= x(t- to) where to is the delay or shift
in time domain. Let Asin(2πft) is the signal which is desired to be shifted by an amount
to.
Here is the MATLAB code for it.
% Time Delay/ Time Shifting
inc=0.1;
t=-10:inc:10;
f=0.1;
t0=2; % Shift Units
a=2; % Amplitude
x=a*sin(2*pi*f*t);
y=a*sin(2*pi*f*(t-t0));
subplot(311);
hold on;
plot(t,x);
plot(t,y,'r');
title('Original and Shifted Signal');
subplot(312);
plot(t,x);
title('Original SIgnal');
subplot(313);
plot(t,y);
title('Shifted SIgnal');

Figure 6.1 Time shifting operation


For discrete signal each sample of x (n) is shifted by an amount k to obtain the shifted sequence

y(n).

y (n) ={x (n-k)}

If we let m=n-k then n=m+k and we get

y(m+k)={x(m)}

MATLAB code for the function of time shift operation is as follows:

function[y,n]=sigshift(x,m,n0)
n=m+n0;
y=x;

2. Time Scaling
Operation of time scaling scales the time axis to a certain scale resulting
in increasing or decreasing the frequency of the signal which compresses or expands the
signal on time domain. General expression for the time scaled output is given below.

Let Asin(2πft) is the original signal on time scale. We scale the t domain by amount alpha and
beta. Alpha scaled signal will be compresses due to increment in frequency while beta scaled
signal will be expanded.

Exampled is coded below.


%time Scaling
inc=0.1;
t=-10:inc:10;
f=0.1;
alpha=2; % Compression Units
a=2; % Amplitude
beta=0.5; % Expension units
x=a*sin(2*pi*f*t);
y=a*sin(2*pi*f*(alpha*t));
z=a*sin(2*pi*f*(beta*t));
subplot(311);
plot(t,x);
title('Original Signal');
subplot(312);
plot(t,y);
title('Time scaled Compressed SIgnal');
subplot(313);
plot(t,z);
title('Time Scaled Expanded SIgnal');

Figure 6.2 Time scaling operation

3. Time Reversal

Time Reversal operation flips each sample of the signal about t=0 or n=0 to
obtain a folded sequence.

In MATLAB fliplr(x) function is used to flip the sample values and –fliplr(x) is used to flip
the indices.

inc=0.1;
t=-10:inc:10;
f=0.1;
a=2; % Amplitude
x=a*sin(2*pi*f*t);
rx=fliplr(x);
rt=-1*fliplr(t);
l=length(x);
subplot(311);
hold on;
plot(t(l/2:l),x(l/2:l));
plot(rt(1:l/2),rx(1:l/2),'r');
title('Original and reflected Signal');
subplot(312);
plot(t,x);
title('Original SIgnal');
subplot(313);
plot(rt,rx);
title('Reflected/flipped SIgnal');

Figure 6.3 Time reversal operation

A MATLAB function to implement the signal flipping is given below.

function[y,n]=sigfold(x,m)
y=fliplr(x) %flips the amplitude levels
n=-1*fliplr(n) %flips the indices on negative sides

4. Sampling
Sampling is the reduction of a continuous signal to a discrete signal. A
common example is the conversion of a sound wave (a continuous signal) to a sequence
of samples (a discrete-time signal).

A sample refers to a value or set of values at a point in time and/or space.

Let x=sin(2πft) be the signal with highest frequency component f. We sample the signal
at different rates as follows.

% Sampling of single frequency component


f=1;
fs=10*f; %Sampling frequency
ts=1/fs; % Sampling Interval
t=-1:ts:1;
x=sin(2*pi*f*t);
stem(t,x);

Figure 6.4 Sampling


Following example shows the sampling of the given signal at different rates. The output graph
shows how sampling rate predicts the signal behavior and loss of information.

% Sampling of single frequency component at different rates


f=10;
fs1=2*f; %Sampling frequency
fs2=10*f;
fs3=0.5*f;
ts1=1/fs1; % Sampling Interval
ts2=1/fs2;
ts3=1/fs3;
t1=-1:ts1:1;
t2=-1:ts2:1;
t3=-1:ts3:1;
x1=sin(2*pi*f*t1);
x2=sin(2*pi*f*t2);
x3=sin(2*pi*f*t3);
subplot(311); stem(t1,x1);
title('sampling at double rate');
subplot(312);stem(t2,x2);
title('sampling at 10 times frequency');
subplot(313);stem(t3,x3);
title('sampling at 0.5 times frequency');

Figure 6.5 Sampling at different rate


Operations on Range
Range operations involve amplitudes of the signal as major focus of
operations. They are classified as

1. Amplitude Scaling
2. Addition of Signals
3. Subtraction of signals
4. Multiplication of Signals
5. Derivative of signals

1. Amplitude Scaling
Amplitude scaling rescales the amplitude of the signal. As a result signal
may be amplified or attenuated. Given a signal x=Asin(2πft) where A is the amplitude of the
signal. We can rescale the amplitude by some constant multiplier alpha or beta. Amplitude scaled
output will be

Code given below scales the amplitude by constant alpha and beta which amplifies and attenuate
the signal respectively.

% Amplitude Scaling
t=-1:0.01:1;
f=1;
a=1; % Amplitude
alpha=2; % Amplitude scale
beta=0.5; % Attenuated scale
x=a*sin(2*pi*f*t);
y=alpha*sin(2*pi*f*t);
z=beta*sin(2*pi*f*t);
subplot(311);
plot(t,x);
title('Original Signal');
subplot(312);
plot(t,y);
title('Amplified Signal');
subplot(313);
plot(t,z);
title('Attenuated SIgnal');
Figure 6.6 Amplitude scaling

A Matlab function to implement the Amplitude scaling is given below.

function y=sigscale(x,alpha)
y=alpha*x;

2. Signal Addition
This is a sample by sample addition given by

It is implemented in MATLAB using + operator however this requires the lengths of the vectors
to be same. But if the signals are of different lengths or if the sample positions are different for
same length sequences, then we cannot directly use the ‘+ ‘operator.We first have to augment the
x1(n) and x2(n) so that we have the same position vector and hence the same length. This
involves MATLAB indexing operations. Logical operators of ‘&’,’<’, :>’ and find functions are
used to make x1(n) and x2(n) of same length. The following function called sigadd performs the
said operation.

% Addition of sgnals
function[y,n]=sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n)); %initialization
y2=y1;
y1(find((n>=min(n1)) & (n<=max(n1))==1))=x1;
y2(find((n>=min(n2)) & (n<=max(n2))==1))=x2;
y=y1+y2;
Example:

Let x1[ 1 2 3 ] = having indices as n1=[-1 : 1]


And x2[5678] = having indices n2=[1 : 4]
To add the sequences, we use the function as below.

>> [y,n]=sigadd(x1,n1,x2,n2)

3. Signal Subtraction
Sequence/Signals subtraction is similar to addition of signals except
the function name and operator sign. Example is given below.

% Subtraction of sgnals
function[y,n]=sigsub(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));
y2=y1;
y1(find((n>=min(n1)) & (n<=max(n1))==1))=x1;
y2(find((n>=min(n2)) & (n<=max(n2))==1))=x2;
y=y1-y2;

Example:
Let x1=[1 2 3] having indices as n1=[ 1 : 1]
Andx 2=[5678] having indices n2=[1 : 4]
To subtract the sequences, we use the function as below.

>> [y,n]=sigsub(x1,n1,x2,n2)

4. Signal Multiplication
Multiplication of signals or sequences involves sample by sample
multiplication. After making the lengths of the vectors same, we multiply the signals using ‘.’
(dot) operator. Here is the example.

% Multiplication of sgnals
function[y,n]=sigmul(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n)); %initialization
y2=y1;
y1(find((n>=min(n1)) & (n<=max(n1))==1))=x1;
y2(find((n>=min(n2)) & (n<=max(n2))==1))=x2;
y=y1.*y2; % Element wise multiplication
Example:
Let x1=[1 2 3] having indices as n1=[ 1 : 1]
Andx 2=[5678] having indices n2=[1 : 4]
To subtract the sequences, we use the function as below.

>> [y,n]=sigmul(x1,n1,x2,n2)

5. Derivative of Signal
Derivative of a given signal is calculated using symbolic Mathematics.
Following is the example to find the derivative of the signal.

%Derivative of signal
syms x y t f
x=sin(t);
y=diff(x);
subplot(211);
ezplot(t,x); %command to plot in syms mode
subplot(212);
ezplot(t,y);

Figure 6.7 Signal derivative


LAB TASKS:
Task 1: Consider the following signals; Write a program in MATLAB for following:
(a) y1 [n]=x1[n]+ x2[n]
(b) y2 [n]=2x1[n]
(c) y3 [n]=x1[n]x2[n]
Task 2: Write a complete code to flip(fold) a signal about origin, also verify the results using
y=flipr(x), where the signal is x[n] ={ -1,5,3,-2,4,2}, 0 ≤ n ≤5

Task 3: 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)=2x(n-5)-3x(n+4)
b) X2(n)=x(3-n)+x(n)x(n-2)
Task 4:Write a MATLAB function named multioperations which returns the following output
on graph simultaneously.
a. Amplitude scaled signal
b. Time Scaled signal
c. Time Shifted Signal
Alpha, beta, gamma are the variables used for defining the scales for shifting, time scaling and
amplitude scaling respectively. Take the input signal Asin(2πfn+ φ). Alpha, beta and gamma
must be taken from user on run time.
Task 5: Write a MATLAB function named multioperators which returns the following output
on graph simultaneously.
a. Sum of the signals
b. Difference of the signals
c. Product of the signals
Take input from user about the operation. Then use switch statement to perform the
respective operations. Use Input()function to take input from the user.

Task 6:Generate and plot the following sequences:


𝒙[𝒏] = 𝟐 ∗ 𝜹[𝒏 +𝟐] − 𝜹[𝒏 − 𝟒] −𝟓≤𝒏≤𝟓

𝒙[𝒏] = 𝒏[𝒖[𝒏] − 𝒖[𝒏 − 𝟏𝟎]] + 𝟏𝟎𝒂−𝟎.𝟑[𝒏−𝟏𝟎][𝒖[𝒏 − 𝟏𝟎] − 𝒖[𝒏 − 𝟐𝟎]]

− 𝟐𝟎 ≤ 𝒏 ≤ 𝟐𝟎

Where a=2.

You might also like