Haar = {0.5, 0.5}; D4 =0.5 * {(1 + Sqrt[3])/4, (3 + Sqrt[3])/4, (3 - Sqrt[3])/4, (1 - Sqrt[3])/4}//N; Coif2 = {-0.051429728471, 0.238929728471, 0.602859456942, 0.272140543058, -0.051429972847, -0.011070271529}; Coif4 = {0.011587596739, -0.02932013798, -0.04763959031, 0.273021046535, 0.574682393857, 0.294867193696, -0.054085607092, -0.042026480461, 0.016744410163, 0.003967883613, -0.001289203356, -0.000509505399}; LHtilde[low_List,high_List,ls_Integer]:= Module[{len = Length[low],tmp}, If[len <= ls, Return[{low, high}], Return[Table[tmp = Floor[(len - i)/ls]; {Sum[low[[i + k*ls]],{k,0,tmp}], Sum[high[[i + k*ls]],{k,0,tmp}]}, {i,1,ls}]//Transpose]]]; indexer[arr_List, ind_List]:= arr[[ Table[ind[[i]]+1, {i, 1, Length[ind]}] ]] highPass[lowFilter_List] := Module[{len = Length[lowFilter]}, Table[((-1)^n)*lowFilter[[len-n+1]], {n,1,len}]]; Options[ForwardConv] = {View -> False}; ForwardConv[low_List, high_List, data_List,opts___Rule] := Module[{lowT, highT, len=Length[data], len2, points, tmp, tmp2, opt}, opt = View /. {opts} /. Options[ForwardConv]; len2 = len/2; tmp2=Min[len,Length[low]]-1; {lowT,highT} = LHtilde[low,high,len]; tmp = (len2-1)*2; points = Table[Table[Mod[i+j,len], {j, 0, tmp2}], {i, 0, tmp, 2}]; Table[tmp = indexer[data, points[[i]]]; {lowT . tmp, highT . tmp}, {i, 1, len2}]//Transpose]; Options[InverseConv] = {View -> False}; InverseConv[low_List, high_List, atmp_List, dtmp_List,opts___Rule] := Module[{lowT, highT, len=Length[atmp], len2, opt, evenOdd, datPoints, filPoints, tmp, tmp1, tmp2,asd,qwe,zxc}, opt = View /. {opts} /. Options[InverseConv]; len2 = 2*len; tmp2 = Min[Length[low]/2,len]; {lowT,highT} = LHtilde[low,high,len2]; tmp = (Length[lowT]/2-1)*2; evenOdd = Table[{i,++i}, {i,0,tmp,2}]//Transpose; filPoints = Flatten[Table[evenOdd,{len}],1]; datPoints = Flatten[Table[tmp = Table[Mod[i-j,len], {j,1,tmp2}]; {tmp,tmp}, {i,1,len}],1]; If[opt == True, tmp1 = Table[(indexer[lowT,filPoints[[i]]] . indexer[atmp,datPoints[[i]]]), {i, 1, len2}]//Chop; tmp2 = Table[(indexer[highT,filPoints[[i]]] . indexer[dtmp,datPoints[[i]]]), {i, 1, len2}]]//Chop; tmp = Table[((indexer[lowT,filPoints[[i]]] . indexer[atmp,datPoints[[i]]])+ (indexer[highT,filPoints[[i]]] . indexer[dtmp,datPoints[[i]]]))*2, {i, 1, len2}]; If[opt == True, tmp = tmp//Chop; asd = ListPlot[tmp1,PlotJoined->True,PlotRange->All, DisplayFunction->Identity, Axes->False,Frame->True,FrameTicks->{None,Automatic}]; qwe = ListPlot[tmp2,PlotJoined->True,PlotRange->All, DisplayFunction->Identity, Axes->False,Frame->True,FrameTicks->{None,Automatic}]; zxc = ListPlot[tmp,PlotJoined->True,PlotRange->All, DisplayFunction->Identity, Axes->False,Frame->True,FrameTicks->{None,Automatic}]; Show[GraphicsArray[{asd,qwe,zxc}]]]; tmp]; Trans1D[data_List, loFil_List, lev_Integer, opts___Rule] := Module[{hiFil, lowL={}, highL={}, i, low, high}, If[lev > Log[2,Length[data]], Return["lev too large"]]; hiFil = highPass[loFil]; low = data; For[i=1,i <= lev, i++, {low, high} = ForwardConv[loFil, hiFil, low, opts]; PrependTo[highL, high]]; {low, highL}]; InvTrans1D[data_List, lfil_List,opts___Rule] := Module[{hfil,temp,lev,i}, lev = Length[data[[2]]]; hfil = highPass[lfil]; temp = data[[1]]; For[i=1, i <= lev, i++, temp = InverseConv[lfil,hfil,temp,data[[2,i]],opts]]; temp]; NonZeros1D[data_] := Module[{nnz}, nnz = Apply[Plus,Map[If[#!=0.0,1,0]&,data[[1]],{1}],{0,1}]; nnz += Apply[Plus,Map[If[#!=0.0,1,0]&,data[[2]],{2}],{0,2}] ]; Options[Thresh1D] = {ShowCompression->False}; Thresh1D[data_, thresh_,opts___Rule] := Module[{opt,size,size2,size3}, opt = ShowCompression /. {opts} /. Options[Thresh1D]; bar = {Map[If[Abs[#]>=thresh,#,0]&,data[[1]],{1}], Map[If[Abs[#]>=thresh,#,0]&,data[[2]],{2}]}; If[opt==True, size = (Length[data[[1]]]*2)^Length[data[[2]]]; size2 = nonZeros1D[bar]; size3 = nonZeros1D[data]; Print["Signal Size: ",size, " transform ", size3, " newTransform ",size2]; Print["Compression: ",N[size2/size3,3]]]; bar ]; <False}; View1DCoeffs[trans_List,opts___Rule] := Module[{n,matrix={},row,depth = Length[trans[[2]]], len,maxLen,foo,bar,ticks}, opt = ThreeD /. {opts} /. Options[View1DCoeffs]; maxLen = 2^depth; row = trans[[1]]; For[n=1,n<=depth,++n, len = Length[row]; foo = Table[m,{m,len}]; bar = Table[foo,{m,maxLen/len}]//Transpose//Flatten; PrependTo[matrix,row[[bar]]]; row = trans[[2,n]]; ]; PrependTo[matrix,{trans[[2,depth]],trans[[2,depth]]}//Transpose//Flatten]; len = Length[trans[[1]]]; ticks = Table[len*2^(n-1),{n,2^(depth-1)+1}]; ticks = Table[len*2^(n-1),{n,Log[2,Length[matrix]]+1}]; If[opt == False, ShowLegend[ ListDensityPlot[matrix,Mesh->False, DisplayFunction->Identity, PlotLabel->"Scaled Coefficient Matrix", FrameTicks->{None,Automatic}, DefaultFont->{"Helvetica-Bold",12}, FrameLabel->{Signal,Depth}], {GrayLevel[1-#]&,10,ToString[N[Max[matrix],2]], ToString[N[Min[matrix],2]], LegendPosition->{1.1,-.6}, LegendSize->{.4,1.4}, LegendTextSpace->3.5}], ListPlot3D[matrix,Boxed->False, PlotRange->All, PlotLabel->"Scaled Coefficient Matrix", Ticks->{Automatic,ticks,Automatic}, AxesLabel->{"Signal","Depth",""}]] ]; Trans2D[signal_, wavelet_] := Module[{ll=signal,lh,hl,hh,n,depth,high = {}, highWave = highPass[wavelet]}, depth = Log[2,Length[signal]]; For[n=1, n<=depth, ++n, {ll, lh, hl, hh} = OneTrans2D[ll,wavelet,highWave]; PrependTo[high,{lh,hl,hh}]]; {ll,high} ]; InvTrans2D[trans_,wavelet_] := Module[{depth=Length[trans[[2]]],low = trans[[1]], high = trans[[2]], n, foo, bar, highWave = highPass[wavelet]}, For[n=1,n<=depth,++n, foo = OneInvTrans2D[low,high[[n,1]],wavelet,highWave]//Transpose; bar = OneInvTrans2D[high[[n,2]],high[[n,3]], wavelet,highWave]//Transpose; low = OneInvTrans2D[foo,bar,wavelet,highWave]; ]; low//Chop ]; OneTrans2D[signal_, wavelet_,highWave_] := Module[{y,low,high,ll={},lh={},hl={},hh={}, len=Length[signal]}, {low,high} = Table[ForwardConv[wavelet,highWave,signal[[x]]],{x,len}]//Transpose; low = Transpose[low]; high = Transpose[high]; len /= 2; For[y=1,y<=len,++y, data = ForwardConv[wavelet,highWave,low[[y]]]; AppendTo[ll,data//First]; AppendTo[lh,data//Rest//First]; data = ForwardConv[wavelet,highWave,high[[y]]]; AppendTo[hl,data//First]; AppendTo[hh,data//Rest//First]; ]; {ll,lh,hl,hh}//Chop ] OneInvTrans2D[low_,high_,wavelet_,highWave_] := Module[{len=Length[low]}, Table[InverseConv[wavelet,highWave,low[[n]],high[[n]]], {n,len}]//Chop]; Noisy[image_,size_] := Map[# + Random[Real,{-size,size}]&,image,2]; (* compute root mean sqared error *) Error2D[one_, two_] := Module[{foo = {}, bar, n}, For[n=1, n<=Length[one], ++n, bar = one[[n]] - two[[n]]; AppendTo[foo, bar.bar]]; Sqrt[(foo.foo)/Length[one]]]; Options[Thresh2D] = {ShowCompression->False}; Thresh2D[data_, thresh_,opts___Rule] := Module[{bar,size,size2,size3,opt}, opt = ShowCompression /. {opts} /. Options[Thresh2D]; bar = {Map[If[Abs[#]>=thresh,#,0]&,data[[1]],{2}], Map[If[Abs[#]>=thresh,#,0]&,data[[2]],{4}]}; If[opt==True, size = ((Length[data[[1]]]*2)^Length[data[[2]]])^2; size2 = nonZeros2D[bar]; size3 = nonZeros2D[data]; Print["Image Size: ",size, " transform ", size3, " newTransform ",size2]; Print["Compression: ",N[size2/size,3]]]; bar ]; NonZeros2D[data_] := Module[{nnz}, nnz = Apply[Plus,Map[If[#!=0.0,1,0]&,data[[1]],{2}],{0,1}]; nnz += Apply[Plus,Map[If[#!=0.0,1,0]&,data[[2]],{4}],{0,3}] ]; (* if one of the ??P parameters is 0, then zero out the component *) Zero2DComponents[trans_,llP_,lhP_,hlP_,hhP_] := Module[{depth=Length[trans[[2]]],base = Length[trans[[1]]],n, newTrans={},ll,lh,hl,hh,newBase}, If[llP==0, ll = Table[0,{base},{base}], ll = trans[[1]]]; For[n=1,n<=depth,++n, newBase = base * 2^(n-1); If[lhP==0, lh = Table[0,{newBase},{newBase}], lh = trans[[2,n,1]]]; If[hlP==0, hl = Table[0,{newBase},{newBase}], hl = trans[[2,n,2]]]; If[hhP==0, hh = Table[0,{newBase},{newBase}], hh = trans[[2,n,3]]]; AppendTo[newTrans,{lh,hl,hh}]; ]; {ll, newTrans} ]; <False}; View2DCoeffs[trans_List,opts___Rule] := Module[{n,matrix=trans[[1]],depth = Length[trans[[2]]],len,opt, asd,qwe,ticks}, opt = ThreeD /. {opts} /. Options[View2DCoeffs]; For[n=1,n<=depth,++n, asd = Map[Flatten[#,1]&,{matrix,trans[[2,n,1]]}//Transpose]; qwe = Map[Flatten[#,1]&,{trans[[2,n,2]],trans[[2,n,3]]}//Transpose]; matrix = Join[asd, qwe]; ]; len = Length[trans[[1]]]; ticks = Table[len*2^(n-1),{n,Log[2,Length[matrix]]+1}]; ticks = Table[len*2^(n-1),{n,2^(depth-1)+1}]; If[opt == False, ShowLegend[ ListDensityPlot[matrix,Mesh->False, PlotRange->All, DisplayFunction->Identity, FrameLabel->{"HiLo:HiHi\nLoLo:LoHi",""}, DefaultFont->{"Helvetica-Bold",10}, FrameTicks->{ticks,ticks}], {GrayLevel[1-#]&,10,ToString[N[Max[matrix],2]], ToString[N[Min[matrix],2]], LegendPosition->{1.1,-.6}, LegendSize->{.4,1.4}, LegendTextSpace->3.5}], ListPlot3D[matrix,Boxed->False, PlotRange->All, Ticks->{ticks,ticks,Automatic}, DefaultFont->{"Helvetica-Bold",10}, AxesLabel->{"Lo:Hi","Lo:Hi",""}]] ];