From b4dae46be1abb4ce2caf79d4df7de0a50fffc70b Mon Sep 17 00:00:00 2001 From: Frederik Rietdijk Date: Wed, 22 Jun 2016 16:14:47 +0200 Subject: [PATCH] Signal.spectrogram now returns values and Signal.plot_spectrogram plots it. --- acoustics/_signal.pyx | 22 ++++++++++++++++++++++ tests/test__signal.py | 8 +++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/acoustics/_signal.pyx b/acoustics/_signal.pyx index 3465bb08..96524439 100644 --- a/acoustics/_signal.pyx +++ b/acoustics/_signal.pyx @@ -622,6 +622,27 @@ class Signal(numpy.ndarray): def spectrogram(self, **kwargs): + """Spectrogram of signal. + + :returns: Spectrogram. + + See :func:`scipy.signal.spectrogram`. Some of the default values have been changed. + The generated spectrogram consists by default of complex values. + + """ + params = { + 'nfft' : 4096, + 'noverlap' : 128, + 'mode' : 'complex', + } + params.update(kwargs) + + t, s, P = spectrogram(self, fs=self.fs, **params) + + return t, s, P + + + def plot_spectrogram(self, **kwargs): """ Plot spectrogram of the signal. @@ -633,6 +654,7 @@ class Signal(numpy.ndarray): .. note:: This method only works for a single channel. """ + # To do, use :meth:`spectrogram`. params = { 'xlim' : None, 'ylim' : None, diff --git a/tests/test__signal.py b/tests/test__signal.py index 0716279d..30b0adca 100644 --- a/tests/test__signal.py +++ b/tests/test__signal.py @@ -290,16 +290,18 @@ def test_plot_power_spectrum(self, signal): def test_plot_phase_spectrum(self, signal): signal.plot_phase_spectrum() - def test_spectrogram(self, signal): + def test_plot_spectrogram(self, signal): if signal.channels > 1: with pytest.raises(ValueError): - signal.spectrogram() + signal.plot_spectrogram() else: try: - signal.spectrogram() + signal.plot_spectrogram() except NotImplementedError: # easy way to skip mpl 1.3.1 specgram mode issue pass + def spectrogram(self, signal): + signal.spectrogram() def test_pickling(self, signal): import pickle