Coding wavelets the easy way

Implementing general convolutions using structured signals


We want to implement a function called myconv which will implement general convolution, i.e. it will extend conv to non causal filters and/or signals.
To do so, create a new script file by using the "File" menu of Matlab and selecting the "New>function" article (you may have a different interface for old versions of Matlab). This opens a window with the following text content:
function [ output_args ] = untitled2( input_args )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here


end
Alternatively, you can copy/paste the previous text into any plain text editor.
[ output_args ] means that this where you specify the name of the variables in the Matlab code that will be returned by the function. The brackets are necessary if you have multiple output variables. input_args specifies the list of input variables. Multiple variables are separated by commas. The second line, which begins with the % sign, is where you put the text that will be returned by command "help untitled2".
In our case, the first line will be
function y = myconv(x,f)
i.e. we define a function called myconv with two inputs and one output. The input and output are all structured signals.

What you will do now is program convolution of structured signals by managing the d field. By convention, d is zero is the filter or signal is causal, i.e. its z transform does not use the inverse of z. If it is not causal, then d is the opposite of the lowest degree, meaning it is nonnegative. The idea is that the lowest degree of the z-transform of the output is the sum of the lowest degrees of each input.
Save the source file under the name "myconv.m" is a place where Matlab can find it (typically, your working directory).
The solution is here.

Comments on the meaning of the "d" field
d is set to zero if the signal starts after index 0; if it starts strictly after zero, they we assume that it is padded with zeros at the beginning so we have data that effectively starts at 0.
If d>0, this means that the data starts at the index which is the opposite of d. Why do we use the field name "d"?
This becomes clear if the signal is the impulse response of an LTI filter. If the impulse response starts at negative indices, this means that the filter is not causal.
It is however, impossible to implement a non causal filter in real time, or, for similar reasons, in Simulink.
Consider the case where we have two data channels x and y, with x being unprocessed, and y being processed by an (ideally) non causal filter. Since non causal filters can not be implemented in real time, we use a shifted variant of the impulse response so that it begins at index 0.
However, we have now to process x if we wish to keep the synchronization of the two signals (and we do wish that).
Using the causal variant of the impulse response means that we have delayed the output by d indices. To keep the synchronization, we also have to delay x by d indices.
The corresponding field "d" has named this way because of the necessity of this delay in the real time implementation.

Summary


We have now an implementation of the convolution operator which also works for non causal filters and/or signals. This is necessary because orthogonal wavelets use conjugate mirror filters. In particular, it uses two pairs of filters, with one pair having impulse response which are the symmetric with respect to 0 of the impulse responses of the filters in the other pair. As a consequence, the whole set of filters cannot be causal.
In practice, this means that we can now implement the filters, i.e. the boxes with letters inside, in the following diagram:

reconstruct