# Simple Compression Calculations Using MS Excel and Haar Wavelets

## Haar Wavelet Compression

From time to time I have been asked to provide explicit details on the mechanics and methods behind Haar wavelet transforms. The purpose of this post is to walk through two simple examples that demonstrate the use of the Haar transform relative to two one-dimensional signals (time signals). The details of the Haar basis and Haar wavelet transform are available elsewhere. The purpose here is to provide a simple example of how the Haar basis is computed using a simple tool such as an Excel spreadsheet.

## 4×4 Haar Wavelet Example Calculation

Let’s begin with the end product: the following is a 4×4 Haar matrix computed using Microsoft Excel:

The Haar Matrix, which we will denote Hn, is given as follows for the case of 4 data elements, denoted as Vn:

This computes to that shown in the figure above. The Haar wavelet coefficients are computed by first inverting the Haar matrix and multiplying by the output signal vector, Vn:

Haar matrix inverse is calculated using Excel and the result is shown in the following figure:

## Haar MS Excel spreadsheet cell layout

Each cell in the Excel spreadsheet is computed using the following cell entry:

= INDEX(MINVERSE(\$B\$2:\$E\$5),1,1)

Where \$B\$2 corresponds the cell corresponding to the first row and column of the original Haar matrix and \$E\$5 corresponds to the last row and column of the original Haar matrix. The elements (1,1) at the end of the expression must include the components of each cell. So, in the example above, (1,1) represents the element in the first row and column of the matrix inverse. This is place in the first cell of the Excel spreadsheet corresponding to this element. The last row and column element would be (4,4).  So, for example, the cells would be populated as such:

Time and signal vector chosen arbitrarily for this example is as follows:

A plot of this signal is shown in the next figure:

The wavelet coefficients are computed using the follow expression:

It is possible to cull coefficients on some basis, such as their magnitude with respect to the largest coefficient. We can arbitrarily impose a threshold with respect to the largest coefficient (-4.9497) and remove those coefficients (set to zero) that are at or below this magnitude. Suppose we set a threshold of 30%. The wavelet coefficients with 30% threshold imposed result in the removal of the second coefficient:

The signal can be recomputed using this culled set of coefficients. They are calculated using the following expression:

A plot of the signal with an overlay plot of the recreated signal using 30% threshold on the wavelet coefficients is displayed in the figure below. Note the comparison between the two signals, indicating some loss of fidelity owing to the removal of the wavelet coefficient. This is a crude representation of the effects of destructive compression on the reconstruction of signals:

#### 8×8 Wavelet Calculation

The method can be extended easily to any dimension. Let us consider an application of the Haar wavelet transform to an 8×8 Haar matrix:

The inverse of this matrix is as follows:

The base signal is defined as follows:

A plot of this signal provides a convenient visual rendering of the data:

The wavelet coefficients are calculated in precisely the same way as the 4×4 example shown previously:

Finally, the imposition of signal thresholds (20% and 30%) is shown and the signal is reconstructed in the manner previously described, only extended to an 8×8 Haar matrix. The resulting plot with the wavelet threshold impositions are plotted as overlays in the following figure:

If interested in a copy of this spreadsheet, email me at jrz@johnrzaleski.com.

## One Reply to “Simple Compression Calculations Using MS Excel and Haar Wavelets”

1. Hi there,I check your blog named “Simple Compression Calculations Using MS Excel and Haar Wavelets – John R. Zaleski, Ph.D., CAP, CPHIMS” regularly.Your writing style is awesome, keep it up! And you can look our website about proxy server list.