(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 36510, 1155] NotebookOptionsPosition[ 33710, 1064] NotebookOutlinePosition[ 34128, 1080] CellTagsIndexPosition[ 34085, 1077] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Haar Transform 1D", "Title"], Cell["\<\ David Ruch and Patrick J. Van Fleet Minicourse #4, January 2008 Joint Mathematics Meetings San Diego, CA\ \>", "Subsubtitle", CellChangeTimes->{{3.408563123828125*^9, 3.408563148390625*^9}, { 3.408657592296875*^9, 3.408657594375*^9}}], Cell[CellGroupData[{ Cell["Objective", "Section"], Cell["\<\ In this notebook, we will explore the one-dimensional discrete Haar wavelet \ transform.\ \>", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["Conventions", "Section"], Cell[TextData[{ "This notebook uses the package ", StyleBox["DiscreteWavelets", FontColor->RGBColor[1, 0, 0]], " (written by Patrick Van Fleet). All commands from the ", StyleBox["DiscreteWavelets", FontColor->RGBColor[1, 0, 0]], " library will be denoted in ", StyleBox["red", FontColor->RGBColor[1, 0, 0]], ". Help is available for every command in ", StyleBox["the package", FontColor->GrayLevel[0]], ". Click on Help and then Documentation Center. At the bottom-right of the \ page is a link for Installed AddOns. Click this link and one of the options \ is DiscreteWavelets. Click this link to go to the Help Browser. Like all ", StyleBox["Mathematica", FontSlant->"Italic"], " help screens, the help is \"live\" - you can either execute the commands \ in the help to see the effects of the command or cut and paste them into your \ own notebook.\n\nComments are useful within cells of code. Any code enclosed \ by (* *) is a comment and ignored by the ", StyleBox["Mathematica", FontSlant->"Italic"], " kernel." }], "Text", CellChangeTimes->{{3.4085631561875*^9, 3.408563201375*^9}, { 3.4085632433125*^9, 3.408563315796875*^9}, {3.40856339940625*^9, 3.4085634034375*^9}, {3.408565167890625*^9, 3.40856516825*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Load DiscreteWavelets", "Section", CellChangeTimes->{3.408563584328125*^9}], Cell[BoxData[ RowBox[{"<<", "DiscreteWavelets`DiscreteWavelets`"}]], "Input", CellChangeTimes->{{3.408563455078125*^9, 3.408563467234375*^9}, { 3.408565207546875*^9, 3.40856520834375*^9}, {3.40856604671875*^9, 3.408566047*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["One-Dimensional Discrete Haar Transform Module", "Section"], Cell["\<\ The cell below contains a module for computing the one-dimensional discrete \ Haar wavelet transform. It takes as input a list of n elements. The number \ of elements must be even or the module returns an error message. The routine starts with an error check on the length of the input. Then the \ input is partitioned into an n/2 x 2 matrix. The matrix is multiplied by \ (1/2, 1/2) to obtain the top half of the transform and (-1/2,1/2) to form the \ bottom portion of the transform. The two parts are joined together and \ returned by the module. \ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"HaarWT", "[", "x_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"X", ",", "lp", ",", "hp", ",", "y"}], "}"}], ",", "\[IndentingNewLine]", "\t", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"OddQ", "[", RowBox[{"Length", "[", "x", "]"}], "]"}], ",", RowBox[{ "Return", "[", "\"\\"", "]"}]}], "]"}], ";", "\[IndentingNewLine]", "\t", RowBox[{"X", "=", RowBox[{"Partition", "[", RowBox[{"x", ",", "2", ",", "2"}], "]"}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"lp", "=", RowBox[{"X", ".", RowBox[{"{", RowBox[{ RowBox[{"1", "/", "2"}], ",", RowBox[{"1", "/", "2"}]}], "}"}]}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"hp", "=", RowBox[{"X", ".", RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"-", "1"}], "/", "2"}], ",", RowBox[{"1", "/", "2"}]}], "}"}]}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"y", "=", RowBox[{"Join", "[", RowBox[{"lp", ",", "hp"}], "]"}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"Return", "[", "y", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}]], "Input", CellChangeTimes->{{3.40856368753125*^9, 3.408563689078125*^9}, { 3.408563744203125*^9, 3.40856374434375*^9}, {3.40856521884375*^9, 3.408565238609375*^9}, {3.408566059609375*^9, 3.40856614703125*^9}, { 3.408566390734375*^9, 3.40856639828125*^9}, {3.40865894084375*^9, 3.40865898509375*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Using HaarWT", "Section", CellChangeTimes->{{3.40856374971875*^9, 3.408563751765625*^9}}], Cell[TextData[{ "The cells below contain commands that illustrate the use of ", StyleBox["HaarWT", FontWeight->"Bold"], ". Feel free to change the input list x." }], "Text", CellChangeTimes->{{3.408563756390625*^9, 3.408563758609375*^9}}], Cell[BoxData[{ StyleBox[ RowBox[{ RowBox[{"x", "=", RowBox[{"{", RowBox[{ "1", ",", "2", ",", "3", ",", "4", ",", "5", ",", "6", ",", "7", ",", "8"}], "}"}]}], ";"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "=", RowBox[{"HaarWT", "[", "x", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "x", ",", "\"\< is \>\"", ",", "y", ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.40856376153125*^9, 3.40856376334375*^9}, { 3.408565244421875*^9, 3.40856524525*^9}, {3.408566383765625*^9, 3.408566384140625*^9}}], Cell["What should HaarWT do to a constant vector?", "Text", CellChangeTimes->{{3.40856376840625*^9, 3.408563770390625*^9}}], Cell[BoxData[{ StyleBox[ RowBox[{ RowBox[{"x", "=", RowBox[{"Table", "[", RowBox[{"1", ",", RowBox[{"{", "10", "}"}]}], "]"}]}], ";"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "=", RowBox[{"HaarWT", "[", "x", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "x", ",", "\"\< is \>\"", ",", "y", ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.4085637744375*^9, 3.408563777578125*^9}, { 3.408565248625*^9, 3.40856524975*^9}, {3.408566380609375*^9, 3.40856638096875*^9}}], Cell["\<\ Here is an example that introduces a command we can use to plot the wavelet \ transform.\ \>", "Text"], Cell[BoxData[{ StyleBox[ RowBox[{ RowBox[{"x", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"k", "^", "2"}], ",", RowBox[{"{", RowBox[{"k", ",", "1", ",", "100"}], "}"}]}], "]"}]}], ";"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "=", RowBox[{"HaarWT", "[", "x", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{"x", ",", RowBox[{"PlotStyle", "\[Rule]", "Blue"}]}], "]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "1"}], ",", RowBox[{"Frame", "\[Rule]", "True"}], ",", RowBox[{"UseColors", "\[Rule]", "True"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{"Plot", " ", "the", " ", "differences", " ", "only"}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "1"}], ",", RowBox[{"Frame", "\[Rule]", "True"}], ",", RowBox[{"UseColors", "\[Rule]", "True"}], ",", RowBox[{"Region", "\[Rule]", "HighPass"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], "Input", CellChangeTimes->{{3.408562861484375*^9, 3.40856287496875*^9}, { 3.408563781140625*^9, 3.408563788421875*^9}, {3.408563819578125*^9, 3.408563832875*^9}, {3.408565252390625*^9, 3.4085652533125*^9}, { 3.408566378234375*^9, 3.40856637846875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Partition, Flatten, Transpose", "Section"], Cell[TextData[{ "If you want to better understand the commands ", StyleBox["Partition,", FontWeight->"Bold"], " ", StyleBox["Flatten", FontWeight->"Bold"], ", and ", StyleBox["Transpose", FontWeight->"Bold"], ", have a look at the cells below.\n\nHere are two lists" }], "Text"], Cell[BoxData[{ RowBox[{"a", "=", RowBox[{"{", RowBox[{"a1", ",", "a2", ",", "a3", ",", "a4"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"b", "=", RowBox[{"{", RowBox[{"b1", ",", "b2", ",", "b3", ",", "b4"}], "}"}]}]}], "Input", CellChangeTimes->{{3.40856404525*^9, 3.4085640554375*^9}, { 3.408565257890625*^9, 3.4085652586875*^9}, {3.408566374890625*^9, 3.408566375125*^9}}], Cell["\<\ We can build a 2 x 4 matrix from the lists. The second command prints the \ output in a more conventional way.\ \>", "Text"], Cell[BoxData[{ RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], "]"}]}], "Input", CellChangeTimes->{{3.40856526309375*^9, 3.408565263859375*^9}, { 3.40856637284375*^9, 3.40856637309375*^9}}], Cell[TextData[{ "We can perform operations like ", StyleBox["Transpose", FontWeight->"Bold"], "." }], "Text"], Cell[BoxData[{ RowBox[{"Transpose", "[", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], "]"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", RowBox[{"Transpose", "[", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], "]"}], "]"}]}], "Input", CellChangeTimes->{{3.40856526621875*^9, 3.4085652668125*^9}, { 3.408566368796875*^9, 3.4085663690625*^9}}], Cell["\<\ In the cell below, we use Flatten and Transpose to intertwine the elements of \ a and b. Flatten simply removes all inner sets of { } leaving only a single \ list of numbers.\ \>", "Text"], Cell[BoxData[{ RowBox[{"c", "=", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}]}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "c", "]"}], "\[IndentingNewLine]", RowBox[{"d", "=", RowBox[{"Transpose", "[", "c", "]"}]}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "d", "]"}], "\[IndentingNewLine]", RowBox[{"Flatten", "[", "d", "]"}]}], "Input", CellChangeTimes->{{3.40856526925*^9, 3.408565270015625*^9}, { 3.408566364328125*^9, 3.408566364640625*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["One-Dimensional Inverse Discrete Haar Transform Module", "Section"], Cell["\<\ The cell below contains a module for computing the one-dimensional inverse \ discrete Haar wavelet transform. It takes as input a list of n elements. \ The number of elements must be even or the module returns an error message. The routine starts with an error check on the length of the input. Then the \ input is partitioned into an 2 x n/2 matrix. The matrix is multiplied by (1, \ -1) to obtain the odd elements of the transform and (1,1) to form the even \ elements of the transform. The two parts are then intertwined and returned \ by the module. \ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"IHaarWT", "[", "y_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"Y", ",", "a", ",", "b", ",", "x"}], "}"}], ",", "\[IndentingNewLine]", "\t", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"OddQ", "[", RowBox[{"Length", "[", "y", "]"}], "]"}], ",", RowBox[{ "Return", "[", "\"\\"", "]"}]}], "]"}], ";", "\[IndentingNewLine]", "\t", RowBox[{"Y", " ", "=", " ", RowBox[{"Transpose", "[", RowBox[{"Partition", "[", RowBox[{"y", ",", RowBox[{ RowBox[{"Length", "[", "y", "]"}], "/", "2"}]}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"a", "=", " ", RowBox[{"Y", ".", RowBox[{"{", RowBox[{"1", ",", RowBox[{"-", "1"}]}], "}"}]}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"b", "=", RowBox[{"Y", ".", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"x", "=", RowBox[{"Transpose", "[", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], "]"}]}], ";", "\[IndentingNewLine]", "\t", RowBox[{"Return", "[", RowBox[{"Flatten", "[", "x", "]"}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}], ";"}]], "Input", CellChangeTimes->{{3.4085641175*^9, 3.408564119796875*^9}, { 3.4085652745625*^9, 3.40856527540625*^9}, {3.40856636078125*^9, 3.408566361640625*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Using IHaarWT", "Section", CellChangeTimes->{{3.408565847375*^9, 3.408565853234375*^9}}], Cell[TextData[{ "The cell below contain illustrates the use of ", StyleBox["IHWT1D", FontWeight->"Bold"], ". Feel free to change the input vector x. For something different, I've \ formed x as a list of 20 random integers whose values range from 0 to 100." }], "Text"], Cell[BoxData[ RowBox[{ StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{ "If", " ", "you", " ", "are", " ", "using", " ", "Mathematica", " ", "5.2"}], ",", " ", RowBox[{ "use", " ", "the", " ", "commented", " ", "version", " ", "for", " ", "x", " ", "at", " ", "left"}]}], " ", "*)"}], FontColor->GrayLevel[0]], StyleBox["\[IndentingNewLine]", FontColor->GrayLevel[0]], RowBox[{ StyleBox[ RowBox[{ RowBox[{"x", "=", RowBox[{"RandomInteger", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "100"}], "}"}], ",", RowBox[{"{", "20", "}"}]}], "]"}]}], ";"}], FontColor->GrayLevel[0]], StyleBox[" ", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"Random", "[", RowBox[{"Integer", ",", RowBox[{"{", RowBox[{"0", ",", "100"}], "}"}]}], "]"}], ",", RowBox[{"{", "20", "}"}]}], "]"}], ";"}], " ", "*)"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "=", RowBox[{"HaarWT", "[", "x", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["newx", FontColor->GrayLevel[0]], " ", "=", " ", RowBox[{"IHaarWT", "[", "y", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "x", ",", "\"\<.\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "y", ",", "\"\<.\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", StyleBox["newx", FontColor->GrayLevel[0]], ",", "\"\<.\>\""}], "]"}], ";"}]}]}]], "Input", CellChangeTimes->{{3.40856299865625*^9, 3.40856301365625*^9}, { 3.4085641421875*^9, 3.408564149359375*^9}, {3.4085652793125*^9, 3.408565280078125*^9}, {3.408566357578125*^9, 3.408566358015625*^9}, { 3.408635478484375*^9, 3.40863553746875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["An Illuminating Exercise for Students", "Section"], Cell["\<\ I use this example every time I introduce the Haar wavelet transform. The \ student can create a basic function from Calculus I (some examples are \ provided). The function is then sampled 100 times from a to b. This creates \ a list x that we plot. Note the x-axis on the plot is in terms of the list \ elements while the y-axis is in terms of the list values. Feel free to uncomment other functions, define your own, or change n, a, b.\ \>", "Text"], Cell[BoxData[{ RowBox[{ StyleBox[ RowBox[{ RowBox[{ RowBox[{"f", "[", "t_", "]"}], ":=", RowBox[{"Sin", "[", RowBox[{"2", "*", "Pi", "*", "t"}], "]"}]}], ";"}], FontColor->GrayLevel[0]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{ RowBox[{"f", "[", "t_", "]"}], ":=", RowBox[{"Cos", "[", RowBox[{"2", "*", "Pi", "*", "t"}], "]"}]}], ";"}], " ", "*)"}], FontColor->RGBColor[0, 0, 1]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"f", "[", "t_", "]"}], ":=", RowBox[{"t", "^", "2"}]}], " ", "*)"}], FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"f", "[", "t_", "]"}], ":=", "t"}], " ", "*)"}], FontColor->RGBColor[0, 0, 1]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"f", "[", "t_", "]"}], ":=", "1"}], " ", "*)"}], FontColor->RGBColor[0, 0, 1]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"f", "[", "t_", "]"}], ":=", RowBox[{"E", "^", "t"}]}], " ", "*)"}], FontColor->RGBColor[0, 0, 1]], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{ RowBox[{"f", "[", "t_", "]"}], ":=", RowBox[{"Log", "[", RowBox[{"1", "+", "t"}], "]"}]}], ";"}], " ", "*)"}], FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ StyleBox[ RowBox[{ RowBox[{"n", "=", "100"}], ";"}], FontColor->GrayLevel[0]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"(*", " ", RowBox[{"This", " ", "must", " ", "be", " ", RowBox[{"even", "!"}]}], " ", "*)"}], FontColor->RGBColor[0, 0, 1]]}], "\[IndentingNewLine]", StyleBox[ RowBox[{ RowBox[{"a", "=", "0"}], ";"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", RowBox[{ StyleBox[ RowBox[{ RowBox[{"b", "=", "1"}], ";"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"x", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"f", "[", RowBox[{"a", "+", RowBox[{"k", "/", "n"}]}], "]"}], ",", RowBox[{"{", RowBox[{"k", ",", "0", ",", RowBox[{"n", "-", "1"}]}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{"x", ",", RowBox[{"PlotStyle", "\[Rule]", "Red"}]}], "]"}]}], "Input", CellChangeTimes->{ 3.40856304378125*^9, {3.408565284421875*^9, 3.408565285*^9}, { 3.408566352703125*^9, 3.408566353015625*^9}}], Cell["\<\ Next compute the HWT of x. The first plot is the entire transform. The \ second plot shows only the approximation portion of the transform while the \ third plot shows only the differences portion of the transform. How are the second and third plots related? Try a couple of other functions.\ \ \>", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"y", "=", RowBox[{"HaarWT", "[", "x", "]"}]}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "1"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]", RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "1"}], ",", RowBox[{"Region", "\[Rule]", "LowPass"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]", RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "1"}], ",", RowBox[{"Region", "\[Rule]", "HighPass"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], "Input", CellChangeTimes->{{3.408563051953125*^9, 3.408563057359375*^9}, { 3.408564213234375*^9, 3.408564215265625*^9}, {3.408565293328125*^9, 3.4085653156875*^9}, {3.40856634915625*^9, 3.4085663494375*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Iterating the Process", "Section"], Cell[TextData[{ "In many applications, scientists will ", StyleBox["iterate", FontWeight->"Bold"], " the wavelet transform. Suppose you are given n-vector x and you compute \ the HWT y. The next iteration of transform is applied to the approximation \ portion of the y. Here is some code (bulky) to do the job. Note that to \ iterate a second time requires n to be divisible by 4. " }], "Text"], Cell[BoxData[{ StyleBox[ RowBox[{ RowBox[{"x", " ", "=", " ", RowBox[{"{", RowBox[{ "2", ",", "4", ",", "6", ",", "8", ",", "10", ",", "12", ",", "14", ",", "16"}], "}"}]}], ";"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", RowBox[{ RowBox[{"n", "=", RowBox[{"Length", "[", "x", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"HaarWT", "[", "x", "]"}]}], "\[IndentingNewLine]", StyleBox[ RowBox[{"approx", " ", "=", " ", RowBox[{"Take", "[", RowBox[{"y", ",", RowBox[{"n", "/", "2"}]}], "]"}]}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", StyleBox[ RowBox[{ RowBox[{"diff", " ", "=", " ", RowBox[{"Drop", "[", RowBox[{"y", ",", RowBox[{"n", "/", "2"}]}], "]"}]}], "\[IndentingNewLine]"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", StyleBox[ RowBox[{"z", "=", RowBox[{"HaarWT", "[", "approx", "]"}]}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", StyleBox[ RowBox[{"iterated", " ", "=", " ", RowBox[{"Join", "[", RowBox[{"z", ",", "diff"}], "]"}]}], FontColor->GrayLevel[0]]}], "Input", CellChangeTimes->{{3.40856427290625*^9, 3.408564274578125*^9}, { 3.408564316625*^9, 3.40856431925*^9}, {3.40856532078125*^9, 3.408565321453125*^9}, {3.408566345421875*^9, 3.408566345734375*^9}}], Cell[TextData[{ "In general, if the length of x contains the factor ", Cell[BoxData[ SuperscriptBox["2", "i"]]], ", we can iterate i times. There is a routine in ", StyleBox["DiscreteWavelets", FontColor->RGBColor[1, 0, 0]], " for computing the iterated Haar Wavelet Transform. It is called ", StyleBox["HWT1D", FontColor->RGBColor[1, 0, 0]], ". It needs two arguments. The first is the input vector. The second \ input is the number of iterations. \n\nHere is an example. Note that the \ length of the input vector is divisible by 32 so we can perform as many as 5 \ iterations." }], "Text", CellChangeTimes->{ 3.40856358584375*^9, {3.408565892015625*^9, 3.4085658975625*^9}, { 3.408565930359375*^9, 3.408565944328125*^9}}], Cell[BoxData[{ StyleBox[ RowBox[{ RowBox[{"n", "=", "96"}], ";"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", StyleBox[ RowBox[{ RowBox[{"x", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"k", "/", "n"}], ")"}], "^", "2"}], ",", RowBox[{"{", RowBox[{"k", ",", "0", ",", RowBox[{"n", "-", "1"}]}], "}"}]}], "]"}]}], ";"}], FontColor->GrayLevel[0]], "\n", RowBox[{ RowBox[{ RowBox[{"y", "=", RowBox[{ StyleBox["HWT1D", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"x", ",", RowBox[{"NumIterations", "\[Rule]", "2"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], StyleBox[";", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "2"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], "Input", CellChangeTimes->{{3.408564362609375*^9, 3.408564400296875*^9}, { 3.408565325796875*^9, 3.408565326140625*^9}, {3.4085659496875*^9, 3.408565951296875*^9}, {3.408566189203125*^9, 3.408566190546875*^9}, { 3.408566341515625*^9, 3.408566341828125*^9}}], Cell[TextData[{ "Here is three iterations of the transform. Note the approximation portion \ contains 96/", Cell[BoxData[ SuperscriptBox["2", "3"]]], " = 12 elements." }], "Text"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"y", "=", RowBox[{ StyleBox["HWT1D", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"x", ",", RowBox[{"NumIterations", "\[Rule]", "3"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], StyleBox[";", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "3"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], "Input", CellChangeTimes->{{3.4085645700625*^9, 3.4085645799375*^9}, { 3.408565330140625*^9, 3.408565330578125*^9}, {3.408565955328125*^9, 3.40856595825*^9}, {3.4085661959375*^9, 3.408566198375*^9}, { 3.408566338515625*^9, 3.408566338875*^9}}], Cell["You can look at particular portions of the transform.", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "3"}], ",", RowBox[{"Iteration", "\[Rule]", "2"}], ",", RowBox[{"Region", "\[Rule]", "HighPass"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ StyleBox["WaveletVectorPlot", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "3"}], ",", RowBox[{"Region", "\[Rule]", "LowPass"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], "Input", CellChangeTimes->{{3.408564661171875*^9, 3.408564711765625*^9}, { 3.40856533309375*^9, 3.4085653334375*^9}, {3.40856633596875*^9, 3.408566336328125*^9}}], Cell[TextData[{ "The process is invertible using the ", StyleBox["IHWT1D", FontColor->RGBColor[1, 0, 0]], " routine from ", StyleBox["DiscreteWavelets", FontColor->RGBColor[1, 0, 0]], ". Like ", StyleBox["WT1D", FontColor->RGBColor[1, 0, 0]], ", the routine requires two arguments. The first is the input vector and \ the second is the number of iterations." }], "Text", CellChangeTimes->{ 3.40856358703125*^9, {3.408566224890625*^9, 3.40856625925*^9}}], Cell[BoxData[ RowBox[{ StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{ "If", " ", "you", " ", "are", " ", "using", " ", "Mathematica", " ", "5.2"}], ",", " ", RowBox[{ "use", " ", "the", " ", "commented", " ", "version", " ", "for", " ", "x", " ", "at", " ", "left"}]}], " ", "*)"}], FontColor->GrayLevel[0]], StyleBox["\[IndentingNewLine]", FontColor->GrayLevel[0]], RowBox[{ StyleBox[ RowBox[{ RowBox[{"x", "=", RowBox[{"RandomInteger", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "100"}], "}"}], ",", RowBox[{"{", "32", "}"}]}], "]"}]}], ";"}], FontColor->GrayLevel[0]], StyleBox[" ", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"(*", " ", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"Random", "[", RowBox[{"Integer", ",", RowBox[{"{", RowBox[{"0", ",", "100"}], "}"}]}], "]"}], ",", RowBox[{"{", "32", "}"}]}], "]"}], ";"}], " ", "*)"}], FontColor->GrayLevel[0]], "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "=", RowBox[{ StyleBox["HWT1D", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"x", ",", RowBox[{"NumIterations", "\[Rule]", "5"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], StyleBox[";", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"orig", " ", "=", " ", RowBox[{ StyleBox["IHWT1D", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox[ RowBox[{"y", ",", RowBox[{"NumIterations", "\[Rule]", "5"}]}], FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]}], StyleBox[";", FontColor->GrayLevel[0]]}], "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "x", ",", "\"\<.\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"N", "[", "y", "]"}], ",", "\"\<.\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"N", "[", "orig", "]"}], ",", "\"\<.\>\""}], "]"}], ";"}]}]}]], "Input", CellChangeTimes->{{3.408563074234375*^9, 3.408563084328125*^9}, { 3.40856533653125*^9, 3.408565337*^9}, {3.408565994328125*^9, 3.408566002140625*^9}, {3.4085662081875*^9, 3.40856621496875*^9}, { 3.40856629296875*^9, 3.40856633075*^9}, {3.4086355556875*^9, 3.408635571546875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Orthogonalizing the Transform", "Section"], Cell["\<\ In the early development of wavelets, the transforms were designed to be \ orthogonal matrices. Consider the Haar Wavelet transform matrix of size 8 x \ 8 and its inverse:\ \>", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{"h", "=", RowBox[{"Join", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"1", "/", "2"}], ",", RowBox[{"1", "/", "2"}]}], "}"}], ",", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", "6", "}"}]}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"tp", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RotateRight", "[", RowBox[{"h", ",", RowBox[{"2", "*", "k"}]}], "]"}], ",", RowBox[{"{", RowBox[{"k", ",", "0", ",", "3"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"g", "=", RowBox[{"Join", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"-", "1"}], "/", "2"}], ",", RowBox[{"1", "/", "2"}]}], "}"}], ",", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", "6", "}"}]}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"bt", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RotateRight", "[", RowBox[{"g", ",", RowBox[{"2", "*", "k"}]}], "]"}], ",", RowBox[{"{", RowBox[{"k", ",", "0", ",", "3"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"W8", " ", "=", " ", RowBox[{"Join", "[", RowBox[{"tp", ",", "bt"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", "W8", "]"}], "\[IndentingNewLine]", RowBox[{"MatrixForm", "[", RowBox[{"Inverse", "[", "W8", "]"}], "]"}], "\[IndentingNewLine]"}], "Input",\ CellChangeTimes->{{3.408565340640625*^9, 3.408565340953125*^9}, { 3.4085663270625*^9, 3.408566327421875*^9}}], Cell[TextData[{ "How could we modify the matrix so that it is orthgonal?\n\n\nWhen we use \ orthogonal transformations, we can use the same list as the second arguments \ for ", StyleBox["WT1D", FontColor->RGBColor[0.501961, 0, 0]], " and ", StyleBox["IWT1D", FontColor->RGBColor[0.501961, 0, 0]], ". There is a command in ", StyleBox["DiscreteWavelets", FontColor->RGBColor[1, 0, 0]], " for generating this list:" }], "Text", CellChangeTimes->{3.408563589203125*^9}], Cell[BoxData[ RowBox[{ StyleBox["Haar", FontColor->RGBColor[1, 0, 0]], StyleBox["[", FontColor->GrayLevel[0]], StyleBox["]", FontColor->GrayLevel[0]]}]], "Input", CellChangeTimes->{{3.408565346015625*^9, 3.408565346359375*^9}}] }, Open ]] }, Open ]] }, WindowSize->{1272, 683}, WindowMargins->{{0, Automatic}, {Automatic, 0}}, FrontEndVersion->"6.0 for Microsoft Windows (32-bit) (April 20, 2007)", StyleDefinitions->FrontEnd`FileName[{"Creative"}, "NaturalColor.nb", CharacterEncoding -> "WindowsANSI"] ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 34, 0, 73, "Title"], Cell[627, 25, 247, 6, 69, "Subsubtitle"], Cell[CellGroupData[{ Cell[899, 35, 28, 0, 75, "Section"], Cell[930, 37, 112, 3, 29, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[1079, 45, 30, 0, 75, "Section"], Cell[1112, 47, 1259, 28, 101, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[2408, 80, 82, 1, 75, "Section"], Cell[2493, 83, 234, 4, 41, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2764, 92, 65, 0, 75, "Section"], Cell[2832, 94, 580, 11, 119, "Text"], Cell[3415, 107, 1734, 47, 182, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5186, 159, 96, 1, 75, "Section"], Cell[5285, 162, 245, 6, 29, "Text"], Cell[5533, 170, 671, 19, 82, "Input"], Cell[6207, 191, 124, 1, 29, "Text"], Cell[6334, 194, 631, 18, 82, "Input"], Cell[6968, 214, 112, 3, 29, "Text"], Cell[7083, 219, 1791, 51, 162, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[8911, 275, 48, 0, 75, "Section"], Cell[8962, 277, 291, 11, 65, "Text"], Cell[9256, 290, 400, 10, 62, "Input"], Cell[9659, 302, 135, 3, 29, "Text"], Cell[9797, 307, 297, 7, 62, "Input"], Cell[10097, 316, 114, 5, 29, "Text"], Cell[10214, 323, 370, 9, 62, "Input"], Cell[10587, 334, 200, 4, 29, "Text"], Cell[10790, 340, 484, 10, 122, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[11311, 355, 73, 0, 75, "Section"], Cell[11387, 357, 584, 11, 119, "Text"], Cell[11974, 370, 1646, 45, 182, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[13657, 420, 95, 1, 75, "Section"], Cell[13755, 423, 276, 6, 29, "Text"], Cell[14034, 431, 2168, 64, 162, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[16239, 500, 56, 0, 75, "Section"], Cell[16298, 502, 466, 8, 83, "Text"], Cell[16767, 512, 2888, 95, 302, "Input"], Cell[19658, 609, 325, 7, 65, "Text"], Cell[19986, 618, 1375, 43, 122, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[21398, 666, 40, 0, 75, "Section"], Cell[21441, 668, 406, 8, 47, "Text"], Cell[21850, 678, 1344, 38, 182, "Input"], Cell[23197, 718, 750, 17, 83, "Text"], Cell[23950, 737, 1456, 48, 122, "Input"], Cell[25409, 787, 185, 6, 29, "Text"], Cell[25597, 795, 1002, 32, 82, "Input"], Cell[26602, 829, 69, 0, 29, "Text"], Cell[26674, 831, 980, 30, 82, "Input"], Cell[27657, 863, 471, 14, 29, "Text"], Cell[28131, 879, 2833, 87, 182, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[31001, 971, 48, 0, 75, "Section"], Cell[31052, 973, 196, 4, 29, "Text"], Cell[31251, 979, 1697, 55, 182, "Input"], Cell[32951, 1036, 482, 14, 83, "Text"], Cell[33436, 1052, 246, 8, 41, "Input"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)