# NAME

Math::WalshTransform.pm - Fast Hadamard and Walsh Transforms

# SYNOPSIS

```
use Math::WalshTransform;
@f = (1.618, 2.718, 3.142, 4.669); # must be power-of-two long
@FH1 = &fht(@f); # Hadamard transform
@fh1 = &fhtinv(@FH1);
# or
@FW2 = &fwt(@f); # Walsh transform
@fw2 = &fwtinv(@FW2);
@FH2 = &walsh2hadamard(@FW2);
@PS = &power_spectrum(@f);
import Math::WalshTransform qw(:ALL);
@whats_going_on = &biggest(9,&fwt(&sublist(\@time_series,-16)));
@EVENT1 = &fwt(&sublist(\@time_series,478,16));
@EVENT2 = &fwt(&sublist(\@time_series,2316,16));
@EVENT3 = &fwt(&sublist(\@time_series,3261,16));
$EVENT1[$[]=0.0; $EVENT2[$[]=0.0; $EVENT3[$[]=0.0; # ignore constant
@EVENT1 = &normalise(@EVENT1); # ignore scale
@EVENT2 = &normalise(@EVENT2);
@EVENT3 = &normalise(@EVENT3);
@TYPICAL_EVENT = &average(\@EVENT1, \@EVENT2, \@EVENT3);
...
@NOW = &fwt(&sublist(\@time_series,-16));
$NOW[$[] = 0.0;
@NOW = &normalise(@NOW);
if (&distance(\@NOW, \@TYPICAL_EVENT) < .28) { &get_worried(); }
```

# DESCRIPTION

These routines implement fast Hadamard and Walsh Transforms and their inverse transforms.

Also included are routines for converting a Hadamard to a Walsh transform and vice versa, for calculating the Logical Convolution of two lists, or the Logical Autocorrelation of a list, and for calculating the Walsh Power Spectrum - in short, almost everything you ever wanted to do with a Walsh Transform.

Intelligible speech can be reconstructed by transforming blocks of, say, 64 samples, deleting all but the several largest transform components, and inverse-transforming; in other words, these transforms extract from a time-series the most significant things that are going on. They should be useful for **noticing important things**, for example in software that monitors time-series data such as system or network administration data, share-price, currency, ecological, opinion poll, process management data, and so on.

As from version 1.10, Math::WalshTransform uses C routines to perform the transforms. This runs 25 to 30 times faster than previous versions.

Not yet included are multi-dimensional Hadamard and Walsh Transforms, conversion between Logical and Arithmetic Autocorrelation Functions, or conversion between the Walsh Power Spectrum and the Fourier Power Spectrum.

Version 1.17

# SUBROUTINES

Routines which take just one array as argument expect the array itself; those which take more than one array expect a list of references.

*fht*(@f);-
The argument

*@f*is the list of values to be transformed. The number of values must be a power of 2.*fht*returns a list*@F*of the Hadamard transform. *fhtinv*(@F);-
The argument

*@F*is the list of values to be inverse-transformed. The number of values must be a power of 2.*fhtinv*returns a list*@f*of the inverse Hadamard transform. *fwt*(@f);-
The argument

*@f*is the list of values to be transformed. The number of values must be a power of 2.*fwt*returns a list*@F*of the Walsh transform. *fwtinv*(@F);-
The argument

*@F*is the list of values to be inverse-transformed. The number of values must be a power of 2.*fwtinv*returns a list*@f*of the inverse Walsh transform. *walsh2hadamard*(@F);-
The argument

*@F*is a Walsh transform;*walsh2hadamard*returns a list of the corresponding Hadamard transform. *hadamard2walsh*(@F);-
The argument

*@F*is a Hadamard transform;*hadamard2walsh*returns a list of the corresponding Walsh transform. *logical_convolution(\@x, \@y)*-
The arguments are references to two arrays of values

*x*and*y*which must both be of the same size which must be a power of 2.*logical_convolution*returns a list of the logical (or dyadic) convolution of the two sets of values. See the MATHEMATICS section ... *logical_autocorrelation(@x)*-
The argument is a list of values

*x*; the number of values must be a power of 2.*logical_autocorrelation*returns a list of the logical (or dyadic) autocorrelation of the set of values. See the MATHEMATICS section ... *power_spectrum(@x)*-
The argument is a list of values

*x*; the number of values must be a power of 2.*power_spectrum*returns a list of the Walsh Power Spectrum of the set of values. See the MATHEMATICS section ...

# EXPORT_OK SUBROUTINES

The following routines are not exported by default, but are exported under the *ALL* tag, so if you need them you should:

` import Math::WalshTransform qw(:ALL);`

*biggest($k,@x)*-
The first argument

*$k*is the number of elements of the array*@x*which will be conserved;*biggest*returns an array in which the biggest*$k*elements are intact and in place, and the other elements are set to zero. If*$k*is 0 or negative, then*biggest*returns an array in which all elements less than the average (absolute) size have been set to zero. *sublist(\@array, $offset, $length)*-
This routine returns a part of the

*@array*without, as*splice*does, munging the original array. It applies to arrays the same sort of syntax that*substr*applies to strings; the sublist is extracted starting at*$offset*elements from the front of the array; if*$offset*is negative the sublist starts that far from the end of the array instead; if*$length*is omitted, everything to the end of the array is returned; if*$length*is negative, the length is calculated to leave that many elements off the end of the array. *distance(\@array1, \@array2)*-
This routine returns the distance between the two arrays, according to the Euclidian Metric; in other words, the square root of the sum of the squares of the differences between the corresponding elements.

*size(@array)*-
This routine returns the distance between the array and an array of zeroes, according to the Euclidian Metric; in other words, the square root of the sum of the squares of the elements.

*normalise(@array)*-
This routine returns an array scaled so that its

*size*is 1.0 *average(\@array1, \@array2, ... \@arrayN)*-
This routine returns an array in which each element is the average of the corresponding elements of all the argument arrays.

*product(\@array1, \@array2)*-
This routine returns an array in which each element is the product of the corresponding elements of the argument arrays.

# MATHEMATICS

The Hadamard matrix is a square array of plus and minus ones, whose rows and columns are orthogonal to each other. Hence, the product of the matrix and its tranpose is the identity matrix times a constant *N* which is equal to the order of the matrix. If *N* is a power of two, symmetrical Hadamard matrices can be defined recursively:

```
| 1 1 |
Had = | |
2 | 1 -1 |
| Had Had |
| N N |
Had = | |
2N | Had -Had |
| N N |
```

Each row of the Hadamard matrix corresponds to a Hadamard Function *Had(j,k)* where j = 0...N-1

Another way to describe a Hadamard matrix of dimension 2^N x 2^N is that the entry in row i and column j is (-1)^P, where P is the number of positions in which the binary expansion of i and j share a 1. From this definition it is immediate that the last row (and column) is a Thue-Morse (or Morse-Thue) sequence, and also that rows that are of the form 2^N - 2^j will be j-fold repetitions of the Thue-Morse sequence.

The upper half of the Hadamard matrix are cycles of increasing wavelengths, and the lower half are Morse-Thue sequences on decreasing cell-sizes, much as the components of a Fourier analysis are sine-waves of different wavelengths.

The Walsh matrix is derived from the Hadamard matrix by rearranging the rows so that the number of sign-changes is in increasing order. Each row of the Walsh matrix corresponds to a Walsh Function *Wal(j,k)* where j = 0...N-1

The one-dimensional Hadamard transform pair is defined by

```
F(j) = (1/N) * Sigma f(k)*Had(j,k)
f(j) = Sigma F(k)*Had(j,k)
```

and the one-dimensional Walsh transform pair is defined by

```
F(j) = (1/N) * Sigma f(k)*Wal(j,k)
f(j) = Sigma F(k)*Wal(j,k)
```

The two transforms are equivalent, and conversion between them only involves rearranging the order of the components. Since the Walsh functions are in order of increasing number of sign-changes, the Walsh transform is more Fourier-like, and for that reason is used more often, although it does use several per-cent more CPU time.

Because all the matrix elements are either 1 or -1, these transforms involve almost no multiplications and are computationally very efficient.

The Logical (or Dyadic) Convolution of two arrays x and y is defined by

` z(k) = (1/N) * Sigma x(k^j)*y(j)`

where the ^ is used in its Perl sense, to mean bitwise exclusive-or. There exists a Logical (or Dyadic) Convolution Theorem, analogous to the normal case, that the Walsh transform of the logical convolution of two sequences is the product of their Walsh transforms, and that the Walsh transform of the product of two sequences is the logical convolution of their Walsh transforms.

Likewise there exists a Logical Wiener-Khintchine Theorem, stating that the Walsh Power Spectrum is the Walsh transform of the Logical Autocorrelation Function.

There exist linear transformations converting between Logical Convolution and the normal Arithmetic Convolution, and between the Walsh Power Spectrum and the normal Fourier Power Spectrum.

# AUTHOR

Peter J Billam, www.pjb.com.au/comp/contact.html

# REFERENCES

*Walsh Spectrometry, a form of spectral analysis well suited to binary computation*, J. E. Gibbs, National Physical Lab, Teddington, Middlesex, England, unpublished, 1967

*Hadamard transform image encoding*, W. K. Pratt, J. Kane and H. C. Andrews, Proc. IEEE, Vol. 57, Jan 1969, pp. 58-68

*Walsh function generation*, D. A. Swick, IEEE Transactions on Information Theory (Corresp.), Vol. IT-15 part 1, Jan 1969, p. 167

*Computation of the Hadamard transform and the R-transform in ordered form*, L. J. Ulman, IEEE Trans. Comput. (Corresp.), Vol. C-19, Apr 1970, pp. 359-360

*Computation of the Fast Hadamard Transform*, Ying Shum and Ronald Elliot, Proc. Symp. Appl. Walsh Functions, Washington D.C., 1972, pp. 177-180

*Logical Convolution and Discrete Walsh and Fourier Power Spectra*, Guener Robinson, IEEE Transactions on Audio and Electroacoustics, Vol. AU-20 No. 4, October 1972, pp. 271-280

*Speech processing with Walsh-Hadamard Transforms*, Ying Shum, Ronald Elliot and Owen Brown, IEEE Transactions on Audio and Electroacoustics, Vol. AU-21 No. 3, June 1973, pp. 174-179

# SEE ALSO

```
http://www.pjb.com.au/
http://search.cpan.org/perldoc?Math::WalshTransform
Math::Evol http://search.cpan.org/perldoc?Math::Evol
Term::Clui http://search.cpan.org/perldoc?Term::Clui
Crypt::Tea_JS http://search.cpan.org/perldoc?Crypt::Tea_JS
http://en.wikipedia.org/wiki/Thue-Morse_sequence
http://mathworld.wolfram.com/WalshTransform.html
http://arxiv.org/abs/nlin/0510009
http://arxiv.org/abs/cs/0703057
perl(1).
```