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$
6
  • $\begingroup$ So you just want to re-create the output of Spectrogram using ArrayPlot? $\endgroup$ Commented May 1, 2017 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$ Commented May 1, 2017 at 8:05
  • $\begingroup$ Instead of using that unreadable Part expression use SpectrogramArray and Transpose. $\endgroup$ Commented May 1, 2017 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$ Commented May 1, 2017 at 23:09
  • $\begingroup$ The documentation for Spectrogram shows how to create a spectrogram from SpectrogramArray (see the Properties & Relations section) $\endgroup$ Commented May 17, 2017 at 18:33

1 Answer 1

2
$\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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.