scipy.signal.spectrogram output not as expected
<p>I am using <code>scipy.signal.spectrogram()</code> for analysing a dataset containing values for a current. My input to the function is as follows:</p>

<pre><code>f, t, Sxx = signal.spectrogram(y, fs)
</code></pre>

<p>(for plotting in subplot 3 (from the top) I use <code>plt.pcolormesh(t, f, Sxx)</code>)</p>

<p>Where <code>y</code> is a list of 10002 values, containing the y values for the green graph in the first plot (from the top). <code>fs = 1/T</code> where <code>T = x[1]-x[0]</code> where <code>x</code> is the list of x values (time) belonging to the y-values (current).</p>

<p>My problem is that</p>

<pre><code>t[-1]-t[0] != x[-1]-x[0]
</code></pre>

<p>Meaning: I want to compare plot 3 with the green graph in plot 1, and when these two do not range over the same time-span, the spectrogram becomes useless. You can see from the picture that <code>total_length_x &gt; total_length_t</code></p>

<p><a href="http://i.stack.imgur.com/HCI6k.png" rel="nofollow"><img src="http://i.stack.imgur.com/HCI6k.png" alt="enter image description here"></a></p>

<p>Why is this so? And what can I do to make the spectrum range over the same time-span as my original data?</p>

<p>I wrote some code to explain my comments above about the sizes of the data:</p>

<pre><code>#!/usr/bin/env python

import numpy as np
import scipy.signal
from scipy.signal import spectrogram

WINDOW_LEN = 256
OVERLAP_LEN = WINDOW_LEN / 8
DATA_LEN = 10002
DURATION = 2.0001997
fs =  (DATA_LEN - 1) / DURATION
eps = 1/(fs * 1000.0)

y = np.random.rand(DATA_LEN)
x = np.arange(0, DURATION + 1/fs, 1/fs)

f, t, Sxx = spectrogram(y, fs=fs, nperseg=WINDOW_LEN)

T = np.zeros( int(1 +  np.floor((len(y) - WINDOW_LEN) / (WINDOW_LEN - OVERLAP_LEN))) )

T[0] = x[WINDOW_LEN / 2]
T[1:] = [x[WINDOW_LEN / 2 + (n + 1) * (WINDOW_LEN - OVERLAP_LEN)] for n in np.arange(0,len(T) - 1)]


if all(t - T &lt; eps):
    print (t - T)
    print "All are fine"
    print x[-1] - x[0]
    print t[-1] - t[0]
    print T[-1] - T[0]
else:
    print t
    print T
    print "Wrong estimates"
</code></pre>

python scipy signal-processing fft spectrogram