title: How do I generate a spectrogram of a 1D signal in python?
body: <p>I'm not sure how to do this and I was given an example, <a href="http://matplotlib.org/examples/pylab_examples/specgram_demo.html" rel="noreferrer">spectrogram e.g.</a> but this is in 2D.</p>
  
  <p>I have code here that generates a mix of frequencies and I can pick these out in the fft, how may I
  see these in a spectrogram? I appreciate that the frequencies in my example don't change over time; so does this mean I'll see a straight line across the spectrogram?</p>
  
  <p>my code and the output image:</p>
  
  <pre><code># create a wave with 1Mhz and 0.5Mhz frequencies
  dt = 2e-9
  t = np.arange(0, 10e-6, dt)
  y = np.cos(2 * pi * 1e6 * t) + (np.cos(2 * pi * 2e6 *t) * np.cos(2 * pi * 2e6 * t))
  y *= np.hanning(len(y))
  yy = np.concatenate((y, ([0] * 10 * len(y))))
  
  # FFT of this
  Fs = 1 / dt  # sampling rate, Fs = 500MHz = 1/2ns
  n = len(yy)  # length of the signal
  k = np.arange(n)
  T = n / Fs
  frq = k / T  # two sides frequency range
  frq = frq[range(n / 2)]  # one side frequency range
  Y = fft(yy) / n  # fft computing and normalization
  Y = Y[range(n / 2)] / max(Y[range(n / 2)])
  
  # plotting the data
  subplot(3, 1, 1)
  plot(t * 1e3, y, 'r')
  xlabel('Time (micro seconds)')
  ylabel('Amplitude')
  grid()
  
  # plotting the spectrum
  subplot(3, 1, 2)
  plot(frq[0:600], abs(Y[0:600]), 'k')
  xlabel('Freq (Hz)')
  ylabel('|Y(freq)|')
  grid()
  
  # plotting the specgram
  subplot(3, 1, 3)
  Pxx, freqs, bins, im = specgram(y, NFFT=512, Fs=Fs, noverlap=10)
  show()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/UYQE2.png" alt="output file"></p>