|
1D Convolution Visualization Tutorial
This interactive tutorial demonstrates how 1D convolution works through an animated visualization. Convolution is a fundamental operation in signal processing, image processing, and deep learning (especially convolutional neural networks). The tutorial shows how a kernel (filter) slides across an input signal, computing the dot product at each position to produce an output signal.
The visualization consists of three main panels: (1) Input Signal (top left) - a static stem plot showing the selected input signal in bright blue, (2) Kernel (top right) - a static stem plot showing the selected kernel in bright red, and (3) Convolution Animation (bottom) - the main view where the kernel slides across the input signal, and the output is computed point-by-point in yellow. The animation includes a green rectangle that highlights the current overlap window where the convolution is being calculated. The visualization uses a dark theme (black background) with bright colors for optimal visibility, and the kernel position is perfectly synchronized with the output point at each step.
You can select from different input signals (Square Pulse, Double Square Pulse, Sine Wave, Noised Sine Wave, Triangle, Random Noise, Impulse) and different kernels (Box/Average, Gaussian, Edge Detection, Smoothing, Low Pass Filter, High Pass Filter, Band Pass Filter, Asymmetric, Ringing, Differentiator, Scaled Delay, Reflection). The animation can be controlled with step forward/backward buttons, a play/pause button, and a reset button. The output accumulates as the kernel slides, making it easy to see how each position contributes to the final result. The visualization uses a dark theme (black background) with bright colors for optimal visibility, and the kernel position is perfectly synchronized with the output point at each step.
NOTE : The tutorial uses stem plots (discrete-time signals) to clearly show individual sample values. The convolution operation is computed using the standard discrete convolution formula: y[n] = Σk x[k] × h[n-k], where x[n] is the input signal, h[n] is the kernel, and y[n] is the output. For visualization clarity, the kernel is shown sliding without flipping (correlation-style), but the mathematical computation uses proper convolution (with kernel flip). The green baseline (y=0) is visible in all plots to help understand positive and negative values.
Mathematical Model
Convolution is a mathematical operation that combines two signals to produce a third signal:
Discrete Convolution Formula:
y[n] = Σk x[k] × h[n-k]
where:
- x[n]: Input signal (discrete-time sequence)
- h[n]: Kernel or filter (discrete-time sequence)
- y[n]: Output signal (convolution result)
- k: Summation index over all valid positions
Operation: At each output position n, the kernel is flipped and shifted, then multiplied element-wise with the overlapping portion of the input signal. The sum of these products gives the output value at position n. The kernel slides from left to right, computing one output value at a time.
Output Length: If the input signal has length L and the kernel has length M, the output has length L + M - 1. This accounts for the kernel entering and exiting the signal boundaries. The visualization shows the full convolution result, including the transient regions at the beginning and end.
Usage Example
Follow these steps to explore the 1D Convolution tutorial:
-
Initial State: When you first load the simulation, you'll see three panels: Input Signal (top left, blue), Kernel (top right, red), and Convolution Animation (bottom). The default input signal is a Square Pulse, and the default kernel is Box/Average. The animation is paused, showing the initial state with no output computed yet.
-
Observe Input Signal: The top left panel shows the input signal as a stem plot (discrete-time signal). Each vertical line represents a sample value, with circles at the top showing the exact value. The green horizontal line at y=0 is the baseline. The default Square Pulse signal has values of 1 from positions 5 to 11, and 0 elsewhere.
-
Observe Kernel: The top right panel shows the kernel (filter) as a stem plot. The default Box/Average kernel has 5 equal values of 0.2, which creates a moving average effect. The kernel is what will slide across the input signal during convolution.
-
Run the Animation: Click the "▶ Run" button to start the animation. The kernel (red) will slide from left to right across the input signal (blue) in the bottom panel. A green rectangle highlights the current overlap window where the convolution is being calculated. The output (black) accumulates point-by-point as the kernel moves.
-
Step Through Manually: Use the "Step Fwd ⏭" and "⏮ Step Back" buttons to move through the convolution one position at a time. This allows you to carefully observe how each output value is computed. The current position is indicated by a pink label showing "n=X" below the output plot.
-
Try Different Input Signals: Use the "Input Signal" dropdown to select different signals:
- Square Pulse (default): A rectangular pulse from positions 5-11 (20 samples)
- Double Square Pulse: Two square pulses at amplitude 0.8, separated by zeros, with each value repeated 20 times (100 samples) - useful for demonstrating filter response to multiple pulses
- Sine Wave: A sinusoidal signal that oscillates smoothly (20 samples)
- Noised Sine Wave: A longer sine wave (101 samples) with uniform random noise, based on MATLAB: 0.5*sin(6πt) + 0.5*(rand-0.5), useful for demonstrating filter effects
- Triangle: A triangular waveform that rises and falls (20 samples)
- Random Noise: Random values between -1 and 1 (20 samples)
- Impulse: A single spike at position 10 (Dirac delta function, 20 samples)
Each signal type demonstrates different aspects of convolution. The Impulse signal is particularly useful for understanding how a kernel responds to a single point.
-
Try Different Kernels: Use the "Kernel" dropdown to select different filters:
- Box/Average (default): [0.2, 0.2, 0.2, 0.2, 0.2] - uniform smoothing (5 points)
- Gaussian: [0.1, 0.2, 0.4, 0.2, 0.1] - bell-shaped smoothing (5 points)
- Edge Detection: [-1, -1, 0, 1, 1] - highlights edges and changes (5 points)
- Smoothing: [0.1, 0.15, 0.5, 0.15, 0.1] - emphasizes center, smooths edges (5 points)
- Low Pass Filter: 9-point Gaussian (bell-shaped) kernel - passes low frequencies, attenuates high frequencies (ideal for noise reduction)
- High Pass Filter: 11-point filter with specific coefficients - passes high frequencies, attenuates low frequencies (ideal for edge detection and change detection)
- Band Pass Filter: 11-point filter - passes mid-range frequencies, attenuates both low and high frequencies
- Asymmetric (Ramp): 7-point ramp kernel [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7] - clearly shows kernel flipping during convolution
- Ringing: 11-point exponentially decaying kernel with alternating signs - creates ringing/oscillating effects
- Differentiator: [0, 1, -1, 0, ...] (11 points) - computes first derivative (difference between adjacent samples)
- Scaled Delay: [0, 0, ..., 1.5, 0] (11 points) - delays signal by 9 samples and scales by 1.5
- Reflection: 28-point kernel with main pulse at position 2 and echo at position 17 - simulates echo/reflection effects
Each kernel produces different effects. The Edge Detection kernel will produce negative values in the output when it detects transitions.
-
Observe the Output: The bottom panel shows the convolution result accumulating in yellow. Notice how the output length is longer than the input (L + M - 1, where L is input length and M is kernel length). The kernel position and output point are perfectly aligned vertically at each step n, thanks to the synchronized coordinate system. The transient regions at the beginning and end occur as the kernel enters and exits the signal boundaries.
-
Understand the Overlap Window: The green rectangle in the bottom panel shows the current overlap window. This is where the kernel overlaps with the input signal. At each position, the convolution multiplies corresponding values and sums them. Watch how the window moves and how the output value changes based on what's inside the window.
-
Observe Kernel Flipping: Select the "Asymmetric (Ramp)" kernel to clearly see how convolution flips the kernel. In the top right panel, you'll see the kernel as [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7] (ramp from small to large). In the bottom animation panel, watch as the kernel slides - it appears flipped as [0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1] (ramp from large to small). This demonstrates that convolution mathematically flips the kernel before sliding it across the signal.
-
Reset and Experiment: Click "Reset" to clear the output and start over. Try different combinations of signals and kernels to see how they interact. For example, try Edge Detection kernel on a Square Pulse to see how it detects the rising and falling edges, or try the Asymmetric kernel to observe the flipping behavior.
Tip: The key to understanding convolution is recognizing that it's a sliding dot product. At each position, the kernel is flipped, shifted, and multiplied element-wise with the overlapping portion of the input signal. The sum of these products gives the output value. The visualization shows the kernel sliding (without flip for clarity), but the mathematical computation uses proper convolution (with flip). Start with simple signals (Square Pulse, Impulse) and simple kernels (Box/Average) to build intuition, then try more complex combinations. Use step-by-step mode to carefully observe how each output value is computed. The green baseline (y=0) helps you understand positive and negative values in the output.
Parameters
Followings are short descriptions on each parameter
-
Input Signal (x[n]): The discrete-time input signal that the kernel will slide across. The signal is represented as a sequence of sample values. Available signal types include Square Pulse (rectangular pulse, 20 samples), Double Square Pulse (two pulses at 0.8 amplitude, 100 samples), Sine Wave (sinusoidal oscillation, 20 samples), Noised Sine Wave (sine wave with uniform random noise, 101 samples - based on MATLAB: 0.5*sin(6πt) + 0.5*(rand-0.5)), Triangle (triangular waveform, 20 samples), Random Noise (random values, 20 samples), and Impulse (single spike, 20 samples). The signal is displayed as a stem plot (discrete-time visualization) in bright blue, with each sample shown as a vertical line with a circle at the top. The visualization uses a dark theme (black background) for better contrast.
-
Kernel (h[n]): The filter that slides across the input signal. The kernel is also a discrete-time sequence, typically shorter than the input signal. Available kernel types include Box/Average (uniform smoothing, 5 points), Gaussian (bell-shaped smoothing, 5 points), Edge Detection (highlights transitions, 5 points), Smoothing (emphasizes center, 5 points), Low Pass Filter (9-point Gaussian bell-shaped kernel - passes low frequencies, attenuates high frequencies), High Pass Filter (11-point filter with specific coefficients - passes high frequencies, attenuates low frequencies), Band Pass Filter (11 points - passes mid-range frequencies), Asymmetric (Ramp) (7 points - ramp from 0.1 to 0.7, designed to clearly show kernel flipping), Ringing (11-point exponentially decaying with alternating signs), Differentiator (11 points - [0, 1, -1, 0, ...] computes first derivative), Scaled Delay (11 points - delays by 9 samples, scales by 1.5), and Reflection (28 points - creates echo/reflection pattern). The kernel is displayed as a stem plot in bright red in the top right panel (unflipped, as defined). In the bottom animation panel, the kernel is shown flipped (as required by convolution mathematics) - compare the two to observe the flip, especially with the Asymmetric kernel.
-
Output Signal (y[n]): The result of the convolution operation. The output has length L + M - 1, where L is the input length (20 for most signals, 100 for Double Square Pulse, 101 for Noised Sine Wave) and M is the kernel length (5 for basic kernels, 7-28 for various filters), resulting in output lengths ranging from 24 to 128. The output accumulates point-by-point as the kernel slides across the input. The output is displayed as a stem plot in yellow, with each computed value shown as it's calculated. The kernel position and output point are perfectly synchronized vertically at each step n, using a common coordinate system based on the output length.
-
Convolution Formula: The discrete convolution is computed as y[n] = Σk x[k] × h[n-k], where the summation is over all valid positions where the kernel overlaps with the input. At each output position n, the kernel is flipped (h[n-k] instead of h[k]), shifted by n positions, multiplied element-wise with the overlapping portion of the input, and summed. This operation is repeated for all output positions.
-
Overlap Window: The green rectangle in the animation panel highlights the current overlap region where the kernel overlaps with the input signal. This window moves from left to right as the convolution progresses. The size of the window equals the kernel length (5 samples). The output value at the current position is computed from the values inside this window.
-
Boundary Effects: At the beginning and end of the convolution, the kernel only partially overlaps with the input signal. These transient regions show how the convolution handles boundaries. The output length (L + M - 1) accounts for these partial overlaps. In the visualization, you can see the kernel entering from the left and exiting to the right.
Controls and Visualizations
Followings are short descriptions on each control
-
Input Signal Dropdown: Selects the type of input signal to use. Available options include Square Pulse, Double Square Pulse, Sine Wave, Noised Sine Wave, Triangle, Random Noise, and Impulse. Changing the signal resets the animation and recalculates the convolution result. Each signal type demonstrates different aspects of convolution behavior. The Double Square Pulse is particularly useful for observing filter response to multiple pulses, and the Noised Sine Wave (101 samples) is ideal for demonstrating frequency filtering effects.
-
Kernel Dropdown: Selects the type of kernel (filter) to use. Available options include Box/Average, Gaussian, Edge Detection, Smoothing, Low Pass Filter (Gaussian), High Pass Filter, Band Pass Filter, Asymmetric (Ramp), Ringing, Differentiator, Scaled Delay, and Reflection. Changing the kernel resets the animation and recalculates the convolution result. Each kernel produces different effects on the input signal. The Differentiator computes the first derivative, Scaled Delay demonstrates time-shifting, and Reflection shows echo effects.
-
Step Back Button (⏮ Step Back): Moves the animation backward by one position. This allows you to carefully observe how each output value is computed. The button pauses the animation if it's currently playing. Useful for detailed analysis of the convolution process.
-
Play/Pause Button (▶ Run / ❚❚ Pause): Starts or pauses the continuous animation. When playing, the kernel slides across the input signal automatically, and the output accumulates. The button text changes to "❚❚ Pause" when playing. Clicking again pauses the animation. The animation stops automatically when it reaches the end.
-
Step Forward Button (Step Fwd ⏭): Moves the animation forward by one position. This allows you to step through the convolution manually, observing each computation step. The button pauses the animation if it's currently playing. Useful for understanding the convolution process in detail.
-
Reset Button: Clears the output and resets the animation to the initial state (position 0). The output is cleared, and the animation is paused. This allows you to start over with the current signal and kernel settings.
-
Input Signal Panel (Top Left): Displays the input signal as a stem plot in blue. Each sample is shown as a vertical line with a circle at the top. The green horizontal line at y=0 is the baseline. The signal is static and doesn't change during the animation. This panel helps you see the input signal clearly before convolution.
-
Kernel Panel (Top Right): Displays the kernel as a stem plot in red. Each kernel coefficient is shown as a vertical line with a circle at the top. The kernel is static and doesn't change during the animation. This panel helps you see the kernel values that will be applied during convolution.
-
Convolution Animation Panel (Bottom): The main visualization area showing the convolution process. It contains three layers: (1) Input Signal (bright blue, static, top half) - the same signal as the top left panel, (2) Kernel (bright red, sliding, top half) - the kernel sliding across the input, and (3) Output (yellow, accumulating, bottom half) - the convolution result being computed point-by-point. A green rectangle highlights the current overlap window. Grid lines help align values. The current position is indicated by a pink label showing "n=X". The visualization uses a dark theme (black background) with bright colors for visibility. The kernel position and output point are perfectly aligned vertically at each step n, using a synchronized coordinate system based on the output length.
-
Info Panel: The explanation panel at the bottom provides mathematical background, including the convolution formula, definitions of terms, and a description of the animation. This helps users understand the mathematical concepts behind the visualization.
Key Concepts
-
Discrete-Time Signals: Both the input signal and kernel are discrete-time sequences, meaning they are defined only at integer time indices (n = 0, 1, 2, ...). The stem plot visualization clearly shows this discrete nature, with each sample represented as a vertical line with a circle.
-
Sliding Window Operation: Convolution is fundamentally a sliding window operation. The kernel acts as a window that slides across the input signal. At each position, the window captures a portion of the input, and the convolution computes a weighted sum of the values inside the window, where the weights are the kernel coefficients.
-
Kernel Flipping: Mathematically, convolution requires flipping the kernel before sliding (h[n-k] instead of h[k]). The tutorial correctly implements this: in the top right panel, the kernel is shown unflipped (as defined: h[0], h[1], h[2], ...). In the bottom animation panel, the kernel is shown flipped (as used in convolution: h[M-1], h[M-2], ..., h[0]). To clearly observe this flipping, select the "Asymmetric (Ramp)" kernel - you'll see it as [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7] in the top panel (small to large, left to right) and as [0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1] when sliding in the animation (large to small, left to right). The actual computation uses proper convolution with kernel flip, which is why the output values differ from what you might expect if the kernel weren't flipped.
-
Output Length: The output length is L + M - 1, where L is the input length and M is the kernel length. This accounts for the kernel entering and exiting the signal boundaries. The transient regions at the beginning and end show partial overlaps, while the middle region shows full overlaps.
-
Point-by-Point Computation: Each output value is computed independently by sliding the kernel to that position, multiplying overlapping values, and summing. The visualization shows this accumulation process, making it clear that convolution is not a global operation but a local, sliding operation.
-
Different Kernel Effects: Different kernels produce different effects:
- Smoothing Kernels (Box, Gaussian): Reduce high-frequency variations, creating smoother outputs
- Edge Detection Kernels: Highlight transitions and changes, producing peaks at edges
- Impulse Response: When the input is an impulse, the output shows the kernel itself (scaled), demonstrating that the kernel is the system's impulse response
-
Applications: Convolution is fundamental to many fields:
- Signal Processing: Filtering, smoothing, edge detection
- Image Processing: Blurring, sharpening, feature detection
- Deep Learning: Convolutional Neural Networks (CNNs) use convolution to detect features in images
- Communication Systems: Channel equalization, matched filtering
-
Real-Time Visualization: The animation updates in real-time as the kernel slides. The output accumulates point-by-point, making it easy to see how each position contributes to the final result. The step controls allow detailed analysis of individual computation steps.
|
|