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

MATLAB Illustration

The stem() function in MATLAB is specifically designed for visualizing discrete-time signals and sequences. In continuous plotting (using `plot()`), data points are connected by lines — which is mathematically incorrect for discrete/data-sampled signals. stem() solves this by drawing a vertical line (stem) from the baseline (usually zero) to each data point and placing a marker at the top — exactly how discrete sequences are represented in textbooks and research papers.

 Why stem() Exists ?

  • Continuous signals → use `plot()`
  • Discrete-time / sampled / digital signals → use `stem()`
  • Shows exact sample locations and amplitudes without implying continuity between samples
  • Standard in DSP, control systems, and digital filter design

### Basic Syntax & Return Values 

h = stem(Y) % Y vs its index

h = stem(X,Y) % Custom x-axis

h = stem(...,'filled') % Filled markers

h = stem(ax,...) % Target axes

h = stem(...,Name,Value) % Customization

1.Simple Discrete Sequence (Most Common Use)

% Theory: Plot x[n] = [0,1,2,3,2,1,0,-1,-2,-1,0] for 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. Unit Impulse & Unit Step (Core DSP Concepts)

n = -5:10;
delta = (n == 0);           % Kronecker delta δ[n]
step  = (n >= 0);           % Unit step u[n]

subplot(2,1,1); stem(n, delta, 'filled', 'MarkerSize', 8);
title('Unit Impulse δ[n]'); grid on;

subplot(2,1,2); stem(n, step, 'filled', 'Color', 'm');
title('Unit Step u[n]'); grid on;

3. 3D Stem Plot using stem3()

[X,Y] = meshgrid(-5:1:5, -5:1:5);
Z = X.*exp(-X.^2 - Y.^2);      % 3D Gaussian

stem3(X, Y, Z, 'filled', 'MarkerSize', 6);
title('3D Discrete Surface using stem3()');
xlabel('X'); ylabel('Y'); zlabel('Z');
colorbar;

4. Plotting Impulse Response of a Filter

b = [1 0.5]; a = [1 -0.9];      % Simple IIR filter
[h, n] = impz(b, a, 30);       % 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;

5. stem() vs plot() – Quick Comparison Table

Feature stem() plot()
Discrete data Correct representation Misleading (implies continuity)
DSP & signal processing Industry standard Not used
Marker + vertical line Yes No
Baseline control Yes (BaseLine property) No

The stem() function is not just cosmetic — it is the mathematically and academically correct way to visualize discrete-time signals in MATLAB. Always prefer stem() over plot() when working with sequences, digital filters, DFT/FFT results, or any sampled data.

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 R2026a: What's New in the 2026 Release? Major Features & Highlights

MATLAB R2026a (the first major release of 2026) is now available, and it brings a strong mix of AI-powered tools, deeper language integrations, improved productivity features, and performance boosts.

Unlocking Generative AI in MATLAB: Practical Guide to MATLAB Copilot and Beyond

Generative AI in MATLAB is transforming how engineers, scientists, and researchers approach coding, prototyping, and problem-solving. No longer do you need to switch between tools or struggle with syntax MATLAB Copilot, MathWorks' officia