How to Use stem() Function in MATLAB to Plot Discrete-Time Signals

MATLAB Illustration

The stem() function in MATLAB is built specifically for visualizing discrete-time signals and sequences. While plot() connects data points with continuous lines, doing so on sampled or digital data is mathematically misleading. stem() fixes this by drawing a vertical line from the baseline to each data point topped with a marker—matching how discrete signals appear in DSP textbooks and engineering papers.

When Should You Use stem()?

  • Use plot() for continuous functions and smooth physical signals.
  • Use stem() for discrete-time, digital, or sampled data.
  • It highlights exact sample locations and amplitudes without falsely implying data exists between samples.
  • It is the standard choice in digital signal processing, control systems, and digital filter design.

Basic Syntax Options

MATLAB
h = stem(Y)               
% Plots Y values against their index numbers h = stem(X, Y)            
% Plots Y values at custom X coordinates h = stem(..., 'filled')   % Fills the top markers h = stem(ax, ...)         % Targets a specific axes object h = stem(..., Name, Value)% Customizes colors, line widths, and marker types

Practical Examples

1. Plotting a Basic Discrete Sequence

MATLAB
% Define a sequence x[n] from n = 0 to 10 n = 0:10;
x = [0 1 2 3 2 1 0 -1 -2 -1 0];
stem(n, x, 'filled', 'MarkerFaceColor', 'r', 'LineWidth', 1.5);
title('Basic Discrete Sequence x[n]');
xlabel('Sample index n');
ylabel('Amplitude');
grid on;

2. Generating Unit Impulse & Unit Step Signals

MATLAB
n = -5:10;
delta = (n == 0);
% Kronecker delta δ[n] step  = (n >= 0);
% Unit step u[n]  sub
plot(2,1,1);
stem(n, delta, 'filled', 'MarkerSize', 8);
title('Unit Impulse δ[n]');
grid on;
sub
plot(2,1,2);
stem(n, step, 'filled', 'Color', 'm');
title('Unit Step u[n]');
grid on;

3. 3D Discrete Data Visualization using stem3()

MATLAB
[X, Y] = meshgrid(-5:1:5, -5:1:5);
Z = X .* exp(-X.^2 - Y.^2);
% 3D Gaussian surface points  stem3(X, Y, Z, 'filled', 'MarkerSize', 6);
title('3D Discrete Surface using stem3()');
xlabel('X');
ylabel('Y');
zlabel('Z');
colorbar;

4. Plotting the Impulse Response of an IIR Filter

MATLAB
b = [1 0.5];
a = [1 -0.9];
% IIR filter coefficients [h, n] = impz(b, a, 30);
% Calculate impulse response  stem(n, h, 'filled', 'MarkerFaceColor', 'g');
title('Impulse Response of H(z) = (1 + 0.5z^{-1}) / (1 - 0.9z^{-1})');
xlabel('n');
ylabel('h[n]');
grid on;

Quick Comparison: stem() vs. plot()

Feature stem() plot()
Discrete Data Representation Accurate and clear Misleading (implies continuous values between points)
Signal Processing Standard Yes (DSP & digital filters) No
Visual Marker & Vertical Line Yes No
Baseline Control Yes (via BaseLine property) No

Whenever you are working with sequences, digital filter responses, DFT/FFT outputs, or discrete sampled data, use stem() to keep your plots mathematically accurate.

What Our Students Say

★★★★★

“I got full marks on my MATLAB assignment! The solution was perfect and delivered well before the deadline. Highly recommended!”

Aditi Sharma, Mumbai
★★★★☆

“Quick delivery and excellent communication. The team really understood the problem and provided a great solution. Will use again.”

John M., Australia

Latest Blogs

Explore how MATLAB Solutions has helped clients achieve their academic and research goals through practical, tailored assistance.

MATLAB Autonomous Vehicle Path Planning Project for Students: Step-by-Step with Code

One of the most fascinating and sought-after projects for engineering students is autonomous vehicles. One of the main challenges in the development of self-driving cars is path planning, which is the process of determining a safe, collision-free route from start to o

Battery Management System (BMS) in Electric Vehicles Using MATLAB & Simulink: A Comprehensive Guide

The Battery Management System (BMS) serves as the intelligent brain of an electric vehicle (EV) battery pack. It ensures safety, maximizes performance, extends battery life, and optimizes energy usage in real-world driving conditions. As EVs become mainst