9
$\begingroup$

I'm trying to reproduce the result of Spectrogram from SpectrogramArray, but having no luck, any help?

What I've got so far:

x = "some-audio.wav";
colorFunc = (Blend[{White, Orange, Red, Black}, #] &);

(* The spectrogram plotted by the Spectrogram function *)
Spectrogram[
 Audio[x], 400, 160, HammingWindow, ColorFunction -> colorFunc, 
 Frame -> None]

(* Trying to reproduce it *)
spect = SpectrogramArray[Audio[x], 400, 160, HammingWindow];
half = spect[[All, 1 ;; Dimensions[spect][[2]]/2]];
data = Abs[half];
ArrayPlot[Reverse@Transpose[data], AspectRatio -> 1/3, Frame -> None, 
 ColorFunction -> colorFunc]

The result:

enter image description here

enter image description here

$\endgroup$
6
  • 1
    $\begingroup$ Have you tried InverseSpectrogram[ data ] ? $\endgroup$
    – LouisB
    Commented Feb 5, 2020 at 6:13
  • $\begingroup$ Might be a headache, there’s a lot of preprocessing and formatting logic inside Spectrogram $\endgroup$
    – M.R.
    Commented Feb 13, 2020 at 17:47
  • $\begingroup$ @M.R. do you have access to the Spectrogram source code? $\endgroup$ Commented Feb 14, 2020 at 2:42
  • $\begingroup$ @Incömplete Just do this: <<GeneralUtilities`; PrintDefinitions[Spectrogram] $\endgroup$
    – M.R.
    Commented Feb 14, 2020 at 3:07
  • 1
    $\begingroup$ @M.R. The actual definition is not printed by it, it just prints the options of the function, and some other stuff. $\endgroup$ Commented Feb 14, 2020 at 7:05

1 Answer 1

1
$\begingroup$

There are two ways, both showing the same result. I use the sound of an empty glass when hitting a side surface - see here

x=AudioCapture["C:\\Users\\...\\Desktop\\\\glass0.wav", MaxDuration -> 2]
colorFunc = (Blend[{White, Orange, Red, Black}, #] &);

(*The spectrogram plotted by the Spectrogram function*)
Spectrogram[Audio[x], 400, 160, HammingWindow, 
 ColorFunction -> colorFunc, FrameTicks -> None]

(*Trying to reproduce it*)
spect = SpectrogramArray[Audio[x], 400, 160, HammingWindow];
half = spect[[All, 1 ;; Dimensions[spect][[2]]/2]];
data = Abs[half];
ArrayPlot[Reverse@Transpose[data], AspectRatio -> 1/3, 
 ColorFunction -> colorFunc, FrameTicks -> None, 
 ColorFunctionScaling -> False]
(* Second way*)
ListDensityPlot[Transpose[Abs[spect]][[;; 200]], AspectRatio -> 1/3, 
 PlotRange -> All, ColorFunction -> colorFunc, 
 ColorFunctionScaling -> False, FrameTicks -> None]

Figure 1

$\endgroup$
1
  • $\begingroup$ Unfortunately, the two graphs doesn't look the same. I also tried on my data, it doesn't look the same either $\endgroup$ Commented Feb 17, 2020 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.