Signal Processing - Maple Help

Home : Support : Online Help : System : Information : Updates : Maple 17 : Signal Processing

 Signal Processing

Introduction

The new Signal Processing package in Maple 17 offers a suite of tools for frequency domain analysis, windowing, signal generation & analysis, and more. This document explores several applications of some of the functionality.

 > $\mathrm{with}\left(\mathrm{SignalProcessing}\left[\mathrm{Engine}\right]\right)$
 $\left[{\mathrm{AutoCorrelation}}{,}{\mathrm{BartlettWindow}}{,}{\mathrm{BlackmanWindow}}{,}{\mathrm{Conjugate}}{,}{\mathrm{ConjugateFlip}}{,}{\mathrm{Convolution}}{,}{\mathrm{CrossCorrelation}}{,}{\mathrm{DCT}}{,}{\mathrm{DFT}}{,}{\mathrm{DWT}}{,}{\mathrm{DotProduct}}{,}{\mathrm{DownSample}}{,}{\mathrm{FFT}}{,}{\mathrm{FiniteImpulseResponseFilter}}{,}{\mathrm{GenerateButterworthTaps}}{,}{\mathrm{GenerateChebyshev1Taps}}{,}{\mathrm{GenerateFiniteImpulseResponseFilterTaps}}{,}{\mathrm{GenerateGaussian}}{,}{\mathrm{GenerateJaehne}}{,}{\mathrm{GenerateSlope}}{,}{\mathrm{GenerateTone}}{,}{\mathrm{GenerateTriangle}}{,}{\mathrm{GenerateUniform}}{,}{\mathrm{HammingWindow}}{,}{\mathrm{HannWindow}}{,}{\mathrm{InfiniteImpulseResponseFilter}}{,}{\mathrm{InverseDCT}}{,}{\mathrm{InverseDFT}}{,}{\mathrm{InverseDWT}}{,}{\mathrm{InverseFFT}}{,}{\mathrm{KaiserWindow}}{,}{\mathrm{Magnitude}}{,}{\mathrm{Maximum}}{,}{\mathrm{MaximumEvery}}{,}{\mathrm{Mean}}{,}{\mathrm{MeanStandardDeviation}}{,}{\mathrm{Minimum}}{,}{\mathrm{MinimumEvery}}{,}{\mathrm{MinimumMaximum}}{,}{\mathrm{Norm}}{,}{\mathrm{NormDifference}}{,}{\mathrm{Phase}}{,}{\mathrm{PowerSpectrum}}{,}{\mathrm{StandardDeviation}}{,}{\mathrm{Sum}}{,}{\mathrm{Threshold}}{,}{\mathrm{UpSample}}\right]$ (1.1)

Application: Imaging a Speech Spectrogram

Import Wave File and Manipulate Data into Overlapping Slices

 > $\mathrm{with}\left(\mathrm{plots}\right):$
 > $\mathrm{filename}≔\mathrm{FileTools}:-\mathrm{JoinPath}\left(\left[\mathrm{kernelopts}\left(\mathrm{datadir}\right),"audio","maplesim.wav"\right]\right)$
 ${\mathrm{filename}}{:=}{"C:\Program Files\Maple 18\data\audio\maplesim.wav"}$ (2.1.1)
 > $\mathrm{data}:=\mathrm{AudioTools}:-\mathrm{Read}\left(\mathrm{filename}\right)$
 > $\mathrm{samplingRate}:={\mathrm{attributes}\left(\mathrm{data}\right)}_{1}$
 ${\mathrm{samplingRate}}{:=}{11025}$ (2.1.2)
 > $\mathrm{len}:=\mathrm{evalf}\left(\frac{\mathrm{numelems}\left(\mathrm{data}\right)}{\mathrm{samplingRate}}\right)$
 ${\mathrm{len}}{:=}{0.7462131519}$ (2.1.3)

Slice the data into segments with 256 samples. Each slice has a 50% overlap with the previous slice (that is, each slice starts at the half-way point of the previous slice).

 > $\mathrm{samps}:=256:$
 > $\mathrm{nTimes}:=\mathrm{floor}\left(\frac{2\mathrm{numelems}\left(\mathrm{data}\right)}{\mathrm{samps}}\right)-1$
 ${\mathrm{nTimes}}{:=}{63}$ (2.1.4)
 >

Calculate the Spectrogram Data

Filter the data, slice by slice.

 > $a≔⟨⟨\mathrm{seq}\left(\mathrm{HannWindow}\left(\mathrm{sample}\left[..,i\right]\right),i=1..\mathrm{nTimes}\right)⟩⟩:$

Calculate FFT of time segment consecutively.

 > $\mathrm{FFTa}≔⟨⟨\mathrm{seq}\left(\mathrm{FFT}\left(a\left[i,..\right]\right),i=1..\mathrm{nTimes}\right)⟩⟩:$

Calculate Power Spectrum of each slice consecutively.

 > $\mathrm{spectra}≔⟨⟨\mathrm{seq}\left(\mathrm{sqrt}~\left(\mathrm{PowerSpectrum}\left(\mathrm{FFTa}\left[i,..\right]\right)\right),i=1..\mathrm{nTimes}\right)⟩⟩:$

Convert spectrum into decibels.

 > $\mathrm{dB}:=x→20\mathrm{log10}\left(x\right):$
 > $\mathrm{spectra}:=\mathrm{map}\left(\mathrm{dB},\mathrm{spectra}\right):$

Strip out the repeated data.

 > $\mathrm{sim}:=\mathrm{LinearAlgebra}:-\mathrm{SubMatrix}\left(\mathrm{ℜ}\left(\mathrm{spectra}\right),1..\mathrm{nTimes},1..\frac{\mathrm{samps}}{2}+1\right):$
 > $\mathrm{nFreqs}:=\mathrm{LinearAlgebra}:-\mathrm{ColumnDimension}\left(\mathrm{sim}\right)$
 ${\mathrm{nFreqs}}{:=}{129}$ (2.2.1)

The frequencies go from 0 to half the sampling rate. Hence the frequencies are in steps of (in Hz):

 > $\frac{\mathrm{samplingRate}}{2.\mathrm{nFreqs}}$
 ${42.73255814}$ (2.2.2)

Scale the spectra so that the values are between 0 and 255.

 > $\mathrm{minSim}:=\mathrm{min}\left(\mathrm{sim}\right)$
 ${\mathrm{minSim}}{:=}{-}{111.188885238739}$ (2.2.3)
 > $\mathrm{maxSim}:=\mathrm{max}\left(\mathrm{sim}\right)$
 ${\mathrm{maxSim}}{:=}{4.43801796851939}$ (2.2.4)
 > $\mathrm{scale}:=i→\frac{\left(i-\mathrm{minSim}\right)\cdot 255}{\mathrm{maxSim}-\mathrm{minSim}}:$

Consecutive rows represent slices in time, while columns contain the spectra at each time.

 > $\mathrm{simScaled}≔\mathrm{scale}~\left(\mathrm{sim}\right);$

Plot the Spectrogram and Waveform

 > $\mathrm{commonPlotOpts1}≔\mathrm{labelfont}=\left[\mathrm{Helvetica}\right],\mathrm{labeldirections}=\left[\mathrm{horizontal},\mathrm{vertical}\right]:$
 > $\mathrm{p1}:=\mathrm{listdensityplot}\left(-\mathrm{simScaled},\mathrm{style}=\mathrm{patchnogrid},\mathrm{smooth}=\mathrm{true},\mathrm{tickmarks}=\left[\left[\right],\left[\mathrm{seq}\left(i=\frac{\mathrm{round}\left(\frac{\mathrm{samplingRate}i\cdot 10}{2000.\mathrm{nFreqs}}\right)}{10.},i=0..\mathrm{nFreqs},25\right)\right]\right],\mathrm{labels}=\left["","Frequency \left(kHz\right)"\right],\mathrm{commonPlotOpts1}\right):$



 > $\mathrm{p2}:=\mathrm{listplot}\left(\left[\mathrm{seq}\left(\left[\frac{i}{\mathrm{samplingRate}},{\mathrm{data}}_{i}\right],i=1..\mathrm{numelems}\left(\mathrm{data}\right)\right)\right],\mathrm{thickness}=0,\mathrm{gridlines},\mathrm{axes}=\mathrm{boxed},\mathrm{labels}=\left["Time \left(s\right)","Amplitude"\right],\mathrm{commonPlotOpts1}\right):$
 > \mathrm{display}\left(\mathrm{Array}\left(⟨⟨\mathrm{p1},\mathrm{p2}⟩⟩\right),\mathrm{aligncolumns}=\left[1\right]\right)

Application: Filtering Audio

Import Speech Sample

 > $\mathrm{filename2}≔\mathrm{FileTools}:-\mathrm{JoinPath}\left(\left[\mathrm{kernelopts}\left(\mathrm{datadir}\right),"audio","MapleSimMono11025.wav"\right]\right)$
 ${\mathrm{filename2}}{:=}{"C:\Program Files\Maple 18\data\audio\MapleSimMono11025.wav"}$ (3.1.1)
 > $\mathrm{originalSpeech}:=\mathrm{AudioTools}:-\mathrm{Read}\left(\mathrm{filename2}\right)$

Plot Waveform and Power Spectrum

 > $\mathrm{commonPlotOpts2}≔\mathrm{titlefont}=\left[\mathrm{Helvetica},12\right],\mathrm{labelfont}=\left[\mathrm{Helvetica}\right],\mathrm{labeldirections}=\left[\mathrm{horizontal},\mathrm{vertical}\right],\mathrm{axis}=\left[\mathrm{gridlines}=\left[\mathrm{color}="SteelBlue"\right]\right]:$
 > $\mathrm{samplingRate}≔\mathrm{attributes}\left(\mathrm{originalSpeech}\right)\left[1\right]$
 ${\mathrm{samplingRate}}{:=}{11025}$ (3.2.1)
 > $\mathrm{duration}:=\mathrm{evalf}\left(\mathrm{AudioTools}:-\mathrm{Duration}\left(\mathrm{originalSpeech}\right)\right)$
 ${\mathrm{duration}}{:=}{4.504671202}$ (3.2.2)
 > $\mathrm{p1}:=\mathrm{plots}:-\mathrm{listplot}\left(\left[\mathrm{seq}\left(\left[\frac{i}{\mathrm{samplingRate}},{\mathrm{originalSpeech}}_{i}\right],i=1..\mathrm{numelems}\left(\mathrm{originalSpeech}\right)\right)\right],\mathrm{thickness}=0,\mathrm{gridlines},\mathrm{axes}=\mathrm{boxed},\mathrm{title}="Original Speech",\mathrm{labels}=\left["Time \left(s\right)",\mathrm{Waveform}\right],\mathrm{commonPlotOpts2}\right):$
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{p1}\right)$
 > $\mathrm{fq}:=\mathrm{FFT}\left(\mathrm{originalSpeech}\left[1..{2}^{15}\right]\right):$
 > $\mathrm{psq}:=\mathrm{PowerSpectrum}\left(\mathrm{fq}\right):$
 >
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{ps1}\right)$

Apply IIR Butterworth or Chebyshev Filter

Apply Filter

 > $\mathrm{fc}:=800:$
 > $\mathrm{taps}:=\mathrm{GenerateButterworthTaps}\left(9,\frac{\mathrm{fc}}{\mathrm{samplingRate}},'\mathrm{filtertype}'='\mathrm{lowpass}','\mathrm{normalize}'=\mathrm{true}\right)$
 > $\mathrm{filteredSpeech}:=\mathrm{InfiniteImpulseResponseFilter}\left(\mathrm{originalSpeech},\mathrm{taps}\right):$

View Before and After Power Spectrum and Waveform

 > $\mathrm{FFTfilteredSpeech}:=\mathrm{FFT}\left(\mathrm{filteredSpeech}\left[1..{2}^{15}\right]\right):$
 > $\mathrm{PSfilteredSpeech}:=\mathrm{PowerSpectrum}\left(\mathrm{FFTfilteredSpeech}\right):$
 > $\mathrm{ps2}:=\mathrm{plots}:-\mathrm{pointplot}\left(\left[\mathrm{seq}\left(\left[\frac{i\mathrm{samplingRate}}{{2}^{15}},{\mathrm{PSfilteredSpeech}}_{i}\right],i=1..\frac{{2}^{15}}{2}\right)\right],\mathrm{thickness}=0,\mathrm{color}=\mathrm{black},\mathrm{gridlines},\mathrm{connect}=\mathrm{true},\mathrm{title}="Power Spectrum of Filtered Speech",\mathrm{labels}=\left["Frequency \left(Hz\right)",\mathrm{Power}\right],\mathrm{view}=\left[100..2000,0..1.6\right],{\mathrm{axis}}_{1}=\left[\mathrm{mode}=\mathrm{log}\right],\mathrm{commonPlotOpts2}\right):$
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{Array}\left(⟨⟨\mathrm{ps1}|\mathrm{ps2}⟩⟩\right)\right)$

 > $\mathrm{p2}:=\mathrm{plots}:-\mathrm{listplot}\left(\left[\mathrm{seq}\left(\left[\frac{i}{\mathrm{samplingRate}},{\mathrm{filteredSpeech}}_{i}\right],i=1..\mathrm{numelems}\left(\mathrm{originalSpeech}\right)\right)\right],\mathrm{thickness}=0,\mathrm{gridlines},\mathrm{axes}=\mathrm{boxed},\mathrm{color}=\mathrm{black},\mathrm{title}="Filtered Speech",\mathrm{labels}=\left["Time \left(s\right)",\mathrm{Waveform}\right],\mathrm{commonPlotOpts2}\right):$
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{Array}\left(⟨⟨\mathrm{p1}|\mathrm{p2}⟩⟩\right),\mathrm{view}=\left[0..\mathrm{duration},-1..1\right]\right)$

Apply FIR Filter

Apply Filter

 > $\mathrm{flow}:=200:$
 >
 > $\mathrm{taps}:=\mathrm{GenerateFiniteImpulseResponseFilterTaps}\left(50,\left[\frac{\mathrm{flow}}{\mathrm{samplingRate}},\frac{\mathrm{fhigh}}{\mathrm{samplingRate}}\right],\mathrm{filtertype}=\mathrm{bandpass}\right):$
 > $\mathrm{filteredSpeech}:=\mathrm{FiniteImpulseResponseFilter}\left(\mathrm{originalSpeech},\mathrm{taps}\right):$

View Before and After Power Spectrum and Waveform

 > $\mathrm{FFTfilteredSpeech}:=\mathrm{FFT}\left(\mathrm{filteredSpeech}\left[1..{2}^{15}\right]\right):$
 > $\mathrm{PSfilteredSpeech}:=\mathrm{PowerSpectrum}\left(\mathrm{FFTfilteredSpeech}\right):$
 > $\mathrm{ps2}:=\mathrm{plots}:-\mathrm{pointplot}\left(\left[\mathrm{seq}\left(\left[\frac{i\mathrm{samplingRate}}{{2}^{15}},{\mathrm{PSfilteredSpeech}}_{i}\right],i=1..\frac{{2}^{15}}{2}\right)\right],\mathrm{thickness}=0,\mathrm{color}=\mathrm{black},\mathrm{gridlines},\mathrm{connect}=\mathrm{true},\mathrm{title}="Power Spectrum of Filtered Speech",\mathrm{labels}=\left["Frequency \left(Hz\right)",\mathrm{Power}\right],\mathrm{view}=\left[100..2000,0..1.6\right],{\mathrm{axis}}_{1}=\left[\mathrm{mode}=\mathrm{log}\right],\mathrm{commonPlotOpts2}\right):$
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{Array}\left(⟨⟨\mathrm{ps1}|\mathrm{ps2}⟩⟩\right)\right)$

 > $\mathrm{p2}:=\mathrm{plots}:-\mathrm{listplot}\left(\left[\mathrm{seq}\left(\left[\frac{i}{\mathrm{samplingRate}},{\mathrm{filteredSpeech}}_{i}\right],i=1..\mathrm{numelems}\left(\mathrm{originalSpeech}\right)\right)\right],\mathrm{thickness}=0,\mathrm{gridlines},\mathrm{axes}=\mathrm{boxed},\mathrm{color}=\mathrm{black},\mathrm{title}="Filtered Speech",\mathrm{labels}=\left["Time \left(s\right)",\mathrm{Waveform}\right],\mathrm{commonPlotOpts2}\right):$
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{Array}\left(⟨⟨\mathrm{p1}|\mathrm{p2}⟩⟩\right),\mathrm{view}=\left[0..\mathrm{duration},-1..1\right]\right)$