(* 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[ 14054, 449] NotebookOptionsPosition[ 12157, 384] NotebookOutlinePosition[ 12575, 400] CellTagsIndexPosition[ 12532, 397] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Daubechies Filters", "Title", CellChangeTimes->{{3.4217479401079364`*^9, 3.4217479429048653`*^9}}], Cell["\<\ Wavelet Workshop June 2-5, 2009 University of St. Thomas\ \>", "Subtitle", CellChangeTimes->{{3.421426484890625*^9, 3.42142648821875*^9}, { 3.453030366875*^9, 3.4530303721875*^9}}], Cell["\<\ Catherine Beneteau Caroline Haddad David Ruch Patrick Van Fleet\ \>", "Subsubtitle", CellChangeTimes->{{3.42144927371875*^9, 3.42144928703125*^9}}], Cell[CellGroupData[{ Cell["Objective", "Section", CellChangeTimes->{{3.4217479766243973`*^9, 3.421747978296315*^9}}], Cell["\<\ The purpose of this notebook is to introduce you to Daubechies filters and \ see how they can be used in applications.\ \>", "Text"] }, Open ]], Cell[CellGroupData[{ Cell["DiscreteWavelets", "Section"], Cell[TextData[{ StyleBox["You should run this cell each time you open this notebook!!", FontColor->RGBColor[1, 0, 0]], " It loads the ", StyleBox["Mathematica", FontSlant->"Italic"], " package ", StyleBox["DiscreteWavelets", FontFamily->"Courier"], " for use in subsequent computations." }], "Text"], Cell[BoxData[ RowBox[{"<<", "DiscreteWavelets`DiscreteWavelets`"}]], "Input", CellChangeTimes->{{3.421749074496252*^9, 3.421749074590005*^9}, { 3.453030844328125*^9, 3.453030845421875*^9}}, FontColor->GrayLevel[0.500008]] }, Open ]], Cell[CellGroupData[{ Cell["Help on DiscreteWavelets", "Section"], Cell[TextData[{ "If you ever need help with ", StyleBox["DiscreteWavelets", FontFamily->"Courier"], ", go to the ", StyleBox["Documentation Center", FontSlant->"Italic"], ", then look at the lower right for a link called ", StyleBox["Installed Add-Ons", FontSlant->"Italic"], ". Click this and then click ", StyleBox["DiscreteWavelets", FontSlant->"Italic"], ". " }], "Text", CellChangeTimes->{{3.421748017312939*^9, 3.4217480658454313`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Solving for the D4 Filter", "Section", CellChangeTimes->{{3.4217480856584387`*^9, 3.421748091408586*^9}}], Cell["We first set up the orthogonality conditions", "Text", CellChangeTimes->{{3.421748098877527*^9, 3.4217481194249277`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"Clear", "[", RowBox[{"h", ",", "h0", ",", "h1", ",", "h2", ",", "h3"}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"h", "=", RowBox[{"{", RowBox[{"h0", ",", "h1", ",", "h2", ",", "h3"}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"orth", "=", RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"Total", "[", RowBox[{"h", "^", "2"}], "]"}], "\[Equal]", "1"}], ",", RowBox[{ RowBox[{ RowBox[{"h0", "*", "h2"}], "+", RowBox[{"h1", "*", "h3"}]}], "\[Equal]", "0"}]}], "}"}]}]}], "Input", CellChangeTimes->{{3.421748125471958*^9, 3.42174818355157*^9}, { 3.4217489035856276`*^9, 3.4217489036950045`*^9}, {3.4217490726680803`*^9, 3.4217490727930837`*^9}, {3.453030848203125*^9, 3.45303084896875*^9}}], Cell["Next the lowpass conditions ", "Text", CellChangeTimes->{{3.42174818494223*^9, 3.421748239146743*^9}, { 3.45303038496875*^9, 3.453030385125*^9}}], Cell[BoxData[ RowBox[{"lowpass", "=", RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"Total", "[", "h", "]"}], "\[Equal]", RowBox[{"Sqrt", "[", "2", "]"}]}], ",", RowBox[{ RowBox[{"h", ".", RowBox[{"{", RowBox[{"1", ",", RowBox[{"-", "1"}], ",", "1", ",", RowBox[{"-", "1"}]}], "}"}]}], "\[Equal]", "0"}], ",", RowBox[{ RowBox[{"h", ".", RowBox[{"{", RowBox[{"0", ",", RowBox[{"-", "1"}], ",", "2", ",", RowBox[{"-", "3"}]}], "}"}]}], "\[Equal]", "0"}]}], "}"}]}]], "Input",\ CellChangeTimes->{{3.4217482055365076`*^9, 3.421748258475363*^9}, { 3.421748904804408*^9, 3.421748904945037*^9}, {3.4217490716211786`*^9, 3.421749071746182*^9}, {3.453030391296875*^9, 3.45303043871875*^9}, { 3.4530308516875*^9, 3.45303085234375*^9}}], Cell["Now we solve the system", "Text", CellChangeTimes->{{3.421748276100814*^9, 3.421748281772834*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"eqs", "=", RowBox[{"Join", "[", RowBox[{"orth", ",", "lowpass"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"s", "=", RowBox[{"Solve", "[", RowBox[{"eqs", ",", "h"}], "]"}]}], "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"Length", "[", "s", "]"}], ",", "\"\< solutions.\>\""}], "]"}]}], "Input", CellChangeTimes->{{3.421748291179325*^9, 3.4217484861530657`*^9}, { 3.4217489021637154`*^9, 3.421748902304344*^9}, {3.4217490693398705`*^9, 3.421749069480499*^9}, {3.453030465203125*^9, 3.453030476078125*^9}, { 3.453030855234375*^9, 3.453030856234375*^9}}], Cell[TextData[{ "Parsing out the solutions from a Solve call is a bit cumbersome in ", StyleBox["Mathematica", FontSlant->"Italic"], ". Here is some code to do it." }], "Text", CellChangeTimes->{{3.453030483078125*^9, 3.453030508015625*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"sln1", "=", RowBox[{"h", "/.", RowBox[{"s", "[", RowBox[{"[", "1", "]"}], "]"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"sln2", "=", RowBox[{"h", "/.", RowBox[{"s", "[", RowBox[{"[", "2", "]"}], "]"}]}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"TableForm", "[", "sln1", "]"}], ",", "\"\<.\\n\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"TableForm", "[", "sln2", "]"}], ",", "\"\<.\>\""}], "]"}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", "\"\\"", "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"TableForm", "[", RowBox[{"N", "[", "sln1", "]"}], "]"}], ",", "\"\<.\\n\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"TableForm", "[", RowBox[{"N", "[", "sln2", "]"}], "]"}], ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.421748291179325*^9, 3.4217484861530657`*^9}, { 3.4217489021637154`*^9, 3.421748902304344*^9}, {3.4217490693398705`*^9, 3.421749069480499*^9}, 3.453030465203125*^9, {3.453030858453125*^9, 3.45303085909375*^9}}], Cell["Note that the second solution is a reflection of the first.", "Text", CellChangeTimes->{{3.421748489090641*^9, 3.42174849782524*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["The Highpass Filter", "Section", CellChangeTimes->{{3.42174876719151*^9, 3.4217487693634415`*^9}}], Cell["\<\ We obtain the highpass filter by reversing the solution and multiplying each \ coefficient alternately by 1 and - 1.\ \>", "Text", CellChangeTimes->{{3.421748777098014*^9, 3.421748813177063*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"lp", "=", "sln2"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"hp", "=", RowBox[{ RowBox[{"Reverse", "[", "lp", "]"}], "*", RowBox[{"{", RowBox[{"1", ",", RowBox[{"-", "1"}], ",", "1", ",", RowBox[{"-", "1"}]}], "}"}]}]}], ";"}]}], "Input", CellChangeTimes->{{3.421748818786581*^9, 3.4217488986948767`*^9}, { 3.421749065777279*^9, 3.4217490659491587`*^9}, {3.45303053371875*^9, 3.453030556703125*^9}, {3.453030668234375*^9, 3.45303067296875*^9}, { 3.45303072209375*^9, 3.453030749390625*^9}, {3.453030861828125*^9, 3.4530308623125*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["The Wavelet Matrix", "Section", CellChangeTimes->{{3.421748660438778*^9, 3.4217486630169687`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"n", "=", "8"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"r1", "=", RowBox[{"Join", "[", RowBox[{"lp", ",", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"n", "-", "4"}], "}"}]}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"top", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RotateRight", "[", RowBox[{"r1", ",", RowBox[{"2", "*", "k"}]}], "]"}], ",", RowBox[{"{", RowBox[{"k", ",", "0", ",", RowBox[{ RowBox[{"n", "/", "2"}], "-", "1"}]}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"r2", "=", RowBox[{"Join", "[", RowBox[{"hp", ",", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"n", "-", "4"}], "}"}]}], "]"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"bot", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"RotateRight", "[", RowBox[{"r2", ",", RowBox[{"2", "*", "k"}]}], "]"}], ",", RowBox[{"{", RowBox[{"k", ",", "0", ",", RowBox[{ RowBox[{"n", "/", "2"}], "-", "1"}]}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"W", "=", RowBox[{"Join", "[", RowBox[{"top", ",", "bot"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "n", ",", "\"\\"", ",", "n", ",", "\"\< wavelet matrix is \>\"", ",", RowBox[{"MatrixForm", "[", "W", "]"}], ",", "\"\<.\>\""}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"MatrixForm", "[", RowBox[{"Simplify", "[", RowBox[{"W", ".", RowBox[{"Transpose", "[", "W", "]"}]}], "]"}], "]"}], ",", "\"\<.\>\""}], "]"}], ";"}]}], "Input", CellChangeTimes->{{3.4217486744235106`*^9, 3.421748763066405*^9}, { 3.421748896147937*^9, 3.4217488965229464`*^9}, {3.4217490393547277`*^9, 3.421749064011609*^9}, {3.453030565046875*^9, 3.45303061975*^9}, { 3.453030650203125*^9, 3.453030662828125*^9}, {3.45303075971875*^9, 3.453030816484375*^9}, {3.453030865484375*^9, 3.453030865921875*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Daubechies Filters in DiscreteWavelets", "Section", CellChangeTimes->{{3.4217489075076027`*^9, 3.4217489150859213`*^9}}], Cell[TextData[{ "We can generate Daubechies filters in DiscreteWavelets using the ", StyleBox["Daub", FontColor->RGBColor[1, 0, 0]], " command." }], "Text", CellChangeTimes->{{3.421748919429783*^9, 3.421748933633271*^9}}], Cell[BoxData[ RowBox[{"Daub", "[", "2", "]"}]], "Input", CellChangeTimes->{{3.4217489493367987`*^9, 3.4217489510712175`*^9}}], Cell[BoxData[ RowBox[{"Daub", "[", "4", "]"}]], "Input", CellChangeTimes->{{3.421748953165021*^9, 3.4217489539150405`*^9}}], Cell[BoxData[ RowBox[{"Daub", "[", "6", "]"}]], "Input", CellChangeTimes->{{3.4217489573526287`*^9, 3.4217489590245466`*^9}}], Cell[BoxData[ RowBox[{"Daub", "[", "12", "]"}]], "Input", CellChangeTimes->{{3.4217489615089846`*^9, 3.4217489635715375`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Section", CellChangeTimes->{{3.421748966024726*^9, 3.421748967524764*^9}}], Cell["Can you write code to find the D6 filter? The D8 filter?", "Text", CellChangeTimes->{{3.4217489728530254`*^9, 3.4217489812282395`*^9}, { 3.4530307345625*^9, 3.453030737546875*^9}}], Cell[BoxData[ RowBox[{"(*", " ", RowBox[{"Put", " ", "code", " ", "here"}], " ", "*)"}]], "Input", CellChangeTimes->{{3.42174898672838*^9, 3.421748990009714*^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, 105, 1, 73, "Title"], Cell[698, 26, 193, 6, 96, "Subtitle"], Cell[894, 34, 158, 6, 89, "Subsubtitle"], Cell[CellGroupData[{ Cell[1077, 44, 96, 1, 75, "Section"], Cell[1176, 47, 142, 3, 29, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[1355, 55, 35, 0, 75, "Section"], Cell[1393, 57, 312, 10, 29, "Text"], Cell[1708, 69, 226, 4, 41, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[1971, 78, 43, 0, 75, "Section"], Cell[2017, 80, 461, 15, 29, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[2515, 100, 112, 1, 75, "Section"], Cell[2630, 103, 128, 1, 29, "Text"], Cell[2761, 106, 809, 22, 82, "Input"], Cell[3573, 130, 154, 2, 29, "Text"], Cell[3730, 134, 836, 23, 41, "Input"], Cell[4569, 159, 105, 1, 29, "Text"], Cell[4677, 162, 674, 15, 82, "Input"], Cell[5354, 179, 248, 6, 29, "Text"], Cell[5605, 187, 1535, 40, 182, "Input"], Cell[7143, 229, 140, 1, 29, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[7320, 235, 105, 1, 75, "Section"], Cell[7428, 238, 207, 4, 29, "Text"], Cell[7638, 244, 625, 15, 62, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[8300, 264, 105, 1, 75, "Section"], Cell[8408, 267, 2316, 68, 182, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[10761, 340, 127, 1, 75, "Section"], Cell[10891, 343, 227, 6, 29, "Text"], Cell[11121, 351, 127, 2, 41, "Input"], Cell[11251, 355, 125, 2, 41, "Input"], Cell[11379, 359, 127, 2, 41, "Input"], Cell[11509, 363, 128, 2, 41, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[11674, 370, 93, 1, 75, "Section"], Cell[11770, 373, 189, 2, 29, "Text"], Cell[11962, 377, 167, 3, 41, "Input"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)