Advertisement
Matthen

Viola Spectrum

Aug 11th, 2011
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. (* first import stuff and play it: *)
  2. viola = Import["https://ccrma.stanford.edu/~jos/wav/viola.wav",
  3. "Data"];
  4. violadata = First[viola][[;; 87000]];
  5. sr = 44100;
  6. ListPlay[violadata, SampleRate -> sr]
  7.  
  8.  
  9. (* functions to cut out windows and compute spectra:*)
  10. windowwidth = 0.08;
  11. Window[data_, t_] := Module[{mid, halfwidth, mask, points},
  12. mid = Floor[sr t];
  13. halfwidth = Floor[windowwidth*sr/2];
  14. mask =
  15. Table[Exp[-(i - halfwidth)^2/(0.3 halfwidth^2)], {i, 2 halfwidth}];
  16. points =
  17. Table[data[[i]], {i, mid - halfwidth + 1, mid + halfwidth}];
  18. points* mask
  19. ];
  20. Truncate[l_] := l[[;; Floor[Length[l]/15]]];
  21. (*calculate spectrum at time t *)
  22. Spectrum[data_, t_] := Module[{frame},
  23. frame = Window[data, t];
  24. Truncate[Abs[Fourier[frame]]]
  25. ];
  26.  
  27. (*calculate spectrum*)
  28. spectrumdata =
  29. Table[Spectrum[violadata, t], {t, windowwidth, 1.965 - windowwidth,
  30. 0.004}];
  31.  
  32. (* create spectrogram*)
  33.  
  34. normalize[x_, a_] := Log[x + 1/a];
  35. (*a is just a parameter to tweak how it looks*)
  36. spectrogram[a_] :=
  37. ArrayPlot[
  38. Map[normalize[#, a] &, Transpose[Reverse /@ spectrumdata], {2}],
  39. ColorFunction -> "AvocadoColors", ImageSize -> {500}];
  40. Export["viola.png", spectrogram[20]];
  41.  
  42. (* create animated spectrum*)
  43. f1 = 21.22;
  44. f2 = 24.4;
  45. f3 = 26.9;
  46. Manipulate[
  47. f = If[t < 0.594, f1, If[t < 1.1, f2, f3]];
  48. Show[ListPlot[Spectrum[violadata, t], PlotRange -> {0, 2},
  49. Joined -> True, PlotStyle -> Directive[Thick, Black],
  50. Ticks -> False, Axes -> None]
  51. ,
  52. Graphics[{Darker@ColorData["DarkRainbow"][(f - 21)/7], Thick,
  53. Table[Line[{{f i, 0}, {f i, 2}}], {i, 0, 15, 0.5}]
  54. }]
  55. ]
  56. , {t, windowwidth, 1.965 - windowwidth}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement