Signal and Image Processing
Frequency Spectrum of Irregularly Sampled Data
FindPeakPoints
Spectrogram
Real and Complex Cepstrum
FFTShift
Edge Detect
ImagePeriodogram
More Updates
The new LSPeriodogram command uses the Lomb-Scargle technique to generate a periodogram of data sampled at irregular time intervals. This is in contrast to the standard Fourier approach (such as that used in Periodogram) which requires regularly sampled data.
with⁡SignalProcessing:
Generate an irregularly spaced vector of times
t≔sort⁡LinearAlgebra:-RandomVector⁡210,generator=0..36.0, datatype=float8
Generate a signal using frequencies of 1 Hz and 8 Hz
f1≔1.0:f2≔8.0:
s ≔ Vector⁡210,i→sin⁡f1⋅2⁢π⁢ti+1.5⁢sin⁡f2⋅2⁢π⁢ti,datatype=float8
Generate a periodogram assuming that the signal is sampled at each point in the irregularly spaced time vector
LSPeriodogram⁡t,s,frequencyscale=Hz, size = 800,300
Several options allow you to specify the frequencies over which the periodogram is generated, the normalization and more. Additionally, you can also generate the numerical data used to create this plot with the LSSpectrum command.
LSSpectrum⁡t,s
The new FindPeakPoints command finds the peaks and valleys of 1D data sets. This versatile command offers several options that let you filter out peaks or valleys that are too close, what defines a peak or valley, and more.
Here, we find the fundamental frequency and harmonics of a violin note by locating the peak points of its periodogram. This speaker component is needed to play audio:
First import, play the data, and view the power spectrum.
violinNote ≔ AudioTools:-ReadFileTools:-JoinPathkerneloptsdatadir, audio, ViolinThreePosVibrato.wav..,1;AudioTools:-PlayviolinNote
violinNote≔Sample Rate44100Bit Depth16Channels1Points/Channel64724Duration1.47⁢s
violinPeriodogram≔PeriodogramviolinNote,powerscale=dB:plots:-display⁡violinPeriodogram,size=800,300,view=0..10000,default
Now isolate the peak points on the periodogram, while filtering out any spurious peaks.
periodogramData≔plottools:-getdata⁡violinPeriodogram3:peakPoints≔FindPeakPointsperiodogramData,minimumheight=−40,minimumprominence=40,includeendpoints=false
Overlay the peak points over the periodogram
peakPlot≔plot⁡peakPoints,style=point,symbol=solidcircle, symbolsize=15:plots:-displaypeakPlot,violinPeriodogram,view=1..10000,default, size = 800,300
The fundamental frequency of the violin note is the first frequency in peakPoints (987.8 Hz). The other values are the harmonics (which are approximately integer multiples of the fundamental frequency)
peakPoints2..,1/~peakPoints1,1
You can now specify the overlap when generating a spectrogram (in prior releases, the overlap was fixed at 50%).
Increasing overlap increases frequency resolution, but at the expense of lower time resolution. In other words, increasing overlap will reveal smaller time events, but will smear those events over time. Increasing overlap also increases computation time because there are more time slices to analyze.
Here, we import an audio file and generate a spectrogram with overlaps of 10% and 90%.
aud≔AudioTools:-ReadFileTools:-JoinPathkerneloptsdatadir, audio,maplesim.wav
aud≔Sample Rate11025Bit Depth16Channels1Points/Channel8227Duration0.75⁢s
Spectrogram⁡aud,overlap=0.1,fftsize=256,size=800,300
Spectrogram⁡aud,overlap=0.90,fftsize=256,size=800,300
Maple 2019 features new commands for calculation the real cepstrum, complex cepstrum and inverse complex cepstrum of a signal. These have many applications in seismic analysis, the characterisation of explosions, speech analysis, homomorphic filtering, and more.
In this example, the location of an echo in an audio file is found using the RealCepstrum command; this information is used to remove the echo with an IIR filter.
echoAudio≔AudioTools:-ReadFileTools:-JoinPathkerneloptsdatadir, audio,ding_echo.wav;Fs≔attributes⁡echoAudio1
echoAudio≔Sample Rate22050Bit Depth16Channels1Points/Channel29232Duration1.33⁢s
Fs≔22050
Play the audio - note the echo
DocumentTools:-SetProperty⁡Speaker0,samplerate,Fs;AudioTools:-PlayechoAudio,Speaker0
Vector of times for each point in the audio signal
t ≔ Vector⁡numelems⁡echoAudio,i→1.0⁢iFs:
Use RealCepstrum to identify start of echo
c≔RealCepstrum⁡echoAudio:plott,c,view=default,−0.3..0.3,labels=Quefrency (s),,labeldirections=horizontal,vertical
The cepstrum plot shows a strong peak at a quefrency of just over 0.4 s - that's probably where the echo starts. Let's find the precise location of the peak.
threshold ≔ map⁡x→piecewise⁡x<0.1,0,1,c:ind≔ArrayTools:-SearchArray⁡threshold,4
ind≔27299042
The peak occurs at an index of 9042. Use this information to attenuate the echo with an IIR filter
filterCoeffs ≔ Array1, 0$9040, 0.5:cleanAudio≔FilterechoAudio, filterCoeffs,Array1:
Playing the audio reveals that the echo has been attenuated
AudioTools:-Play⁡cleanAudio,Speaker0
Visualize waveform of audio before and after filtering
pltEchoAudio≔plott,echoAudio,color=DarkOliveGreen,legend=Audio with echo:pltCleanAudio≔plot⁡t,cleanAudio,color=black,legend=Audio with echo removed:plots:-displaypltEchoAudio,pltCleanAudio
FFTShift swaps data in a matrix or a vector into a different position. For a matrix swapped about both dimensions, the quadrants are moved like so.
The Fourier transform of an image places lower frequency data near all four corners, with higher frequency data near the center. In this instance, FFTShift is typically applied to move the lowest frequencies to the center and the highest frequencies to the corners.
This results in a more meaningful visualization of the power spectrum where the lowest frequency data is contiguous, and simplifies the manipulation of frequency data
First, we import an image.
withImageTools:img≔ReadFileTools:-JoinPathkerneloptsdatadir, images,phone.jpg: Embed⁡img
Now, we convert from the image domain to the spatial frequency domain, move the zero-frequency data to the center, and visualize the power spectrum (the square-root scaling simply rescales the frequencies for a more meaningful visualization).
img_fourier≔FFTShift⁡FFT⁡img:EmbedCreatesqrt~abs~img_fourier
Multiply the spatial frequencies by a Gaussian mask (this is similar to a low pass filter that attenuates higher frequency data).
nRows,nCols≔LinearAlgebra:-Dimensionimg:img_fourier_blur ≔ img_fourier⋅~MatrixnRows,nCols,i,j→evalfⅇ−i−nRows22+j−nCols222.0⁢⋅102,datatype=float8:EmbedCreate⁡sqrt~abs~img_fourier_blur
Move the zero frequency data back to their original positions, and then invert the spatial frequency data back to the image domain.
img_low_pass≔Re~InverseFFT⁡FFTShift⁡img_fourier_blur:EmbedFitIntensity⁡Create⁡img_low_pass
The new EdgeDetect command applies an edge detection convolution mask to an image. The command supports several masks including Sobel, Robert, Prewitt 3x3 and Prewitt 4x4.
edge1≔EdgeDetectimg, method = Sobel:EmbedFitIntensityedge1
edge2≔EdgeDetectimg, method = Prewitt4:EmbedFitIntensity⁡edge2
ImagePeriodogram calculates the magnitude of the Fourier transform of an image; this data can then be embedded as an image to visualize the spatial frequencies of the image. By default the lowest frequencies are shifted to the center (this can, however, be modified). Moreover, you can also scale the data to better visualize spatial frequencies that would otherwise not be apparent).
img≔Readcat⁡kernelopts⁡datadir,/images/tree.jpg:Embedimg
EmbedImagePeriodogramimg
Detrend
A new option for the SignalPlot, Periodogram and Spectrogram lets you remove a linear trend from the data, prior to the primary analysis.
Download Help Document