0

Consider following example:

import numpy as np
import scipy as scp
import matplotlib.pyplot as plt

#make signal
deltat = 1e-4
t = np.linspace(0,300000,300001) * deltat
y = np.exp(-t/10)*np.sin(np.pi * (t/4)**2)

#plot signal
plt.figure(1)
plt.clf()
plt.plot(t, y)
plt.show()

#calculate spectrogram
f, t, Sxx = scp.signal.spectrogram(y, 1/deltat)

#plot spectrogram
plt.figure(2)
plt.clf()
plt.pcolormesh(t, f, Sxx)
plt.ylim([0, 100])
plt.show()

A sine with increasing frequency and decreasing amplitude is created and its spectrogram is plotted. Note that the sine is oversampled (it represents a measurement at fixed time step deltat).

enter image description here

The spectrogram should be an increasing line (increasing frequency) of decreasing color (decreasing amplitude). How can I obtain the correct spectrogram?

  • Trying to include the nperseg and noverlap keywords in the spectrogram function does only give me other wrong spectrograms.
  • Should I use an alternative Python function instead of spectrogram? Update: I have also tried using librosa.stft without success.
  • Is the problem related to the oversampling?
Karlo
  • 1,630
  • 4
  • 33
  • 57
  • 1
    Take a look at the examples in the docstring of [`scipy.signal.chirp`](https://scipy.github.io/devdocs/reference/generated/scipy.signal.chirp.html) (scroll down to the bottom to see the examples that use `spectrogram`). Also, my [chapter on linear filters with SciPy](https://warrenweckesser.github.io/papers/weckesser-scipy-linear-filters.pdf) (for the PyData book that never was) has some notes on using `spectrogram` starting on page 4. – Warren Weckesser Apr 07 '22 at 17:25
  • 1
    Another example that might be useful: https://stackoverflow.com/questions/56456419/reproduce-sox-spectrogram-in-scipy – Warren Weckesser Apr 11 '22 at 01:56

0 Answers0