Lab # 07
Lab # 07
OBJECTIVE:
Write a MATLAB code to perform signal processing operations. time scaling time
shifting time inversion.
Hardware/Software required:
1) MATLAB version7.6
2) Computer
3) Printer
Theory:
Various signal processing operation that can be performed on a signal are:
x(t)=
• Time inversion: if x (t) is the original signal then x (-t) is the time inverted signal.
• Time scaling: if x (t) is a given signal then x(t) is time scaled by ‘a’ is given below;
• Time shifting:- if x(t) is a given signal then x(t) is time shifted by t0 can be obtained
Delayed x(t-t0)
• Advanced x(t+t0)
%Time Scaling
k = input('Enter the value by which you want to scale the signal (compress or elongate) ->’);
Xsc = sin(k*w*t);
%Time Shifting
t1 = input('Enter the value of time by which you want to advance (enter positive value) OR delay
(enter negative value) the signal ->');
Xsh = sin(w*(t-t1));
%Time Inversion
Xin = sin(-w*t);
%Plotting the transformed and original signals
Hold on;
plot(t,X,'-k','Linewidth',2);
gridon;
plot(t,Xsc,':r','Linewidth',2);
gridon;
plot(t,Xsh,'-g','Linewidth',2);
gridon;
plot(t,Xin,'-.b','Linewidth',2);
gridon;
xlabel('Time');
ylabel('Amplitude of the signal');
Another Code for Understanding:
% Signals, Spectra, & Signal Processing
% NFC IET Multan. Designed By Mughees Riaz
% Plot Continuous-Time and with time shifting, reversal and scaling
% Continuous-time Piecewise Functions:
% 2 sin (pi/4)t 0 < or = t < or = 4
% -t -2 < or = t < or = 0
% 2 -4 < or = t < or = -2
%
% piecewise values set in different variables
x1 = 0:.2:4; % Limits of the first piecewise
function 2 sin(pi/4)t
x2 = -2:.5:0; % Limits of the second piecewise
function -t
x3 = -4:-2; % Limits of the third piecewise
function 2
y1 = (2*(sin((pi/4)*x1))); % First piecewise function which is a
curve
y2 = -x2; % Second piecewise function which is
a slope
y3 = [2 2 2]; % Third piecewise function which is a
horizontal
% Starts plotting the piecewise function to see the signal output
% creates a figure
figure (1);subplot(2,2,1); % creates a subplot with 2
rows and 2 columns and this is at the first row and column
plot(x1,y1,x2,y2,x3,y3,'LineWidth', 3); % plots the signal
xlabel('time'); % sets the X-axis label for the graph
ylabel('f(t)'); % sets the Y-axis label for the graph
title('Original Continuous-Time Signal'); % sets the title for the graph
grid on; % turns the grid lines on
hold on; % holds the current graph for
additional data
axis1 = [0 0]; % values for the Y-axis (vertical
line)
axis2 = [-1 2.5]; % values for the Y-axis (vertical
line)
axis3 = [-5 5]; % values for the X-axis (Horizontal
line)
axis4 = [0 0]; % values for the X-axis (Horizontal
line)
plot(axis1,axis2,axis3,axis4,'LineWidth', 1); % plots the cross in cartesian plane
hold off; % takes the hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% Time-shift signal code %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
dlg = inputdlg({'Time Shift ( + or - numbers / fraction )','Scale (positive
number/fraction)','Reverse (negative or positive number)'},... % creates a dialog box
that asks for input
'Continuous-time', [1 50; 1 50; 1 50]); % continuation of the code
for dialog box
%% gg= str2double(dlg(1,1)); % converts the string input from
the dialog box into a number
[aa,bb] = numden (sym(dlg(1,1)));
t = aa/bb;
ts1 = [0 0]; % values for the Y-axis (vertical
line)
ts2 = [-1 2.5]; % values for the Y-axis (vertical
line)
ts3 = [(-5-(t)) (5-(t))]; % values for the X-axis (Horizontal
line)
ts4 = [0 0]; % values for the X-axis (Horizontal
line)
if t < 0 ; % if statement condition if "t" is
less than zero (negative)
subplot(2,2,2); % creates a subplot with 2 rows and
2 columns and this is at the second column and first row
hold on;
plot(ts1,ts2,ts3,ts4,'LineWidth',1); % plots the cross in cartesian plane
hold off; % takes the hold off
else % else statement condition if "t" is
greater than zero (positive)
subplot(2,2,2); % creates a subplot with 2 rows and
2 columns and this is at the second column and first row
plot ((x1-t),y1,(x2-t),y2,(x3-t),y3,'LineWidth', 4); % plots the data that has
been time-shifted according to the desired value
grid on; % turns the grid lines on
xlabel('time'); % sets the X-axis label for the
graph
ylabel('f(t)'); % sets the Y-axis label for the
graph
title('Time-Shifted Continuous-Time Signal'); % sets the title for the graph
hold on;
plot(ts1,ts2,ts3,ts4,'LineWidth',1); % plots the cross in cartesian plane
hold off; % takes the hold off
end % end of if function
%pause(20); % pause the program for 20 seconds
%clear; % clears all data
%clc; % clears the command window
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% Scaling signal code %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
[n,d] = numden(sym(dlg(2,1))); % separates the numerator and
denominator
t2 = n/d; % equates t2 with the input number
tc1 = [0 0]; % values for the Y-axis (vertical
line)
tc2 = [-1 2.5]; % values for the Y-axis (vertical
line)
tc3 = [-5 5]; % values for the X-axis (Horizontal
line)
tc4 = [0 0]; % values for the X-axis (Horizontal
line)
if t2 == n/d; % if condition statement
subplot(2,2,3); % subplot in row 2, column 1
plot(((x1-(t))*(d/n)),y1,((x2-(t))*(d/n)),y2,((x3-(t))*(d/n)),y3,'LineWidth',4); %
plots the signal
else
subplot(2,2,3); % subplot in row 2, column 1
plot (((x1-t)*(n/d)),y1,((x2-t)*(n/d)),y2,((x3-t)*(n/d)),y3,'LineWidth', 4); %
plots the signal
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% Reverse signal code %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%
t3 = str2double(dlg(3,1));
if t3 < 0 && t2 == n/d;
subplot(2,2,4);
% plot(-(x1*(d/n)),y1,-(x2*(d/n)),y2,-(x3*(d/n)),y3,'LineWidth', 3);
plot(-((x1-(t))*(d/n)),y1,-((x2-(t))*(d/n)),y2,-((x3-(t))*(d/n)),y3,'LineWidth',4);
% plots the signal
grid on;
grid minor;
xlabel('time');
ylabel('f(t)');
title('Reversed Continuous-Time Signal');
hold on;
plot (-(tc1),tc2,-(tc3*(d/n)),tc4,'LineWidth',1);
hold off;
subplot(2,2,4);
% plot((x1*(d/n)),y1,(x2*(d/n)),y2,(x3*(d/n)),y3,'LineWidth', 3);
grid on;
grid minor;
xlabel('time');
ylabel('f(t)');
title('Reversed Continuous-Time Signal');
h = msgbox('no reversing done!');
hold on;
plot (-(tc1),tc2,-(tc3*(d/n)),tc4,'LineWidth',1);
hold off;
h = msgbox('no reversing done!');
subplot(2,2,4);
% plot(-(x1*(n/d)),y1,-(x2*(n/d)),y2,-(x3*(n/d)),y3,'LineWidth', 3);
grid on;
grid minor;
xlabel('time');
ylabel('f(t)');
title('Scaled Continuous-Time Signal');
hold on;
plot (tc1,tr2,(tc3*(n/d)),tc4,'LineWidth',1);
hold off;
else
subplot(2,2,4);
% plot((x1*(n/d)),y1,(x2*(n/d)),y2,(x3*(n/d)),y3,'LineWidth', 3);
plot (-((x1-t)*(n/d)),y1,-((x2-t)*(n/d)),y2,-((x3-t)*(n/d)),y3,'LineWidth', 4); %
plots the signal
grid on;
grid minor;
xlabel('time');
ylabel('f(t)');
title('Scaled Continuous-Time Signal');
h = msgbox('no reversing done!');
hold on;
plot (tc1,tc2,(tc3*(n/d)),tc4,'LineWidth',1);
hold off;
end