Fourier Analysis of Audio Sounds

Sample Sounds

(* Simple functions to scale everything to have a magnitude <= 1. *)
scaled1[data_]:= Module[{max, scaled},
max = Max[Abs[data]];
scaled = Map[# / max &, data]
] (* For 1-dimensional list *)

scaled2[data_]:= Module[{max, scaled},
max = Max[Abs[data[[All, 2]]]];
scaled = Map[{#[[1]], #[[2]] / max} &, data]
] (* For 2-dimensional list *)

colorlist = {Gray, Blue, Orange, Purple};

Pure Sine Wave at 440 Hz

samplerate = 44100;(* Samples per second for the stored sounds. *);
Δt = 1/samplerate;

sin440= Play[Sin[2*π * 440t ],{t,0,1.0}, SampleRate->samplerate]

sin440data = AudioData[Audio[sin440]][[1]];

Violin “A” note

violinA=Sound[SoundNote["A",1,"Violin"]]

violinAdata= AudioData[violinA][[1]];

AudioPlot[violinA]

Trumpet “A” note

trumpetA=Sound[SoundNote["A",1,"Trumpet"]]

trumpetAdata= AudioData[trumpetA][[1]];

AudioPlot[trumpetA]

Oboe “A” note

oboeA=Sound[SoundNote["A",1,"Oboe"]]

oboeAdata= AudioData[oboeA][[1]];

AudioPlot[oboeA]

Basic Plots

showWave[data_, color_, label_] := ListLinePlot[scaled1[data[[Range[2500,3000]]]],LabelStyle->Larger, AxesLabel ->{"Sample", "Amplitude"},AspectRatio->1/2, ImageSize->Scaled[0.7], PlotStyle -> color, PlotLegends -> {label}, PlotRange->All]

sin440Plot = showWave[sin440data, Gray, "Sine 440Hz"]

violinAPlot = showWave[violinAdata, Blue, "Violin"]

Show[{sin440Plot, violinAPlot}]

trumpetAPlot = showWave[trumpetAdata, Orange, "Trumpet"]

Show[{sin440Plot, trumpetAPlot}]

oboeAPlot = showWave[oboeAdata, Purple, "Oboe"]

Show[{sin440Plot, oboeAPlot}]

Show[{violinAPlot, trumpetAPlot}]

Show[{violinAPlot, oboeAPlot}]

Fourier Transforms

Handy functions

doFFT[sound_] := Module[{data, fft, fftamp, max, scaled},
data = AudioData[sound][[1]];
fft = Abs[Fourier[data]];
fftamp = Table[{n, fft[[n+1]]^2}, {n, 200, 1500}];
scaled2[fftamp]
]

show1FFT[sound_, color_] := ListLinePlot[doFFT[sound], PlotRange -> All,PlotStyle -> color, AspectRatio->1/2, LabelStyle->Larger, AxesLabel -> {"f (Hz)", "Amplitude"}, ImageSize->Scaled[0.8]]

showFFTs[soundlist_, colorlist_] := ListLinePlot[Table[doFFT[soundlist[[i]]], {i, 1, Length[soundlist]}], PlotStyle -> colorlist, PlotRange -> All, AspectRatio->1/2, LabelStyle->Larger, PlotLegends -> Automatic, AxesLabel -> {"f (Hz)", "Amplitude"}, ImageSize->Scaled[0.8]]

Applications

sin440FFT = show1FFT[sin440, Gray]

violinAFFT = show1FFT[violinA, Blue]

trumpetAFFT = show1FFT[trumpetA, Orange]

oboeAFFT = show1FFT[oboeA, Purple]

Show[{violinAFFT, trumpetAFFT}]