Plotting Unit Impulse, Unit Step, Unit Ramp and Exponential Function in MATLAB
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}');