Skip to main content

MATLAB

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}');

Plotting Liner and Circular Convolution with MATLAB

In this post, we implement Linear Convolution and Circular Convolution in MATLAB and plot both results side by side. Convolution is a fundamental operation in signal processing used to find the output of a Linear Time-Invariant (LTI) system when an input signal is applied to it. Linear convolution computes the full convolution of two sequences, producing an output of length M + N - 1. Circular convolution (also called cyclic convolution) wraps around the result and is the basis of fast frequency-domain filtering using the DFT. MATLAB Code % Linear and Circular Convolution in MATLAB % Reads two sequences from the user, computes both convolutions, % displays the numerical results and plots all four signals. clc; % Clear the command window clear all; % Clear all workspace variables % --- Input --- x = input('Enter first sequence : '); % e.g. [1 1 2 2] y = input('Enter second sequence : '); % e.g. [1 2 3 4] % --- Linear Convolution --- % conv() returns a vector of length length(x)+length(y)-1 z = conv(x, y); disp('Linear convolution result:'); disp(z); % --- Circular Convolution --- % cconv(x, y) uses the length of the longer sequence by default c = cconv(x, y); disp('Circular convolution result:'); disp(c); % --- Plot all four signals --- figure; subplot(4, 1, 1); stem(x); title('First Input Sequence (x)'); xlabel('Sample Index'); ylabel('Amplitude'); subplot(4, 1, 2); stem(y); title('Second Input Sequence (y)'); xlabel('Sample Index'); ylabel('Amplitude'); subplot(4, 1, 3); stem(z); title('Linear Convolution (z = x * y)'); xlabel('Sample Index'); ylabel('Amplitude'); subplot(4, 1, 4); stem(c); title('Circular Convolution (c = x ⊛ y)'); xlabel('Sample Index'); ylabel('Amplitude');