(* 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[ 34407, 1180] NotebookOptionsPosition[ 29588, 1031] NotebookOutlinePosition[ 30002, 1049] CellTagsIndexPosition[ 29959, 1046] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Using Discrete Fourier Transforms", "Title"], Cell["\<\ APAM 1601y Columbia University\ \>", "Subsubtitle"], Cell[CellGroupData[{ Cell["Introduction", "Section"], Cell["\<\ This notebook illustrates two typical applications of the DFT/FFT: filtering \ (or convolution) and creating spectrograms.\ \>", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Importing Sampled Data", "Section"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " can import common file formats. AIFF (or .aif) stands for \"audio \ interchange file format\" is one of the most widely used format for storing \ and transmitting sampled sound. (Another format, that uses lossy data \ compression, is MP3 (or MPEG-1 Audio Layer-3). ", StyleBox["Mathematica", FontSlant->"Italic"], " can not import an MP3 file. MP3 generally reduces data size by a factor of \ 12 while retaining the perception of high fidelity.)" }], "Text"], Cell[CellGroupData[{ Cell["Set Directory Path and Audio File Import", "Subsection"], Cell["\<\ In order to import a file, the path to the directory/folder containing the \ data must be specified.\ \>", "Text"], Cell[BoxData[ RowBox[{"Directory", "[", "]"}]], "Input", CellLabel->"In[1]:="], Cell[BoxData[ RowBox[{"SetDirectory", "[", "\"\<~/Desktop/\>\"", "]"}]], "Input", CellLabel->"In[2]:="], Cell[BoxData[ RowBox[{"s", " ", "=", " ", RowBox[{ "Import", "[", "\"\\"", "]"}]}]], "Input", CellLabel->"In[3]:="], Cell[BoxData[ RowBox[{"Show", "[", "s", "]"}]], "Input", CellLabel->"In[4]:="] }, Closed]], Cell[CellGroupData[{ Cell["Examine Sound Object", "Subsection"], Cell[BoxData[ RowBox[{"?", "s"}]], "Input", CellLabel->"In[5]:="], Cell[BoxData[ RowBox[{"Short", "[", " ", RowBox[{"InputForm", "[", "s", "]"}], "]"}]], "Input", CellLabel->"In[6]:="], Cell[BoxData[ RowBox[{"Shallow", "[", RowBox[{ RowBox[{"InputForm", "[", "s", "]"}], ",", "5"}], "]"}]], "Input", CellLabel->"In[7]:="], 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", CellLabel->"In[8]:="], Cell[BoxData[ RowBox[{ RowBox[{"1", "/", "rate"}], " ", "//", " ", "EngineeringForm", RowBox[{"(*", " ", RowBox[{"time", " ", "per", " ", "sample"}], " ", "*)"}]}]], "Input", CellLabel->"In[9]:="], Cell[BoxData[ RowBox[{ RowBox[{"Shallow", "[", RowBox[{"Part", "[", RowBox[{"s", ",", "1", ",", "1", ",", "1"}], "]"}], "]"}], StyleBox[" ", FontColor->RGBColor[0, 1, 0]], StyleBox[ RowBox[{"(*", " ", RowBox[{"Data", " ", "List"}], " ", "*)"}], FontColor->RGBColor[0, 1, 0]]}]], "Input", CellChangeTimes->{{3.4476972446644793`*^9, 3.4476972447784643`*^9}}, CellLabel->"In[10]:="], Cell[BoxData[ RowBox[{"Length", "[", RowBox[{"Part", "[", RowBox[{"s", ",", "1", ",", "1", ",", "1"}], "]"}], "]"}]], "Input", CellChangeTimes->{{3.4476972474715242`*^9, 3.447697247593424*^9}}, CellLabel->"In[11]:="] }, Closed]], Cell[CellGroupData[{ Cell["ListPlot", "Subsection"], 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.447697250144682*^9, 3.4476972502819*^9}}, CellLabel->"In[12]:="], Cell[BoxData[ RowBox[{"nData", " ", "=", " ", RowBox[{"Length", "[", "sData", "]"}]}]], "Input", CellLabel->"In[13]:="], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"Max", "[", "sData", "]"}], ",", " ", RowBox[{"Min", "[", "sData", "]"}]}], "}"}]], "Input", CellLabel->"In[14]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{"sData", ",", RowBox[{"{", RowBox[{"1", ",", "nData", ",", "10"}], "}"}]}], "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}]}], "]"}]], "Input", CellLabel->"In[15]:="], Cell["\<\ Closer examination, within a narrow range of samples,\[Ellipsis]\ \>", "Text"], Cell[BoxData[ RowBox[{"Floor", "[", RowBox[{"nData", RowBox[{"{", RowBox[{"0.5", ",", "0.51"}], "}"}]}], "]"}]], "Input", CellLabel->"In[16]:="], Cell[BoxData[ RowBox[{ RowBox[{"Take", "[", RowBox[{"sData", ",", " ", RowBox[{"Floor", "[", RowBox[{"nData", RowBox[{"{", RowBox[{"0.5", ",", "0.51"}], "}"}]}], "]"}]}], "]"}], " ", "//", " ", "Shallow"}]], "Input", CellLabel->"In[17]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{"sData", ",", RowBox[{"Floor", "[", RowBox[{"nData", " ", RowBox[{"{", RowBox[{"0.5`", ",", "0.51`"}], "}"}]}], "]"}]}], "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}], ",", RowBox[{"Joined", "\[Rule]", "True"}], ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[18]:="] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Fourier Transform", "Section"], Cell[TextData[{ "In ", StyleBox["Mathematica,", FontSlant->"Italic"], " ", StyleBox["Fourier[\[Ellipsis]]", FontWeight->"Bold"], " can be applied to sound data and used for digital signal processing. " }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"sf", " ", "=", " ", RowBox[{"Fourier", "[", "sData", "]"}]}], ";"}], " ", ")"}], " ", "//", " ", "Timing"}]], "Input", CellLabel->"In[19]:="], Cell[BoxData[ RowBox[{"sf", " ", "//", " ", "Shallow"}]], "Input", CellLabel->"In[20]:="], Cell[TextData[{ "When ", StyleBox["Fourier[\[Ellipsis]]", FontWeight->"Bold"], " is applied to a sequence of Real numbers, it returns a list of Complex \ numbers. We'll plot these functions using the special command. " }], "Text"], 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", CellLabel->"In[21]:="], Cell[BoxData[ RowBox[{ RowBox[{"ListPlotComplex", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{"sf", ",", " ", RowBox[{"{", RowBox[{"1", ",", " ", RowBox[{"Length", "[", "sf", "]"}], ",", " ", "10"}], "}"}]}], "]"}], ",", " ", RowBox[{"PlotRange", "\[Rule]", " ", "All"}]}], "]"}], ";"}]], "Input", CellLabel->"In[22]:="], Cell[BoxData[ RowBox[{ RowBox[{"ListPlotComplex", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{"sf", ",", RowBox[{"{", RowBox[{"1", ",", "500"}], "}"}]}], "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}], ",", RowBox[{"Joined", "\[Rule]", "True"}]}], "]"}], ";"}]], "Input", CellLabel->"In[23]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Log", "[", RowBox[{"Abs", "[", RowBox[{"Take", "[", RowBox[{"sf", ",", RowBox[{"{", RowBox[{"1", ",", RowBox[{"Floor", "[", FractionBox[ RowBox[{"Length", "[", "sf", "]"}], "2"], "]"}], ",", "5"}], "}"}]}], "]"}], "]"}], "]"}], ",", RowBox[{"PlotRange", "\[Rule]", RowBox[{"{", RowBox[{"1.`", ",", RowBox[{"-", "6.`"}]}], "}"}]}]}], "]"}]], "Input", CellLabel->"In[24]:="], Cell[BoxData[ RowBox[{ RowBox[{"frequencies", " ", "=", " ", RowBox[{ RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"i", " ", RowBox[{"rate", "/", "nData"}]}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "0", ",", " ", RowBox[{"nData", "/", "2"}]}], " ", "}"}]}], "]"}], " ", "~", "Join", "~", " ", RowBox[{"Reverse", "[", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"-", "i"}], " ", RowBox[{"rate", "/", "nData"}]}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", RowBox[{ RowBox[{"nData", "/", "2"}], "-", "1"}]}], "}"}]}], "]"}], "]"}]}], " ", "//", " ", "N"}]}], ";"}]], "Input", CellLabel->"In[25]:="], Cell[BoxData[ RowBox[{ RowBox[{"sff", " ", "=", " ", RowBox[{"Transpose", "[", RowBox[{"{", RowBox[{"frequencies", ",", " ", "sf"}], "}"}], "]"}]}], ";"}]], "Input",\ CellLabel->"In[26]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Apply", "[", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"#1", ",", RowBox[{"Log", "[", RowBox[{"Abs", "[", "#2", "]"}], "]"}]}], "}"}], "&"}], ",", RowBox[{"Take", "[", RowBox[{"sff", ",", RowBox[{"{", RowBox[{"1", ",", RowBox[{"Length", "[", "sff", "]"}], ",", "10"}], "}"}]}], "]"}], ",", RowBox[{"{", "1", "}"}]}], "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}], ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[27]:="], Cell[BoxData[{ RowBox[{ RowBox[{"sDataf", " ", "=", " ", RowBox[{"InverseFourier", "[", "sf", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"sDataf", " ", "//", " ", "Shallow"}]}], "Input", CellLabel->"In[28]:="], Cell[BoxData[ RowBox[{"Max", "[", RowBox[{"Abs", "[", RowBox[{"sData", " ", "-", " ", "sDataf"}], "]"}], "]"}]], "Input", CellLabel->"In[30]:="] }, Closed]], Cell[CellGroupData[{ Cell["Rayleigh Energy Theorem (Parseval's Theorem)", "Section"], Cell[BoxData[ RowBox[{"sData", " ", ".", " ", "sData"}]], "Input", CellLabel->"In[31]:="], Cell[BoxData[ RowBox[{ RowBox[{"sf", " ", ".", " ", RowBox[{"Conjugate", "[", "sf", "]"}]}], " ", "//", " ", "Chop"}]], "Input",\ CellLabel->"In[32]:="] }, Closed]], Cell[CellGroupData[{ Cell["Creating a Filter", "Section"], Cell["\<\ The DFT transform signals sampled in time into signals sampled in frequency. \ A filter eliminates specific bands in frequency space. In this section, a \ low-pass filter and a high-pass filter are applied to Dylan Thomas's reading.\ \ \>", "Text"], Cell[CellGroupData[{ Cell["Low Pass Filter", "Subsection"], Cell[BoxData[ RowBox[{"5000", " ", RowBox[{"rate", " ", "/", "nData"}]}]], "Input", CellLabel->"In[33]:="], Cell[BoxData[ RowBox[{ RowBox[{"lowpass", "[", RowBox[{"j_", ",", RowBox[{"lp_:", "5000"}]}], "]"}], " ", ":=", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"j", " ", "<", "lp"}], " ", "||", " ", RowBox[{"j", " ", ">", " ", RowBox[{"nData", " ", "-", " ", "lp"}]}]}], ",", "1.0", ",", "0.0"}], "]"}]}]], "Input", CellLabel->"In[34]:="], Cell[BoxData[ RowBox[{ RowBox[{"lpFilter", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"lowpass", "[", "i", "]"}], ",", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "nData"}], "}"}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[35]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", "lpFilter", "]"}]], "Input", CellLabel->"In[36]:="], Cell[BoxData[ RowBox[{ RowBox[{"sfLP", " ", "=", " ", RowBox[{"sf", " ", "lpFilter"}]}], ";"}]], "Input", CellLabel->"In[37]:="], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"sfLP", " ", ".", " ", RowBox[{"Conjugate", "[", "sfLP", "]"}]}], " ", "//", " ", "Chop"}], ")"}], " ", "/", " ", RowBox[{"(", RowBox[{"sData", " ", ".", "sData"}], ")"}], " ", StyleBox[ RowBox[{"(*", " ", RowBox[{"energy", " ", "fraction", " ", "retained"}], " ", "*)"}], FontColor->RGBColor[0, 1, 0]]}]], "Input", CellLabel->"In[38]:="], Cell[BoxData[ RowBox[{ RowBox[{"ListPlotComplex", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{"sfLP", ",", " ", RowBox[{"{", RowBox[{"1", ",", " ", RowBox[{"Length", "[", "sf", "]"}], ",", " ", "10"}], "}"}]}], "]"}], ",", " ", RowBox[{"PlotRange", "\[Rule]", " ", "All"}]}], "]"}], ";"}]], "Input", CellLabel->"In[39]:="], Cell[BoxData[ RowBox[{"Count", "[", RowBox[{"sfLP", ",", RowBox[{"c_", "/;", RowBox[{ RowBox[{"Abs", "[", "c", "]"}], ">", "0"}]}]}], "]"}]], "Input", CellLabel->"In[40]:="], Cell[BoxData[ RowBox[{ RowBox[{"xfLP", " ", "=", " ", RowBox[{"InverseFourier", "[", "sfLP", "]"}]}], ";"}]], "Input", CellLabel->"In[41]:="], Cell[BoxData[ RowBox[{"xfLP", " ", "//", " ", "Shallow"}]], "Input", CellLabel->"In[42]:="], Cell[BoxData[ RowBox[{"Max", "[", RowBox[{"Im", "[", "xfLP", "]"}], "]"}]], "Input", CellLabel->"In[43]:="], Cell[BoxData[ RowBox[{"Max", "[", RowBox[{"Re", "[", "xfLP", "]"}], "]"}]], "Input", CellLabel->"In[44]:="], Cell[BoxData[ RowBox[{"ListPlay", "[", RowBox[{ RowBox[{"Re", "[", "xfLP", "]"}], ",", " ", RowBox[{"SampleRate", " ", "\[Rule]", " ", "rate"}]}], "]"}]], "Input", CellChangeTimes->{3.447697153898098*^9}, CellLabel->"In[45]:="] }, Closed]], Cell[CellGroupData[{ Cell["Band Pass Filter", "Subsection"], Cell[BoxData[ RowBox[{"1000", " ", RowBox[{"rate", " ", "/", "nData"}]}]], "Input", CellLabel->"In[46]:="], 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", CellLabel->"In[47]:="], Cell[BoxData[ RowBox[{ RowBox[{"bpFilter", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"bandpass", "[", "i", "]"}], ",", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "nData"}], "}"}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[48]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", "bpFilter", "]"}]], "Input", CellLabel->"In[49]:="], Cell[BoxData[ RowBox[{ RowBox[{"sfBP", " ", "=", " ", RowBox[{"sf", " ", "bpFilter"}]}], ";"}]], "Input", CellLabel->"In[50]:="], 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", CellLabel->"In[51]:="], Cell[BoxData[ RowBox[{ RowBox[{"ListPlotComplex", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{"sfBP", ",", " ", RowBox[{"{", RowBox[{"1", ",", " ", RowBox[{"Length", "[", "sf", "]"}], ",", " ", "10"}], "}"}]}], "]"}], ",", " ", RowBox[{"PlotRange", "\[Rule]", " ", "All"}]}], "]"}], ";"}]], "Input", CellLabel->"In[52]:="], Cell[BoxData[ RowBox[{"Count", "[", RowBox[{"sfBP", ",", RowBox[{"c_", "/;", RowBox[{ RowBox[{"Abs", "[", "c", "]"}], ">", "0"}]}]}], "]"}]], "Input", CellLabel->"In[53]:="], Cell[BoxData[ RowBox[{ RowBox[{"xfBP", " ", "=", " ", RowBox[{"InverseFourier", "[", "sfBP", "]"}]}], ";"}]], "Input", CellLabel->"In[54]:="], Cell[BoxData[ RowBox[{"ListPlay", "[", RowBox[{ RowBox[{"Re", "[", "xfBP", "]"}], ",", " ", RowBox[{"SampleRate", " ", "\[Rule]", " ", "rate"}]}], "]"}]], "Input", CellChangeTimes->{3.447697157432435*^9}, CellLabel->"In[55]:="] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Time-Domain Filters", "Section"], Cell[TextData[{ "A filter can also be applied using ", StyleBox["ListConvolve[\[Ellipsis]]", FontWeight->"Bold"], ". In general, convolution requires much more computation than the FFT. \ Indeed, many convolution routines use the FFT to speed execution. In this \ section, the \"time-equivalent\" signals of the filters used above are \ computed using the IDFT." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"fLPtime", " ", "=", " ", RowBox[{"InverseFourier", "[", "lpFilter", "]"}]}], ";"}]], "Input", CellLabel->"In[56]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Re", "[", "fLPtime", "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}], ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[57]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{ RowBox[{"Re", "[", "fLPtime", "]"}], ",", RowBox[{"{", RowBox[{"1", ",", "100"}], "}"}]}], "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}], ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[58]:="], Cell[BoxData[ RowBox[{"Plus", " ", "@@", " ", "fLPtime"}]], "Input", CellLabel->"In[59]:="], Cell[BoxData[ RowBox[{ RowBox[{"fBPtime", " ", "=", " ", RowBox[{"InverseFourier", "[", "bpFilter", "]"}]}], ";"}]], "Input", CellLabel->"In[60]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Re", "[", "fBPtime", "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}], ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[61]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Take", "[", RowBox[{ RowBox[{"Re", "[", "fBPtime", "]"}], ",", RowBox[{"{", RowBox[{"1", ",", "100"}], "}"}]}], "]"}], ",", RowBox[{"PlotRange", "\[Rule]", "All"}], ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[62]:="], Cell[BoxData[ RowBox[{"Plus", " ", "@@", " ", "fBPtime"}]], "Input", CellLabel->"In[63]:="], Cell[CellGroupData[{ Cell["ListConvolve", "Subsection"], Cell[BoxData[ RowBox[{"?", "ListConvolve"}]], "Input", CellLabel->"In[64]:="], Cell[BoxData[ RowBox[{"lista", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"a", "[", "i", "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "10"}], "}"}]}], "]"}]}]], "Input",\ CellLabel->"In[65]:="], Cell[BoxData[ RowBox[{"ListConvolve", "[", RowBox[{ RowBox[{"{", RowBox[{"1", ",", " ", RowBox[{"-", "2"}], ",", " ", "1"}], "}"}], ",", " ", "lista", ",", " ", "2"}], "]"}]], "Input", CellLabel->"In[66]:="], Cell[BoxData[ RowBox[{"lpKernel", " ", "=", " ", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"i", "<=", "8"}], ",", " ", RowBox[{"i", "-", "1"}], ",", " ", RowBox[{"16", " ", "-", " ", "i"}]}], "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "16"}], "}"}]}], "]"}], "/", "56"}]}]], "Input", CellLabel->"In[67]:="], Cell[BoxData[ RowBox[{"Plus", " ", "@@", " ", "lpKernel"}]], "Input", CellLabel->"In[68]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", "lpKernel", "]"}]], "Input", CellLabel->"In[69]:="], Cell[BoxData[ RowBox[{ RowBox[{"skLP", " ", "=", " ", RowBox[{"ListConvolve", "[", RowBox[{"lpKernel", ",", "sData", ",", "8"}], "]"}]}], ";"}]], "Input", CellLabel->"In[70]:="], Cell[BoxData[ RowBox[{"ListPlay", "[", RowBox[{ RowBox[{"Re", "[", "skLP", "]"}], ",", " ", RowBox[{"SampleRate", " ", "\[Rule]", " ", "rate"}]}], "]"}]], "Input", CellChangeTimes->{3.447697161487874*^9}, CellLabel->"In[71]:="], Cell[BoxData[ RowBox[{ RowBox[{"pk1", " ", "=", " ", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Log", "[", RowBox[{"Abs", "[", RowBox[{"Take", "[", RowBox[{"sf", ",", " ", RowBox[{"{", RowBox[{"1", ",", " ", RowBox[{"Floor", "[", RowBox[{ RowBox[{"Length", "[", "sf", "]"}], "/", "2"}], "]"}], ",", " ", "5"}], "}"}]}], "]"}], "]"}], "]"}], ",", " ", RowBox[{"PlotRange", "\[Rule]", " ", RowBox[{"{", RowBox[{"1.0", ",", " ", RowBox[{"-", "9.0"}]}], "}"}]}], ",", " ", RowBox[{"PlotStyle", "\[Rule]", " ", StyleBox[ RowBox[{"RGBColor", "[", RowBox[{"0", ",", "1", ",", "0"}], "]"}], FontColor->RGBColor[0, 1, 0]]}], ",", " ", RowBox[{"DisplayFunction", " ", "\[Rule]", " ", "Identity"}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[72]:="], Cell[BoxData[ RowBox[{ RowBox[{"pk2", " ", "=", " ", RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Log", "[", RowBox[{"Abs", "[", RowBox[{"Take", "[", RowBox[{ RowBox[{"Fourier", "[", "skLP", "]"}], ",", " ", RowBox[{"{", RowBox[{"1", ",", " ", RowBox[{"Floor", "[", RowBox[{ RowBox[{"Length", "[", "sf", "]"}], "/", "2"}], "]"}], ",", " ", "5"}], "}"}]}], "]"}], "]"}], "]"}], ",", " ", RowBox[{"PlotRange", "\[Rule]", " ", RowBox[{"{", RowBox[{"1.0", ",", " ", RowBox[{"-", "9.0"}]}], "}"}]}], ",", " ", RowBox[{"DisplayFunction", " ", "\[Rule]", " ", "Identity"}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[73]:="], Cell[BoxData[ RowBox[{"Show", "[", RowBox[{"pk1", ",", "pk2", ",", RowBox[{"DisplayFunction", "\[Rule]", "$DisplayFunction"}], ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[74]:="] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Frequency Spectrograms", "Section"], Cell["\<\ A frequency spectrogram is made by applying the DFT to a small time \ \"window\" that moves along the sequency of samples. A frequency spectrogram \ is also called a \"time-Fourier-transform\", or TFD.\ \>", "Text"], 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", CellLabel->"In[75]:="], Cell[BoxData[ RowBox[{ RowBox[{"sTFD", " ", "=", " ", RowBox[{"tfd", "[", "sData", "]"}]}], ";"}]], "Input", CellLabel->"In[76]:="], Cell[BoxData[ RowBox[{"ListDensityPlot", "[", RowBox[{"sTFD", ",", RowBox[{"Mesh", "\[Rule]", "False"}], ",", " ", RowBox[{"PerformanceGoal", " ", "\[Rule]", " ", "\"\\""}], ",", " ", RowBox[{"PlotRange", " ", "\[Rule]", " ", "All"}]}], "]"}]], "Input", CellChangeTimes->{{3.4476972000905027`*^9, 3.447697206661646*^9}, { 3.4476972996600323`*^9, 3.44769735437197*^9}}, CellLabel->"In[83]:="], Cell[BoxData[ RowBox[{"ListPlay", "[", RowBox[{"sData", ",", " ", RowBox[{"SampleRate", " ", "\[Rule]", " ", "rate"}]}], "]"}]], "Input", CellChangeTimes->{ 3.447697153898098*^9, {3.4476974514945803`*^9, 3.447697452684217*^9}}, CellLabel->"In[84]:="], Cell[BoxData[{ RowBox[{ RowBox[{"windowSize", "=", "200"}], ";"}], "\n", RowBox[{ RowBox[{"padSize", "=", "1"}], ";"}], "\n", RowBox[{ RowBox[{"dataSize", "=", RowBox[{"windowSize", " ", RowBox[{"(", RowBox[{"1", "+", RowBox[{"2", " ", "padSize"}]}], ")"}]}]}], ";", RowBox[{"frequenciesTFD", "=", RowBox[{"N", "[", RowBox[{"Table", "[", RowBox[{ FractionBox[ RowBox[{"i", " ", "rate"}], "dataSize"], ",", RowBox[{"{", RowBox[{"i", ",", "0", ",", FractionBox["dataSize", "2"]}], "}"}]}], "]"}], "]"}]}], ";", RowBox[{"ListPlot", "[", RowBox[{"frequenciesTFD", ",", RowBox[{ "PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]}]}], "Input", CellLabel->"In[78]:="] }, Closed]], Cell[CellGroupData[{ Cell["Summary", "Section"], Cell["\<\ The digital Fourier transform (DFT) has become a work-horse of modern digital \ signal processing. Using a short sequence of sampled voice-data, we've \ illustrated how to manipulate and analyze the spectral content of \ time-varying signals.\ \>", "Text"] }, Closed]] }, Open ]] }, WindowSize->{848, 879}, WindowMargins->{{14, Automatic}, {Automatic, 0}}, PrintingCopies->1, PrintingPageRange->{1, Automatic}, ShowSelection->True, FrontEndVersion->"6.0 for Mac OS X x86 (32-bit) (May 21, 2008)", StyleDefinitions->"TutorialBook.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 50, 0, 51, "Title"], Cell[643, 25, 61, 3, 41, "Subsubtitle"], Cell[CellGroupData[{ Cell[729, 32, 31, 0, 86, "Section"], Cell[763, 34, 146, 3, 23, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[946, 42, 41, 0, 54, "Section"], Cell[990, 44, 530, 11, 59, "Text"], Cell[CellGroupData[{ Cell[1545, 59, 62, 0, 34, "Subsection"], Cell[1610, 61, 124, 3, 23, "Text"], Cell[1737, 66, 80, 2, 26, "Input"], Cell[1820, 70, 105, 2, 26, "Input"], Cell[1928, 74, 150, 4, 28, "Input"], Cell[2081, 80, 80, 2, 24, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[2198, 87, 42, 0, 34, "Subsection"], Cell[2243, 89, 67, 2, 24, "Input"], Cell[2313, 93, 122, 3, 20, "Input"], Cell[2438, 98, 143, 4, 24, "Input"], Cell[2584, 104, 351, 10, 24, "Input"], Cell[2938, 116, 207, 5, 26, "Input"], Cell[3148, 123, 413, 12, 26, "Input"], Cell[3564, 137, 226, 5, 24, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3827, 147, 30, 0, 34, "Subsection"], Cell[3860, 149, 487, 14, 24, "Input"], Cell[4350, 165, 124, 3, 24, "Input"], Cell[4477, 170, 171, 5, 26, "Input"], Cell[4651, 177, 277, 8, 26, "Input"], Cell[4931, 187, 88, 2, 24, "Text"], Cell[5022, 191, 158, 5, 24, "Input"], Cell[5183, 198, 276, 9, 26, "Input"], Cell[5462, 209, 473, 13, 45, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[5984, 228, 36, 0, 54, "Section"], Cell[6023, 230, 222, 8, 24, "Text"], Cell[6248, 240, 216, 7, 26, "Input"], Cell[6467, 249, 91, 2, 26, "Input"], Cell[6561, 253, 235, 6, 42, "Text"], Cell[6799, 261, 1617, 43, 118, "Input"], Cell[8419, 306, 371, 11, 26, "Input"], Cell[8793, 319, 341, 10, 26, "Input"], Cell[9137, 331, 535, 17, 84, "Input"], Cell[9675, 350, 795, 24, 63, "Input"], Cell[10473, 376, 209, 7, 26, "Input"], Cell[10685, 385, 651, 20, 44, "Input"], Cell[11339, 407, 226, 6, 45, "Input"], Cell[11568, 415, 153, 4, 24, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[11758, 424, 63, 0, 54, "Section"], Cell[11824, 426, 91, 2, 24, "Input"], Cell[11918, 430, 161, 5, 26, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[12116, 440, 36, 0, 54, "Section"], Cell[12155, 442, 258, 5, 59, "Text"], Cell[CellGroupData[{ Cell[12438, 451, 37, 0, 34, "Subsection"], Cell[12478, 453, 111, 3, 26, "Input"], Cell[12592, 458, 387, 12, 28, "Input"], Cell[12982, 472, 283, 9, 26, "Input"], Cell[13268, 483, 92, 2, 26, "Input"], Cell[13363, 487, 136, 4, 26, "Input"], Cell[13502, 493, 434, 13, 45, "Input"], Cell[13939, 508, 373, 11, 26, "Input"], Cell[14315, 521, 192, 6, 28, "Input"], Cell[14510, 529, 149, 4, 26, "Input"], Cell[14662, 535, 93, 2, 26, "Input"], Cell[14758, 539, 112, 3, 24, "Input"], Cell[14873, 544, 112, 3, 24, "Input"], Cell[14988, 549, 240, 6, 26, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[15265, 560, 38, 0, 34, "Subsection"], Cell[15306, 562, 111, 3, 26, "Input"], Cell[15420, 567, 592, 18, 46, "Input"], Cell[16015, 587, 284, 9, 26, "Input"], Cell[16302, 598, 92, 2, 26, "Input"], Cell[16397, 602, 136, 4, 26, "Input"], Cell[16536, 608, 434, 13, 45, "Input"], Cell[16973, 623, 373, 11, 26, "Input"], Cell[17349, 636, 192, 6, 28, "Input"], Cell[17544, 644, 149, 4, 26, "Input"], Cell[17696, 650, 240, 6, 26, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[17985, 662, 38, 0, 54, "Section"], Cell[18026, 664, 379, 8, 60, "Text"], Cell[18408, 674, 156, 4, 26, "Input"], Cell[18567, 680, 264, 7, 26, "Input"], Cell[18834, 689, 378, 11, 45, "Input"], Cell[19215, 702, 93, 2, 26, "Input"], Cell[19311, 706, 156, 4, 26, "Input"], Cell[19470, 712, 265, 7, 26, "Input"], Cell[19738, 721, 379, 11, 45, "Input"], Cell[20120, 734, 93, 2, 26, "Input"], Cell[CellGroupData[{ Cell[20238, 740, 34, 0, 34, "Subsection"], Cell[20275, 742, 79, 2, 26, "Input"], Cell[20357, 746, 252, 8, 26, "Input"], Cell[20612, 756, 230, 7, 26, "Input"], Cell[20845, 765, 430, 13, 26, "Input"], Cell[21278, 780, 94, 2, 24, "Input"], Cell[21375, 784, 92, 2, 26, "Input"], Cell[21470, 788, 190, 5, 26, "Input"], Cell[21663, 795, 240, 6, 26, "Input"], Cell[21906, 803, 926, 26, 62, "Input"], Cell[22835, 831, 776, 22, 61, "Input"], Cell[23614, 855, 255, 6, 45, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[23918, 867, 41, 0, 54, "Section"], Cell[23962, 869, 225, 4, 59, "Text"], Cell[24190, 875, 3399, 88, 257, "Input"], Cell[27592, 965, 139, 4, 24, "Input"], Cell[27734, 971, 423, 9, 44, "Input"], Cell[28160, 982, 262, 6, 26, "Input"], Cell[28425, 990, 803, 25, 126, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[29265, 1020, 26, 0, 54, "Section"], Cell[29294, 1022, 266, 5, 41, "Text"] }, Closed]] }, Open ]] } ] *) (* End of internal cache information *)