Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 !

share|improve this question
What do you get? – Jagra Feb 8 '13 at 3:10
up vote 11 down vote accepted

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[8]:= Dimensions /@ wtdata
Out[8]= {{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[1]:= data=ExampleData[{"TestImage","Boat"}];

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

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

 In[4]:= wtdata["Dimensions"]
Out[4]= {{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[5]:= wtE = Reverse@cumulativeEnergy[wtdata[Automatic, "Values"]];
In[6]:= wtP = Reverse@cumulativeEnergy[wpdata[Automatic, "Values"]];

In[81]:= ListLinePlot[{Sqrt[1 - wtE], Sqrt[1 - wtP]},AxesOrigin -> {1000, 0.1}, PlotLabel -> "relative error vs. compression ratio", PlotStyle -> {Blue, {Red, Dashed}}]

enter image description here

share|improve this answer
+1 Great stuff ;) – Vitaliy Kaurov Feb 8 '13 at 5:01
Thank you for the in-depth explanataion :) ! – Sektor Feb 8 '13 at 7:54
+1. It's nice to see you here, Vivek! – Leonid Shifrin Feb 8 '13 at 16:23
@Leonid It is nice to be part of the Mathematica community :-) – VivekJ Feb 10 '13 at 3:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.