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:

Excel calculation of 4x4 Haar Matrix
Excel calculation of 4×4 Haar Matrix

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

Haar Matrix 4x4 Definition
Haar Matrix 4×4 Definition

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:

Definition of Haar Wavelet
Definition of Haar Wavelet

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

Excel calculation of 4x4 Haar Matrix Inverse
Excel calculation of 4×4 Haar Matrix Inverse

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:

MS Excel Spreadsheet Cell Calculations
MS Excel Spreadsheet Cell Calculations

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

Sample 4 element signal vector
Sample 4 element signal vector

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

Signal vector plot versus time
Signal vector plot versus time

The wavelet coefficients are computed using the follow expression:

Equation for 4 dimensional Haar wavelet coefficients
Equation for 4 dimensional Haar wavelet coefficients

Excel spreadsheet calculation:Excel spreadsheet calculation of Haar wavelet coefficients

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:

Haar wavelet coefficients with 30% value threshold applied
Haar wavelet coefficients with 30% value threshold applied

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

Equation for signal reconstruction
Equation for signal reconstruction

Excel spreadsheet calculation:

Recreated signal with 30% threshold
Excel calculation of signal reconstruction with 30% threshold applied on Haar wavelet values.

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:

Overlay of original signal and reconstructed signal
Signal overlay plot showing original, complete signal and reconstructed signal based upon signal “compression” achieved by applying 30% signal threshold on Haar wavelet values.

 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:

Haar matrix constructed for 8x8 case
8×8 Haar wavelet matrix

The inverse of this matrix is as follows:

Inverse 8x8 Haar matrix
Matrix inverse of the 8×8 Haar matrix

The base signal is defined as follows:

Base signal containing 8 elements
Arbitrarily chosen 8 element, one-dimensional signal for testing

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

Plot of base 8 element signal
Time series plot of data from base signal

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

Haar discrete wavelet coefficients for 8 element signal
Haar wavelet coefficients calculated using MS Excel

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:

Overlay of original signal and 20%, 30% threshold signals
Plot of original 8 element base signal with overlay plots of reconstructed signal with both 20% and 30% of wavelet coefficients removed based on threshold calculation.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *