(* 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[ 18267, 593] NotebookOptionsPosition[ 16134, 523] NotebookOutlinePosition[ 16524, 540] CellTagsIndexPosition[ 16481, 537] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Solutions (10)", "Title"], Cell["\<\ Assigned: April 2 Due: April 9\ \>", "Subsubtitle", CellChangeTimes->{{3.448290774881641*^9, 3.44829078357275*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"Off", "[", RowBox[{"General", "::", "\"\\""}], "]"}], ";"}]], "Input"], Cell[CellGroupData[{ Cell["Fourier Spectrum and Power Spectrum", "Section"], Cell[TextData[{ "Compute and display the Fourier transform (using ", StyleBox["ListPlotComplex[\[Ellipsis]]", FontWeight->"Bold"], ") and the power spectrum (using ", StyleBox["ListPlot[Log[Abs[\[Ellipsis]]]] ", FontWeight->"Bold"], "or", StyleBox[" ListPlot[Abs[\[Ellipsis]]]", FontWeight->"Bold"], ") for the following data sequences. " }], "Text"], Cell[BoxData[ RowBox[{"nX", " ", "=", " ", RowBox[{ RowBox[{"2", "^", "10"}], " ", RowBox[{"(*", " ", RowBox[{"1", "k", " ", "Samples"}], " ", "*)"}]}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"x1", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mod", "[", RowBox[{ RowBox[{"\[Theta]", " ", "0.2123"}], ",", "1"}], "]"}], ",", " ", RowBox[{"{", RowBox[{"\[Theta]", ",", " ", "0", ",", " ", RowBox[{"nX", " ", "-", "1"}]}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"x2", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Mod", "[", RowBox[{ RowBox[{"\[Theta]", " ", "0.2123"}], ",", RowBox[{"2", "\[Pi]"}]}], "]"}], " ", "<", " ", "\[Pi]"}], ",", " ", "1", ",", " ", RowBox[{"-", "1"}]}], "]"}], ",", " ", RowBox[{"{", RowBox[{"\[Theta]", ",", " ", "0", ",", " ", RowBox[{"nX", " ", "-", "1"}]}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"x3", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"Sqrt", "[", RowBox[{"Abs", "[", RowBox[{"Sin", "[", RowBox[{"\[Theta]", " ", "0.2123"}], "]"}], "]"}], "]"}], ",", " ", RowBox[{"{", RowBox[{"\[Theta]", ",", " ", "0", ",", " ", RowBox[{"nX", " ", "-", "1"}]}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"x1a", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"Mod", "[", RowBox[{ RowBox[{"\[Theta]", " ", "0.02123"}], ",", "1"}], "]"}], ",", " ", RowBox[{"{", RowBox[{"\[Theta]", ",", " ", "0", ",", " ", RowBox[{"nX", " ", "-", "1"}]}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"x2a", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Mod", "[", RowBox[{ RowBox[{"\[Theta]", " ", "0.02123"}], ",", RowBox[{"2", "\[Pi]"}]}], "]"}], " ", "<", " ", "\[Pi]"}], ",", " ", "1", ",", " ", RowBox[{"-", "1"}]}], "]"}], ",", " ", RowBox[{"{", RowBox[{"\[Theta]", ",", " ", "0", ",", " ", RowBox[{"nX", " ", "-", "1"}]}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"x3a", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"Sqrt", "[", RowBox[{"Abs", "[", RowBox[{"Sin", "[", RowBox[{"\[Theta]", " ", "0.02123"}], "]"}], "]"}], "]"}], ",", " ", RowBox[{"{", RowBox[{"\[Theta]", ",", " ", "0", ",", " ", RowBox[{"nX", " ", "-", "1"}]}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[CellGroupData[{ Cell["Solution", "Subsubsection"], Cell[BoxData[ RowBox[{ RowBox[{"ListPlotComplex", "[", RowBox[{"x_List", ",", "options___"}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"p1", ",", "p2"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{ StyleBox["p1", FontColor->RGBColor[0, 0, 1]], " ", "=", " ", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Re", "[", "x", "]"}], ",", RowBox[{"PlotStyle", "\[Rule]", " ", StyleBox[ RowBox[{"RGBColor", "[", RowBox[{"0", ",", "0", ",", "1"}], "]"}], FontColor->RGBColor[0, 0, 1]]}], ",", RowBox[{"DisplayFunction", " ", "\[Rule]", " ", "Identity"}], ",", "options"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ StyleBox["p2", FontColor->RGBColor[1, 0, 0]], " ", "=", " ", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Im", "[", "x", "]"}], ",", RowBox[{"PlotStyle", "\[Rule]", " ", StyleBox[ RowBox[{"RGBColor", "[", RowBox[{"1", ",", "0", ",", "0"}], "]"}], FontColor->RGBColor[1, 0, 0]]}], ",", RowBox[{"DisplayFunction", " ", "\[Rule]", " ", "Identity"}], ",", "options"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Show", "[", RowBox[{ StyleBox["p1", FontColor->RGBColor[0, 0, 1]], ",", StyleBox["p2", FontColor->RGBColor[1, 0, 0]], ",", RowBox[{"DisplayFunction", " ", "\[Rule]", " ", "$DisplayFunction"}]}], "]"}]}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"DoFourierPlots", "[", RowBox[{"x_List", ",", "xName_"}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", "xf", "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"xf", " ", "=", " ", RowBox[{"Fourier", "[", "x", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{"x", ",", " ", RowBox[{"PlotRange", " ", "\[Rule]", " ", "All"}], ",", " ", RowBox[{"PlotLabel", " ", "\[Rule]", " ", "xName"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"ListPlotComplex", "[", RowBox[{"xf", ",", " ", RowBox[{"PlotRange", " ", "\[Rule]", " ", "All"}], ",", " ", RowBox[{"PlotLabel", " ", "\[Rule]", " ", "xName"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Abs", "[", "xf", "]"}], ",", " ", RowBox[{"PlotRange", " ", "\[Rule]", " ", "All"}], ",", " ", RowBox[{"PlotLabel", " ", "\[Rule]", " ", "xName"}], ",", " ", RowBox[{"PlotStyle", " ", "\[Rule]", " ", RowBox[{"PointSize", "[", "0.01", "]"}]}]}], "]"}]}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"DoFourierPlots", "[", RowBox[{"#1", ",", " ", "#2"}], "]"}], "&"}], " ", "@@@", " ", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x1", ",", " ", "\"\\""}], "}"}], ",", " ", RowBox[{"{", RowBox[{"x2", ",", " ", "\"\\""}], "}"}], ",", " ", RowBox[{"{", RowBox[{"x3", ",", " ", "\"\\""}], "}"}]}], "}"}]}]], "Input", CellChangeTimes->{3.448281601765275*^9}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"DoFourierPlots", "[", RowBox[{"#1", ",", " ", "#2"}], "]"}], "&"}], " ", "@@@", " ", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"x1a", ",", " ", "\"\\""}], "}"}], ",", " ", RowBox[{"{", RowBox[{"x2a", ",", " ", "\"\\""}], "}"}], ",", " ", RowBox[{"{", RowBox[{"x3a", ",", " ", "\"\\""}], "}"}]}], "}"}]}]], "Input", CellChangeTimes->{3.448281602653344*^9}] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["If I Had a Million Dollars", "Section"], Cell[TextData[{ "Using the same procedure used in class, import the sound track \"", StyleBox["Million_Dollar_64kb.aif", FontFamily->"Lucida Grande"], "\" and create a frequency spectrogram." }], "Text"], Cell[CellGroupData[{ Cell["Solution", "Subsubsection"], Cell[BoxData[ RowBox[{"Directory", "[", "]"}]], "Input"], Cell[BoxData[ RowBox[{"SetDirectory", "[", "\"\<~/Desktop/\>\"", "]"}]], "Input"], Cell[BoxData[ RowBox[{"s", " ", "=", " ", RowBox[{"Import", "[", "\"\\"", "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"rate", " ", "=", " ", RowBox[{"Part", "[", RowBox[{"s", ",", "1", ",", "2"}], "]"}], StyleBox[ RowBox[{"(*", " ", RowBox[{"Sample", " ", RowBox[{"Rate", ":", " ", RowBox[{"Samples", " ", "Per", " ", "Second"}]}]}], " ", "*)"}], FontColor->RGBColor[0, 1, 0]]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"sData", " ", "=", " ", RowBox[{"Drop", "[", RowBox[{ RowBox[{"Part", "[", RowBox[{"s", ",", "1", ",", "1", ",", "1"}], "]"}], ",", "1"}], "]"}]}], ";", " ", RowBox[{"(*", " ", RowBox[{ RowBox[{"Make", " ", "exactly", " ", RowBox[{"2", "^", "16"}]}], " ", "=", " ", RowBox[{"64", " ", "kS"}]}], " ", "*)"}]}]], "Input", CellChangeTimes->{{3.448281637720045*^9, 3.448281637933391*^9}}], Cell[BoxData[ RowBox[{"nData", " ", "=", " ", RowBox[{"Length", "[", "sData", "]"}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"tfd", "[", RowBox[{"x_List", ",", RowBox[{"windowSize_:", "200"}], ",", " ", RowBox[{"padSize_:", "1"}]}], "]"}], " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "i", ",", "j", ",", "n", ",", "pad", ",", " ", "window", ",", " ", "xw", ",", " ", "xf", ",", " ", "result"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"n", " ", "=", " ", RowBox[{"Length", "[", "x", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"pad", " ", "=", " ", RowBox[{"Table", "[", RowBox[{"0", ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", RowBox[{"windowSize", " ", "padSize"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"window", " ", "=", " ", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"i", " ", "\[LessEqual]", " ", RowBox[{"windowSize", "/", "2"}]}], ",", " ", RowBox[{"(", RowBox[{"i", "-", "1"}], ")"}], " ", ",", RowBox[{"(", RowBox[{"windowSize", " ", "-", " ", "i"}], ")"}]}], "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "windowSize"}], "}"}]}], "]"}], "/", RowBox[{"(", RowBox[{"0.5", " ", "windowSize"}], ")"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"result", " ", "=", " ", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", RowBox[{ RowBox[{ RowBox[{"xw", " ", "=", " ", RowBox[{"Take", "[", RowBox[{"x", ",", " ", RowBox[{"{", RowBox[{"j", ",", RowBox[{"j", "+", "windowSize", "-", "1"}]}], "}"}]}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"xw", " ", "=", " ", RowBox[{"xw", " ", "-", " ", RowBox[{ RowBox[{"(", RowBox[{"Plus", " ", "@@", " ", "xw"}], ")"}], "/", "n"}]}]}], ";", " ", RowBox[{"(*", " ", RowBox[{"remove", " ", "average"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"xw", " ", "=", " ", RowBox[{"Join", "[", RowBox[{"pad", " ", ",", " ", RowBox[{"(", RowBox[{"xw", " ", "window"}], ")"}], ",", " ", "pad"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"xf", " ", "=", " ", RowBox[{"Fourier", "[", "xw", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"result", " ", "=", " ", RowBox[{"Append", "[", RowBox[{"result", ",", " ", RowBox[{"Abs", "[", RowBox[{"Drop", "[", RowBox[{"xf", ",", RowBox[{"-", RowBox[{"Floor", "[", RowBox[{ RowBox[{"Length", "[", "xf", "]"}], "/", "2"}], "]"}]}]}], "]"}], "]"}]}], "]"}]}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"j", ",", " ", "1", ",", " ", RowBox[{"n", "-", "windowSize", " ", "-", "1"}], ",", " ", RowBox[{"windowSize", "/", "2"}]}], "}"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Transpose", "[", "result", "]"}]}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"sTFD", " ", "=", " ", RowBox[{"tfd", "[", "sData", "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\<[Max, Min] = \>\"", ",", RowBox[{"Max", "[", "sTFD", "]"}], ",", "\"\<, \>\"", ",", RowBox[{"Min", "[", "sTFD", "]"}]}], "]"}], ";", RowBox[{"ListDensityPlot", "[", RowBox[{"sTFD", ",", RowBox[{"Mesh", "\[Rule]", "False"}]}], "]"}]}]], "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Band Pass Filter", "Section"], Cell["\<\ Apply a band-pass filter to the signal above, and re-display the spectrogram.\ \ \>", "Text"], Cell[CellGroupData[{ Cell["Solution", "Subsubsection"], Cell[BoxData[ RowBox[{ RowBox[{"sf", " ", "=", " ", RowBox[{"Fourier", "[", "sData", "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"bandpass", "[", RowBox[{"j_", ",", RowBox[{"lp_:", "5000"}], ",", RowBox[{"hp_:", "1000"}]}], "]"}], " ", ":=", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"hp", " ", "<", " ", "j", " ", "<", " ", "lp"}], ")"}], " ", "||", " ", RowBox[{"(", RowBox[{ RowBox[{"nData", " ", "-", " ", "lp"}], " ", "<", " ", "j", " ", "<", " ", RowBox[{"nData", " ", "-", " ", "hp"}]}], ")"}]}], ",", " ", "1.0", ",", " ", "0.0"}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"bpFilter", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"bandpass", "[", "i", "]"}], ",", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "nData"}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"sfBP", " ", "=", " ", RowBox[{"sf", " ", "bpFilter"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"sfBP", " ", ".", " ", RowBox[{"Conjugate", "[", "sfBP", "]"}]}], " ", "//", " ", "Chop"}], ")"}], " ", "/", " ", RowBox[{"(", RowBox[{"sData", " ", ".", "sData"}], ")"}], " ", StyleBox[ RowBox[{"(*", " ", RowBox[{"energy", " ", "fraction", " ", "retained"}], " ", "*)"}], FontColor->RGBColor[0, 1, 0]]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"xfBP", " ", "=", " ", RowBox[{"InverseFourier", "[", "sfBP", "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"ListPlay", "[", RowBox[{ RowBox[{"Re", "[", "xfBP", "]"}], ",", " ", RowBox[{"SampleRate", " ", "\[Rule]", " ", "rate"}]}], "]"}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"sBPTFD", " ", "=", " ", RowBox[{"tfd", "[", RowBox[{"Re", "[", "xfBP", "]"}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\<[Max, Min] = \>\"", ",", RowBox[{"Max", "[", "sBPTFD", "]"}], ",", "\"\<, \>\"", ",", RowBox[{"Min", "[", "sBPTFD", "]"}]}], "]"}], ";", RowBox[{"ListDensityPlot", "[", RowBox[{"sBPTFD", ",", RowBox[{"Mesh", "\[Rule]", "False"}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"ListDensityPlot", "[", RowBox[{"sBPTFD", ",", RowBox[{"Mesh", "\[Rule]", "False"}], ",", RowBox[{"PlotRange", "\[Rule]", RowBox[{"{", RowBox[{"0", ",", "0.02`"}], "}"}]}]}], "]"}]], "Input"] }, Closed]] }, Closed]] }, Open ]] }, WindowSize->{779, 851}, WindowMargins->{{118, Automatic}, {Automatic, 36}}, PrintingCopies->1, PrintingPageRange->{1, Automatic}, FrontEndVersion->"6.0 for Mac OS X x86 (32-bit) (May 21, 2008)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 31, 0, 73, "Title"], Cell[624, 25, 126, 4, 45, "Subsubtitle"], Cell[753, 31, 118, 3, 28, "Input"], Cell[CellGroupData[{ Cell[896, 38, 54, 0, 69, "Section"], Cell[953, 40, 363, 11, 43, "Text"], Cell[1319, 53, 180, 5, 28, "Input"], Cell[1502, 60, 355, 10, 28, "Input"], Cell[1860, 72, 546, 16, 28, "Input"], Cell[2409, 90, 399, 11, 28, "Input"], Cell[2811, 103, 357, 10, 28, "Input"], Cell[3171, 115, 548, 16, 28, "Input"], Cell[3722, 133, 401, 11, 28, "Input"], Cell[CellGroupData[{ Cell[4148, 148, 33, 0, 24, "Subsubsection"], Cell[4184, 150, 1593, 42, 80, "Input"], Cell[5780, 194, 1229, 28, 97, "Input"], Cell[7012, 224, 460, 13, 28, "Input"], Cell[7475, 239, 466, 13, 28, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[7990, 258, 45, 0, 39, "Section"], Cell[8038, 260, 208, 5, 29, "Text"], Cell[CellGroupData[{ Cell[8271, 269, 33, 0, 24, "Subsubsection"], Cell[8307, 271, 57, 1, 28, "Input"], Cell[8367, 274, 82, 1, 28, "Input"], Cell[8452, 277, 122, 2, 28, "Input"], Cell[8577, 281, 328, 9, 28, "Input"], Cell[8908, 292, 465, 13, 28, "Input"], Cell[9376, 307, 100, 2, 28, "Input"], Cell[9479, 311, 3375, 87, 233, "Input"], Cell[12857, 400, 115, 3, 28, "Input"], Cell[12975, 405, 331, 8, 46, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[13355, 419, 35, 0, 39, "Section"], Cell[13393, 421, 103, 3, 27, "Text"], Cell[CellGroupData[{ Cell[13521, 428, 33, 0, 24, "Subsubsection"], Cell[13557, 430, 117, 3, 28, "Input"], Cell[13677, 435, 568, 17, 46, "Input"], Cell[14248, 454, 260, 8, 28, "Input"], Cell[14511, 464, 112, 3, 28, "Input"], Cell[14626, 469, 410, 12, 28, "Input"], Cell[15039, 483, 125, 3, 28, "Input"], Cell[15167, 488, 198, 6, 28, "Input"], Cell[15368, 496, 147, 4, 28, "Input"], Cell[15518, 502, 337, 8, 46, "Input"], Cell[15858, 512, 236, 6, 28, "Input"] }, Closed]] }, Closed]] }, Open ]] } ] *) (* End of internal cache information *)