Advertisement
Matthen

Visualising Vowels

Dec 24th, 2012
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. (*
  2. download the wav files from:
  3. http://www.matthen.com/misc/vowels.zip
  4. *)
  5.  
  6. vowelData = (Import[#,
  7. "Data"][[1]] &) /@ {"/Users/matt/Desktop/vowels/a.wav",
  8. "/Users/matt/Desktop/vowels/e.wav",
  9. "/Users/matt/Desktop/vowels/i.wav",
  10. "/Users/matt/Desktop/vowels/o.wav",
  11. "/Users/matt/Desktop/vowels/u.wav"};
  12.  
  13. sr = 44100;
  14.  
  15. (*functions to cut out windows and compute spectra:*)
  16.  
  17. windowwidth = 0.03;
  18. Window[data_, t_] :=
  19. Module[{mid, halfwidth, mask, points}, mid = Floor[sr t];
  20. halfwidth = Floor[windowwidth*sr/2];
  21. mask =
  22. Table[Exp[-(i - halfwidth)^2/(0.3 halfwidth^2)], {i, 2 halfwidth}];
  23. points =
  24. Table[data[[i]], {i, mid - halfwidth + 1, mid + halfwidth}];
  25. mask*points
  26. ];
  27. Truncate[l_] := l[[;; Floor[Length[l]/15]]];
  28. (*calculate spectrum at time t*)
  29.  
  30. Spectrum[data_, t_] := Module[{frame}, frame = Window[data, t];
  31. Truncate[Abs[Fourier[frame]]]];
  32.  
  33. (*create spectrogram*)
  34.  
  35. normalize[x_, a_] := Log[x + 1/a];
  36. (*a is just a parameter to tweak how it looks*)
  37.  
  38. spectrogram[a_] :=
  39. ArrayPlot[
  40. Map[normalize[#, a] &, Transpose[Reverse /@ spectrumdata], {2}],
  41. ColorFunction -> "AvocadoColors", ImageSize -> {500}];
  42.  
  43. Visualise[data_] := Module[{T, spectrogramData, iData, n, m, f},
  44. T = Length[data]/sr;
  45. spectrogramData =
  46. Table[N[Spectrum[data, t]], {t, windowwidth, T - windowwidth,
  47. windowwidth*0.1}];
  48. spectrogramData = spectrogramData/Max[spectrogramData];
  49. spectrogramData = Map[Log[1 + #] &,
  50. Transpose[Reverse /@ spectrogramData]
  51. , {2}];
  52. {n, m} = Dimensions[spectrogramData];
  53. iData =
  54. Flatten[Table[{i/n {Cos[ 2 Pi j/m], Sin[ 2 Pi j/m]},
  55. spectrogramData[[i, j]]}, {i, n}, {j, m}], 1];
  56. f = Interpolation[iData, InterpolationOrder -> 1];
  57. ContourPlot[If[x^2 + y^2 <= 1, f[x, y], 0], {x, -1, 1}, {y, -1, 1},
  58. ContourLines -> False, PlotPoints -> 50,
  59. ColorFunction -> "AvocadoColors", Axes -> None, Frame -> False,
  60. PlotRange -> {0, 0.2}]
  61. ];
  62.  
  63. Visualise/@vowelData
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement