Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- download the wav files from:
- http://www.matthen.com/misc/vowels.zip
- *)
- vowelData = (Import[#,
- "Data"][[1]] &) /@ {"/Users/matt/Desktop/vowels/a.wav",
- "/Users/matt/Desktop/vowels/e.wav",
- "/Users/matt/Desktop/vowels/i.wav",
- "/Users/matt/Desktop/vowels/o.wav",
- "/Users/matt/Desktop/vowels/u.wav"};
- sr = 44100;
- (*functions to cut out windows and compute spectra:*)
- windowwidth = 0.03;
- Window[data_, t_] :=
- Module[{mid, halfwidth, mask, points}, mid = Floor[sr t];
- halfwidth = Floor[windowwidth*sr/2];
- mask =
- Table[Exp[-(i - halfwidth)^2/(0.3 halfwidth^2)], {i, 2 halfwidth}];
- points =
- Table[data[[i]], {i, mid - halfwidth + 1, mid + halfwidth}];
- mask*points
- ];
- Truncate[l_] := l[[;; Floor[Length[l]/15]]];
- (*calculate spectrum at time t*)
- Spectrum[data_, t_] := Module[{frame}, frame = Window[data, t];
- Truncate[Abs[Fourier[frame]]]];
- (*create spectrogram*)
- normalize[x_, a_] := Log[x + 1/a];
- (*a is just a parameter to tweak how it looks*)
- spectrogram[a_] :=
- ArrayPlot[
- Map[normalize[#, a] &, Transpose[Reverse /@ spectrumdata], {2}],
- ColorFunction -> "AvocadoColors", ImageSize -> {500}];
- Visualise[data_] := Module[{T, spectrogramData, iData, n, m, f},
- T = Length[data]/sr;
- spectrogramData =
- Table[N[Spectrum[data, t]], {t, windowwidth, T - windowwidth,
- windowwidth*0.1}];
- spectrogramData = spectrogramData/Max[spectrogramData];
- spectrogramData = Map[Log[1 + #] &,
- Transpose[Reverse /@ spectrogramData]
- , {2}];
- {n, m} = Dimensions[spectrogramData];
- iData =
- Flatten[Table[{i/n {Cos[ 2 Pi j/m], Sin[ 2 Pi j/m]},
- spectrogramData[[i, j]]}, {i, n}, {j, m}], 1];
- f = Interpolation[iData, InterpolationOrder -> 1];
- ContourPlot[If[x^2 + y^2 <= 1, f[x, y], 0], {x, -1, 1}, {y, -1, 1},
- ContourLines -> False, PlotPoints -> 50,
- ColorFunction -> "AvocadoColors", Axes -> None, Frame -> False,
- PlotRange -> {0, 0.2}]
- ];
- Visualise/@vowelData
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement