Convolution with Julia Language
Signals and Signal Processing Module
Convolution
In DSP, Convolution is an important operation and has many other uses.
Let's understand how a Linear system changes an input signal into an output signal. First, the input signal can be decomposed into a set of impulses, each of which can be viewed as a scaled and shifted delta function. Second, the output resulting from each impulse is a scaled and shifted version of the impulse response. Third, the overall output signal can be found by adding these scaled and shifted impulse responses. In other words, if we know a system's impulse response, then we can calculate what the output will be for any possible input signal. This means we know everything about the system. There is nothing more that can be learned about a linear system's characteristics.
Convolution is a formal mathematical operation, just as multiplication, addition, and integration. Addition takes two numbers and produces a third number, while convolution takes two signals and produces a third signal. Convolution is used in the mathematics of many fields, such as probability and statistics. In linear systems, convolution is used to describe the relationship between three signals of interest: the input signal, the impulse response, and the output signal.
Just as addition is represented by the plus, +, and multiplication by the cross, ×, convolution is represented by the star, *. It is unfortunate that most programming languages also use the star to indicate multiplication. This is also true for the Julia Language.
An input signal, x[n]
, enters a linear system with an impulse response, h[n]
, resulting in an output signal, y[n]
.
In equation form:
$$y[n] = x[n] * h[n]$$
In most DSP applications, the input signal is hundreds, thousands, or even millions of samples in length. The impulse response is usually much shorter, say, a few points to a few hundred points.
The mathematics behind convolution doesn't restrict how long these signals are. It does, however, specify the length of the output signal. The length of the output signal is equal to the length of the input signal, plus the length of the impulse response, minus one.
Now we come to the detailed mathematics of convolution. As used in Digital Signal Processing, convolution can be understood in two separate ways. The first looks at convolution from the viewpoint of the input signal. This involves analyzing how each sample in the input signal contributes to many points in the output signal. The second way looks at convolution from the viewpoint of the output signal. This examines how each sample in the output signal has received information from many points in the input signal.
Keep in mind that these two perspectives are different ways of thinking about the same mathematical operation.
using MySignalProcessing
using Plots
Building our Signals
Julia language is known to be a highly composable Language.
This means that higher order functions are made of other functions. This is a very powerful tool in DSP.
We build our signals by performing a series of operations on the signal, as the following example shows.
X = stepseq(-5,50) + (-1) * (stepseq(0,40) |> sigshift(10))
Let the rectangular pulse:
$$x(n) = u(n) - u(n - 10)$$
be an input to an LTI System with Impulse Response:
$$h(n) = (0.9)^n u(n)$$
x = stepseq(-5,50) + (-1) * (stepseq(0,40) |> sigshift(10))
plot(x.n, X.A, line =:stem, marker=:o, title = "Input Signal")
h = (signal(0.0, -5,50) + realexp(0.9, 0, 50))
plot(h.n, h.A, line =:stem, marker=:o, title = "Impulse Response")
y = conv(x, h)
plot(y.n, y.A, line =:stem, marker=:o, title = "Output Signal")
x = ([3.0, 11.0, 7.0, 0.0, -1.0, 4.0, 2.0], collect(-3:3))
x = signal(x...)
plot(x.n, x.A, line =:stem, marker=:o, title = "Input Signal")
h = ([2.0, 3.0, 0.0, -5.0, 2.0, 1.0], collect(-1:4))
h = signal(h...)
plot(h.n, h.A, line =:stem, marker=:o, title = "Impulse Response")
y = conv(x, h)
plot(y.n, y.A, line =:stem, marker=:o, title = "Output Signal")
We could verify that Convolution Operation is commutative:
y = conv(h, x)
plot(y.n, y.A, line =:stem, marker=:o, title = "Output Signal")