# Wavelets: Relative error vs compression ratio

I need to plot the relative error vs compression ratio diagram of a discrete wavelet transform on an image, but no luck so far .. Here's the problem in short:

1. random_image.jpg
2. compress the image
3. plot the relative error vs compression ratio diagram

Here's the code and below is the link I am using as a reference:

cumulativeEnergy[data_] :=
Module[{c = Sort[Flatten[data]^2, Greater]}, Accumulate[c]/Total[c]]

data = (* insert random image*)

wtdata = DiscreteWaveletTransform[data]

wtE = cumulativeEnergy[Last /@ wtdata[Automatic]];

ListLinePlot[Sqrt[1 - wtE]]


The problem is I don't get a curve like in the example..

Tutorial used as a reference

Thank you !

• What do you get? – Jagra Feb 8 '13 at 3:10

In the old Mathematica wavelet explorer, the one used in the tutorial, coefficients were arranged in decreasing order of energy. You can see that from the following line from the tutorial,

In:= Dimensions /@ wtdata
Out= {{32,32},{3,32,32},{3,64,64}....}


In the new Mathematica 8 wavelets the coefficients are arranged in increasing order of energy as seen from the following example.

 In:= data=ExampleData[{"TestImage","Boat"}];

In:= wtdata=DiscreteWaveletTransform[data]
Out= DiscreteWaveletData[<<DWT>>,<9>,{512,512}]

In:= wpdata=DiscreteWaveletPacketTransform[data]
Out= DiscreteWaveletData[<<DWPT>>,<4>,{512,512}]

In:= wtdata["Dimensions"]
Out= {{0}->{1,256,256},{1}->{1,256,256},{2}->{1,256,256},{3}->{1,256,256},{0,0}->{1,128,128},{0,1}->{1,128,128},{0,2}->{1,128,128},{0,3}->{1,128,128},{0,0,0}->{1,64,64},{0,0,1}->{1,64,64},...


All you need to do is reverse the order of the coefficients,

In:= wtE = Reverse@cumulativeEnergy[wtdata[Automatic, "Values"]];
In:= wtP = Reverse@cumulativeEnergy[wpdata[Automatic, "Values"]];

In:= ListLinePlot[{Sqrt[1 - wtE], Sqrt[1 - wtP]},AxesOrigin -> {1000, 0.1}, PlotLabel -> "relative error vs. compression ratio", PlotStyle -> {Blue, {Red, Dashed}}] • Thank you for the in-depth explanataion :) ! – Sektor Feb 8 '13 at 7:54
• @Leonid It is nice to be part of the Mathematica community :-) – VivekJ Feb 10 '13 at 3:21