In this post, we use MATLAB to plot four fundamental discrete-time signals: the Unit Impulse, Unit Step, Unit Ramp, and Exponential Function. These signals are the building blocks of digital signal processing (DSP) and control systems theory. Understanding how to generate and visualize them in MATLAB is the first step towards analyzing more complex systems.
MATLAB Code
% Plotting Unit Impulse, Unit Step, Unit Ramp and Exponential Function
% All four signals are plotted in a 2x2 subplot grid.
clc; % Clear the command window
clear all; % Clear all workspace variables
% --- 1. Unit Impulse (delta function) ---
% Non-zero only at n=0; value = 1 at origin, 0 elsewhere
t = -2:1:2; % Time axis: -2, -1, 0, 1, 2
impulse = [zeros(1,2), 1, zeros(1,2)]; % 1 only at center (t=0)
subplot(2, 2, 1);
stem(t, impulse);
title('Unit Impulse');
xlabel('n');
ylabel('delta(n)');
% --- 2. Unit Step ---
% Value = 1 for all n >= 0
nStep = input('Enter length for unit step : '); % e.g. 5
tStep = 0:1:nStep - 1; % Time axis: 0, 1, ..., nStep-1
stepSignal = ones(1, nStep); % All ones
subplot(2, 2, 2);
stem(tStep, stepSignal);
title('Unit Step');
xlabel('n');
ylabel('u(n)');
% --- 3. Unit Ramp ---
% Value increases linearly: r(n) = n for n >= 0
nRamp = input('Enter length for unit ramp : '); % e.g. 4
tRamp = 0:1:nRamp - 1; % Time axis: 0, 1, ..., nRamp-1
subplot(2, 2, 3);
stem(tRamp, tRamp); % y = n (ramp equals the index)
title('Unit Ramp');
xlabel('n');
ylabel('r(n) = n');
% --- 4. Exponential Function ---
% y(n) = exp(a*n); decaying when a0
nExp = input('Enter length for exponential : '); % e.g. 5
tExp = 0:1:nExp - 1; % Time axis
a = input('Enter exponent value (a) : '); % e.g. -0.5 or 2
expSignal = exp(a * tExp);
subplot(2, 2, 4);
stem(tExp, expSignal);
title('Exponential Function');
xlabel('n');
ylabel('e^{an}');
How the Code Works
- Unit Impulse — A fixed time vector
t = [-2, -1, 0, 1, 2]is created. The signal vector is built withzerosflanking a single1at the centre (index 3, corresponding tot = 0). This correctly represents the Kronecker deltaδ(n). - Unit Step — The user specifies the length
nStep.ones(1, nStep)creates a row vector of all 1s, representingu(n) = 1forn ≥ 0. - Unit Ramp — The user specifies the length
nRamp. Becauser(n) = n, the time vectortRampis also the signal value itself, sostem(tRamp, tRamp)is used. - Exponential Function — The user provides the length
nExpand the basea. MATLAB computesexp(a .* tExp)element-wise. A negativeagives a decaying exponential; a positiveagives a growing one. - subplot(2, 2, k) — Arranges the four plots in a 2-row, 2-column grid. The
stem()function is used throughout to emphasise the discrete nature of these signals.
Sample Input / Output
Enter length for unit step : 5
Enter length for unit ramp : 4
Enter length for exponential : 5
Enter exponent value (a) : 2

MATLAB opens a figure window showing all four subplots. The unit impulse is fixed; the remaining three signals reflect the user-supplied lengths and exponent.
Output Explanation
- Unit Impulse — A single stem at
n = 0with amplitude 1. All other stems have amplitude 0, clearly showing the spiked nature of the delta function. - Unit Step (length 5) — Five stems at
n = 0, 1, 2, 3, 4, each with amplitude 1. The flat top represents a constant signal that “switches on” atn = 0. - Unit Ramp (length 4) — Stems at
n = 0, 1, 2, 3with amplitudes 0, 1, 2, 3. The linearly increasing height gives the characteristic ramp shape. - Exponential (a = 2, length 5) — Stems at
n = 0..4with values approximately 1, 7.4, 54.6, 403.4, 2981. The rapidly growing height confirms the exponentially increasing nature whena > 0.
See Also
- Plotting sin and cos Function in MATLAB
- Plotting Linear and Circular Convolution with MATLAB
- Implementing DFT in MATLAB
- Implementing DCT and IDCT in MATLAB
Conclusion
This MATLAB program plots the four cornerstone discrete-time signals — unit impulse, unit step, unit ramp, and exponential. Mastery of these signals is essential before moving to topics such as convolution, DFT, and filter design, as virtually every DSP concept is built on top of these primitives.
well explained
The presented way of plotting impulse, step and ramp is completely incorrect. Watch for generating impulse.