Coding wavelets the easy way

Matlab structures


In the early versions of Matlab, the software only recognized vectors and matrices. This is cumbersome if you want to manipulate heterogenous data.
Starting with version 5, Matlab acknowledges C-like structures, with a similar syntax; the differences is that you not need to declare or reserve memory for using a structure.

Structures are variables which contain heterogenous data. This data can be accessed by using the syntax variable.fieldName, where variable is the name of the structure, and fieldName is the name that has been given for the particular chunk of data you want to access. The list of the fields in a structured variables can be retrieved by calling fieldnames(variable). To see how all of this works, we shall transform our signal variable sig into a structure in the next section.

Structured signal


To transform the variable sig (that you should have in your workspace as a vector), you assign it to a variable with a field named s (s for signal). Since we are lazy and Matlab allows this, the structured variable will have the same name as the original variable, i.e. sig. This is done by executing the following line in the command window:
>> sig.s = sig
to which Matlab should answer with
sig =

s: [1x1024 double]

Note: if you do not want Matlab command lines to produce output on the screen, just put a semi-colon after you command. For instance
>> sig.s = sig;
does not produce any screen output.

Now, this becomes interesting only if you have several fields in the structure. For signals, we use a second field named "d" to store the time (represented as an integer index) at which the signal starts. This will be useful when we shall implement convolution of signals with general filter impulse responses. Since we have no information on the beginning index for sig, we shall assign it the value zero:
>> sig.d = 0
which produces the following output in the command window:
sig =

s: [1x1024 double]
d: 0

Let us do a little sanity check by using the fieldnames function:
>> fieldnames(sig)
produces the output
ans =

's'
'd'
which is a list of strings.

Later on, we shall define the wavelet transform as a structure (but more complicated).

Summary


Heterogenous data can be store in a structure variable using the variable.fieldname syntax.