Source Code for Signals
Shifted Delta Function
clc ;
clear all ; % # ok < CLALL >
close all ;
% Define parameters
n = 30:40;
n_0 = 34; % ID - 2002034
x = zeros (1 , length ( n ) ) ;
% Generate the Shifted delta function
for i = 1: length ( n )
if n ( i ) == n_0
x ( i ) = 1;
else
x ( i ) = 0;
end
end
% Plot the signal
figure ;
% Customize the stem plot
stem (n , x , ’ filled ’ , ’ LineWidth ’ , 1.5 , ’ Color ’ , [0.16 , 0.44 , 1.00]) ;
% Set axis limits
ylim ([0 , 2]) ;
xlim ([30 , 40]) ;
% Add labels and title
xlabel ( ’ Sample ␣ No . ␣ n ’ , ’ FontSize ’ , 12 , ’ FontWeight ’ , ’ bold ’) ;
ylabel ( ’x ( n ) ’ , ’ FontSize ’ , 12 , ’ FontWeight ’ , ’ bold ’) ;
title ( ’ Shifted ␣ Delta ␣ Function ’ , ’ FontSize ’ , 14 , ’ FontWeight ’ , ’ bold ’) ;
% Customize appearance
set ( gca , ’ Box ’ , ’ on ’ , ’ LineWidth ’ , 1.2 , ’ FontSize ’ , 10 , ’ FontWeight ’ , ’ bold ’) ;
set ( gcf , ’ Color ’ , ’w ’) ;
% Save the plot as an image
saveas ( gcf , ’ S h i f t e d _ d e l t a _ f u n c t i o n _ p l o t . png ’) ;
1
Shifted Unit Step Function
clc ;
clear all ; % # ok < CLALL >
close all ;
% Define parameters
n = 30:40;
n_0 = 34; % ID - 2002034
x = zeros (1 , length ( n ) ) ;
% Generate the Shifted Unit Step function
for i = 1: length ( n )
if n ( i ) >= n_0
x ( i ) = 1;
else
x ( i ) = 0;
end
end
% Plot the signal
figure ;
% Customize the stem plot
stem (n , x , ’ filled ’ , ’ LineWidth ’ , 1.5 , ’ Color ’ , [0.16 , 0.44 , 1.00]) ;
% Set axis limits
ylim ([0 , 2]) ;
xlim ([30 , 40]) ;
% Add labels and title
xlabel ( ’ Sample ␣ No . ␣ n ’ , ’ FontSize ’ , 12 , ’ FontWeight ’ , ’ bold ’) ;
ylabel ( ’x ( n ) ’ , ’ FontSize ’ , 12 , ’ FontWeight ’ , ’ bold ’) ;
title ( ’ Shifted ␣ Unit ␣ Step ␣ Function ’ , ’ FontSize ’ , 14 , ’ FontWeight ’ , ’ bold ’) ;
% Customize appearance
set ( gca , ’ Box ’ , ’ on ’ , ’ LineWidth ’ , 1.2 , ’ FontSize ’ , 10 , ’ FontWeight ’ , ’ bold ’) ;
set ( gcf , ’ Color ’ , ’w ’) ;
% Save the plot as an image
saveas ( gcf , ’ S h i f t e d _ U n i t S t e p _ f u n c t i o n _ p l o t . png ’) ;
2
Shifted Ramp Function
clc ;
clear all ; % # ok < CLALL >
close all ;
% Define parameters
n = 30:40;
n_0 = 34; % ID - 2002034
x = zeros (1 , length ( n ) ) ;
% Generate the shifted ramp function
for i = 1: length ( n )
if n ( i ) >= n_0
x ( i ) = n ( i ) - n_0 ;
else
x ( i ) = 0;
end
end
% Plot the signal
figure ;
% Customize the stem plot
stem (n , x , ’ filled ’ , ’ LineWidth ’ , 1.5 , ’ Color ’ , [0.16 , 0.44 , 1.00]) ;
% Set axis limits
ylim ([0 , 5]) ;
xlim ([30 , 40]) ;
% Set y - axis ticks to integers only
yticks (0:5) ;
% Add labels and title
xlabel ( ’ Sample ␣ No . ␣ n ’ , ’ FontSize ’ , 12 , ’ FontWeight ’ , ’ bold ’) ;
ylabel ( ’x ( n ) ’ , ’ FontSize ’ , 12 , ’ FontWeight ’ , ’ bold ’) ;
title ( ’ Shifted ␣ Ramp ␣ Function ’ , ’ FontSize ’ , 14 , ’ FontWeight ’ , ’ bold ’) ;
% Customize appearance
set ( gca , ’ Box ’ , ’ on ’ , ’ LineWidth ’ , 1.2 , ’ FontSize ’ , 10 , ’ FontWeight ’ , ’ bold ’) ;
set ( gcf , ’ Color ’ , ’w ’) ;
% Save the plot as an image
saveas ( gcf , ’ S h i f t e d _ R a m p _ F u n c t i o n _ P l o t . png ’) ;
3
Random Signal
clc ; clear ; close all ;
n = -20:20;
% Delta Functions
delta = (( n ==0) ) ;
shifted_delta = (( n ==5) ) ;
% Unit Step Functions
step = (( n >=0) ) ;
shifted_step = (( n >=6) ) ;
% Ramp Functions
ramp = n .* step ;
scaled_ramp = 0.7* ramp ;
% Sinusoidal and Exponential Signal
sin_signal = sin (0.2 * pi * n ) ;
exp_signal = exp ( -0.1 * n ) .* step ;
% Addition and Subtraction
added_signal = sin_signal + exp_signal ;
subtract_signal = sin_signal - exp_signal ;
% Differentiation and Integration
diff_add = [0 , diff ( added_signal ) ];
intgr_subtr = cumsum ( subtract_signal ) ;
% Random Signal
random = 0.1* shifted_step + 0.2* scaled_ramp + 0.2* added_signal + 0.5* subtract_signal + 0.9*
diff_add + intgr_subtr ;
% Publication - grade settings
set (0 , ’ D ef a u lt A xesFontSize ’ , 12 , ’ D efau ltAxe sFon tWeig ht ’ , ’ bold ’ , ...
’ D e f a u l t L i n e LineWidth ’ , 1.5 , ’ DefaultStemLineWidth ’ , 1.5) ;
% Plot signals in subplots
figure ( ’ Color ’ , ’w ’ , ’ Position ’ , [100 , 100 , 1200 , 800]) ;
subplot (3 , 2 , 1) ;
stem (n , shifted_step , ’ filled ’ , ’ MarkerFaceColor ’ , ’b ’ , ’ MarkerSize ’ , 6) ;
xlabel ( ’n ’) ; ylabel ( ’ Amplitude ’) ; title ( ’ Shifted ␣ Step ␣ Function ’) ;
grid on ;
subplot (3 , 2 , 2) ;
stem (n , scaled_ramp , ’ filled ’ , ’ MarkerFaceColor ’ , ’r ’ , ’ MarkerSize ’ , 6) ;
xlabel ( ’n ’) ; ylabel ( ’ Amplitude ’) ; title ( ’ Scaled ␣ Ramp ␣ Function ’) ;
grid on ;
subplot (3 , 2 , 3) ;
stem (n , added_signal , ’ filled ’ , ’ MarkerFaceColor ’ , ’g ’ , ’ MarkerSize ’ , 6) ;
xlabel ( ’n ’) ; ylabel ( ’ Amplitude ’) ; title ( ’ Added ␣ Signal ’) ;
grid on ;
subplot (3 , 2 , 4) ;
stem (n , subtract_signal , ’ filled ’ , ’ MarkerFaceColor ’ , ’m ’ , ’ MarkerSize ’ , 6) ;
xlabel ( ’n ’) ; ylabel ( ’ Amplitude ’) ; title ( ’ Subtracted ␣ Signal ’) ;
grid on ;
subplot (3 , 2 , 5) ;
stem (n , diff_add , ’ filled ’ , ’ MarkerFaceColor ’ , ’c ’ , ’ MarkerSize ’ , 6) ;
xlabel ( ’n ’) ; ylabel ( ’ Amplitude ’) ; title ( ’ Differentiated ␣ Signal ’) ;
grid on ;
subplot (3 , 2 , 6) ;
stem (n , intgr_subtr , ’ filled ’ , ’ MarkerFaceColor ’ , ’y ’ , ’ MarkerSize ’ , 6) ;
xlabel ( ’n ’) ; ylabel ( ’ Amplitude ’) ; title ( ’ Integrated ␣ Signal ’) ;
4
grid on ;
% Save subplots figure
exportgraphics ( gcf , ’ D i f f e r e n t _ S i g n a l s _ I n _ S u b p l o t s . png ’ , ’ Resolution ’ , 300) ;
% Plot random signal in a separate figure
figure ( ’ Color ’ , ’w ’ , ’ Position ’ , [100 , 100 , 1200 , 400]) ;
stem (n , random , ’ filled ’ , ’ MarkerFaceColor ’ , ’k ’ , ’ MarkerSize ’ , 6) ;
xlabel ( ’n ’) ; ylabel ( ’ Amplitude ’) ; title ( ’ Random ␣ Signal ’) ;
grid on ;
% Save random signal figure
exportgraphics ( gcf , ’ Random_Signal . png ’ , ’ Resolution ’ , 300) ;