Lab No 6
Lab No 6
Objectives:
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');
y(n).
y(m+k)={x(m)}
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.
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');
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).
Let x=sin(2πft) be the signal with highest frequency component f. We sample the signal
at different rates as follows.
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
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:
>> [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);
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.
− 𝟐𝟎 ≤ 𝒏 ≤ 𝟐𝟎
Where a=2.