Aim
Write a program to convolve any two discrete-time sequences, plot all the sequences, and verify the result by analytical calculation. Convolve x₁(n) with x₂(n), then x₁(n) with x₃(n) and x₄(n), and compare the results. Repeat for x₂(n).
Objectives
- Carry out linear convolution with the four steps flip → shift → multiply → sum.
- Predict the length (L₁ + L₂ − 1) and the start index (n₁ + n₂) of the output.
- Check results by hand with the multiplication-table (tabular) method.
- See how a difference filter x₃ and an averaging filter x₄ change a signal.
Test sequences
| Name | Values | Start n | Role |
|---|---|---|---|
| x₁ | [1 2 3 1] | 0 | input A |
| x₂ | [1 2 1] | −1 | input B |
| x₃ | [1 −1] | 0 | first difference (high-pass) |
| x₄ | [0.5 0.5] | 0 | 2-point average (low-pass) |
Convolution sum
For an LTI system with impulse response h[n], the output for any input x[n] is the convolution:
- Fold h[k] to get h[−k].
- Shift it by n to get h[n − k].
- Multiply it point by point with x[k].
- Sum all the products. That sum is y[n]. Repeat for every n.
Properties
| Commutative | x * h = h * x |
| Associative | (x * h₁) * h₂ = x * (h₁ * h₂) |
| Distributive | x * (h₁ + h₂) = x * h₁ + x * h₂ |
| Identity | x * δ[n] = x |
| Length | L_y = L_x + L_h − 1, start n_y = n_x + n_h |
| Sum check | Σy = (Σx)(Σh) |
Tabular (multiplication) method
Write x along the top and h down the side, and fill each cell with the product of its row and column. Adding the cells along each anti-diagonal (↙) gives y[n]. The Simulator builds this table live.
- Choose the preset x₁ * x₂. Before you press play, predict the output length and start index.
- Press ▶ Play or drag the output index n slider. At each n, watch h[n−k] slide, the products light up, and y[n] grow.
- Check each value in the multiplication table (the highlighted diagonal is the current n).
- Switch on Compare mode to see x₁*x₃ and x₁*x₄ side by side, then x₂*x₃ and x₂*x₄. Describe what the difference and averaging filters do.
- Record at least 5 output samples in Observations and type your hand-worked values.
- Edge cases: convolve with δ[n] ([1]), with a shifted impulse ([0 0 1]), and with sequences that have negative start indices.
Pre-lab diagnostic
Sequences
Analytical check — multiplication table
Compare mode
The difference filter x₃ picks out changes (edges): its output is near 0 where the input is flat. The average x₄ smooths the input and keeps the total sum (Σx₄ = 1).
Output samples
Comparison of convolutions
Analysis
Post-lab quiz
Self-evaluation
exp6_convolution.m
%% Experiment 6: Discrete-time convolution clc; clear; close all; x1 = [1 2 3 1]; n1 = 0:3; x2 = [1 2 1]; n2 = -1:1; x3 = [1 -1]; n3 = 0:1; x4 = [0.5 0.5]; n4 = 0:1; % user-defined convolution (no built-in) for verification myconv = @(a,b) arrayfun(@(n) sum(a(max(1,n-length(b)+1):min(n,length(a))) .* ... b(n-(max(1,n-length(b)+1):min(n,length(a)))+1)), 1:length(a)+length(b)-1); pairs = {x1,n1,x2,n2,'x1*x2'; x1,n1,x3,n3,'x1*x3'; x1,n1,x4,n4,'x1*x4'; x2,n2,x3,n3,'x2*x3'; x2,n2,x4,n4,'x2*x4'}; for i = 1:size(pairs,1) [a,na,b,nb,name] = pairs{i,:}; y = conv(a,b); ny = (na(1)+nb(1)) : (na(end)+nb(end)); fprintf('%s = [%s] (n = %d..%d) match=%d\n', name, num2str(y), ny(1), ny(end), isequal(round(y,10),round(myconv(a,b),10))); subplot(5,1,i); stem(ny,y,'filled'); title(name); grid on; end