0
$\begingroup$

I have an Audio that I want to extract the Spectrogram data from such that when I plot it using ArrayPlot it looks the same as the Spectrogram (I reversed it and then 1 minus to make it look exactly the same). It was working when I retrieved it from Spectrogram[audio][[1,1,All,All,1]], but after a Mathematica crash it no longer outputs the same matrix with the same audio. It used to be something like 512x954 but now its 256x5080.

EDIT: Turns out that changing the partition and offset parameters changes the dimensions of the matrix. With default values it reverts to 512x954 but with partition=512 and offset=64 it becomes 256x5080. But when I ArrayPlot the matrix from the default parameters it looks the same as the Spectrogram, this however doesn't happen for other values of the parameters. Is there a way to reconstruct the spectogram with Arrayplot for non-default values of partition and offset'?

CODE:

Export["sound.wav", Play[(2 + Cos[50 t])*Sin[2000*(1 + Round[2 t, 0.1])* t] , {t, 0, 1}]]
audio = Import["sound.wav"]
spec = Spectrogram[audio, ColorFunction -> GrayLevel]
ArrayPlot[1 - Reverse@spec[[1, 1, All, All, 1]]]

produces an ArrayPlot that looks like spec, but changing spec to spec = Spectrogram[audio, 2048, 32, BlackmanHarrisWindow] produces an ArrayPlot that does not look like spec.

$\endgroup$
  • $\begingroup$ So you just want to re-create the output of Spectrogram using ArrayPlot? $\endgroup$ – Alexey Popkov May 1 '17 at 6:29
  • $\begingroup$ Technically, I want the matrix that represents the Spectrogram data and I use an ArrayPlot to verify that they are the same. But short answer is yes. $\endgroup$ – I should change my Username May 1 '17 at 8:05
  • $\begingroup$ Instead of using that unreadable Part expression use SpectrogramArray and Transpose. $\endgroup$ – Sektor May 1 '17 at 11:50
  • $\begingroup$ Could you be a bit more specific. I have tried using SpectrogramArray. I know I can remove half of the array because the amplitudes are mirrored but then I'm stumped. I'm not that familiar with DSP or STFT but I just want the data to play with. Is SpectrogramArray outputted as Freq. x Time or some other dimensions? (I used Re on the whole array just so I could plot it to see what the data looked like. I don't know what the imaginary component is supposed to represent) $\endgroup$ – I should change my Username May 1 '17 at 23:09
  • $\begingroup$ The documentation for Spectrogram shows how to create a spectrogram from SpectrogramArray (see the Properties & Relations section) $\endgroup$ – Simon Woods May 17 '17 at 18:33
1
$\begingroup$

You say you just want to verify it,so you can try SpectrogramArray

spec = Spectrogram[audio, 2048, 32, BlackmanHarrisWindow, 
    ColorFunction -> GrayLevel, Frame -> None, ImageSize -> Medium];
data = SpectrogramArray[audio, 2048, 32, BlackmanHarrisWindow];

ImageResize[ImageCrop@ArrayPlot[1 - Reverse@spec[[1, 1, All, All, 1]], 
        Frame -> None], ImageDimensions@spec]
ImageAdjust@ImageResize[Image[Reverse@Transpose@Abs[data[[All, 1 ;; Dimensions[data][[2]]/2]]]], 
        ImageDimensions@spec]
ImageResize[ImageCrop@spec, ImageDimensions@spec]

enter image description here

exactly the same.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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