4

The same data lead two different graph.

Plot[E^(-Pi*(t - 5)^2), {t, -10, 10}, PlotRange -> All]

enter image description here

The method to plot the spectrogram use the build-in function:

 Samplerate = 20;data = Table[E^(-Pi*(t - 5)^2), {t, 0, 10, 1/Samplerate}];
Spectrogram[data, 10, 2, SampleRate -> Samplerate]

enter image description here

You can see the center is not 5.but if you use the following code(sorry,I don't know how to add a link here,thanks Sjord C. De Vries). The center is 5.

sndData = data;
sndSampleRate = 20;
min = Min[Abs[Fourier[sndData]]];
partSize = 10;
offset = 2;
spectroGramData = Take[20*Log10[Abs[Fourier[#]]/min],{2, partSize/2 // Floor}] & /@ 
Partition[sndData, partSize, offset]; MatrixPlot[
Reverse[spectroGramData\[Transpose]], ColorFunction -> "Rainbow", 
DataRange ->Round[{{0, Length[sndData]/sndSampleRate}, {sndSampleRate/partSize, 
 sndSampleRate/2}}, 0.1], AspectRatio -> 1/2, ImageSize -> 800, 
Frame -> True, FrameLabel -> {"Frequency (Hz)", "Time (s)", "", ""}, BaseStyle -> {FontFamily -> "Arial", FontWeight -> Bold, 12}]

enter image description here

Do I misunderstand something?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
partida
  • 6,816
  • 22
  • 48

1 Answers1

7

I don't really know if what you calculate in your code is the same as what Spectrogram is calculating, even aside from the log scale you use. Just compare the dimensions of the data being plotted,

Dimensions@spectroGramData
Dimensions@SpectrogramArray[data, 10, 2]
(* {96, 4} *)
(* {101, 10} *)

I can't quite figure out exactly how SpectrogramArray is calculating things, but I can say that if you want to have the center of the spectrogram show up in the right spot, you need to increase your sampling rate:

plotGrid[
 Transpose@{Table[
    Spectrogram[
     Table[E^(-π*(t - 5.0)^2), {t, 0, 10, 1/Samplerate}], 10, 2, 
     SampleRate -> Samplerate], {Samplerate, {1, 2, 5, 10, 20, 30, 50,
       100}}]}
 , 400, 600]

enter image description here

where I'm using Jens's plotGrid function to line the plots up nicely.

Jason B.
  • 68,381
  • 3
  • 139
  • 286