Coding wavelets the easy way

Summation


All that remains to implement our perfect reconstruction diagram is the summation operator at the right of the diagram.
While it is the simplest to understand ideally, it is not the easiest to implement.
The difficulty is that the two signal that we have may not have the same support. Imagine, for instance, that you have implemented the perfect reconstruction diagram (or, for that matter a simple LTI filter); then the filtering process mechanically increases the size of the support of the signal. Therefore we cannot directly subtract it from the original signal in Matlab, because the signals do not have the same length.
Things are considerably simpler if we have kept track of the signal "d" field in the process. Indeed, together with the length information on the signal, this allows us to always have access to the start and end indices of the signal.

You will code the summation operator in a Matlab function file called "addSignals" which take two structured signal inputs and produces a structured signal output.
Most of the work is two expand, if necessary, the supports of the signals by adding series of zeros so that, in the end, both signals have the same support and can be added by the ordinary Matlab routine. Take care of keeping track of the "d" field in the process.
You may be interested in the "elseif" Matlab keyword.
You may divide your code in to parts:
  • part one aligns the left borders of the support
  • part two aligns the right borders

Summary


We have implemented the last operator in the diagram, i.e., the summation operator:

SummationBox