Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* first import stuff and play it: *)
- viola = Import["https://ccrma.stanford.edu/~jos/wav/viola.wav",
- "Data"];
- violadata = First[viola][[;; 87000]];
- sr = 44100;
- ListPlay[violadata, SampleRate -> sr]
- (* functions to cut out windows and compute spectra:*)
- windowwidth = 0.08;
- 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}];
- points* mask
- ];
- 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]]]
- ];
- (*calculate spectrum*)
- spectrumdata =
- Table[Spectrum[violadata, t], {t, windowwidth, 1.965 - windowwidth,
- 0.004}];
- (* 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}];
- Export["viola.png", spectrogram[20]];
- (* create animated spectrum*)
- f1 = 21.22;
- f2 = 24.4;
- f3 = 26.9;
- Manipulate[
- f = If[t < 0.594, f1, If[t < 1.1, f2, f3]];
- Show[ListPlot[Spectrum[violadata, t], PlotRange -> {0, 2},
- Joined -> True, PlotStyle -> Directive[Thick, Black],
- Ticks -> False, Axes -> None]
- ,
- Graphics[{Darker@ColorData["DarkRainbow"][(f - 21)/7], Thick,
- Table[Line[{{f i, 0}, {f i, 2}}], {i, 0, 15, 0.5}]
- }]
- ]
- , {t, windowwidth, 1.965 - windowwidth}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement