def spectrogram(self, start_s=None, duration_s=None, start_sample=None, num_samples=None,<EOL>window_length_s=None, window_length_samples=None, overlap=<NUM_LIT:0.5>, window=('<STR_LIT>', <NUM_LIT>)):
if start_s is not None and start_sample is not None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if duration_s is not None and num_samples is not None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if window_length_s is not None and window_length_samples is not None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if window_length_s is None and window_length_samples is None:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if start_s is None and start_sample is None:<EOL><INDENT>start_sample = <NUM_LIT:0><EOL><DEDENT>elif start_s is not None:<EOL><INDENT>start_sample = int(round(start_s * self.frame_rate))<EOL><DEDENT>if duration_s is None and num_samples is None:<EOL><INDENT>num_samples = len(self.get_array_of_samples()) - int(start_sample)<EOL><DEDENT>elif duration_s is not None:<EOL><INDENT>num_samples = int(round(duration_s * self.frame_rate))<EOL><DEDENT>if window_length_s is not None:<EOL><INDENT>window_length_samples = int(round(window_length_s * self.frame_rate))<EOL><DEDENT>if start_sample + num_samples > len(self.get_array_of_samples()):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>arr = self.to_numpy_array()[start_sample:start_sample+num_samples]<EOL>fs, ts, sxx = signal.spectrogram(arr, self.frame_rate, scaling='<STR_LIT>', nperseg=window_length_samples,<EOL>noverlap=int(round(overlap * window_length_samples)),<EOL>mode='<STR_LIT>', window=window)<EOL>return fs, ts, sxx<EOL>
Does a series of FFTs from `start_s` or `start_sample` for `duration_s` or `num_samples`.
Effectively, transforms a slice of the AudioSegment into the frequency domain across different
time bins.

.. code-block:: python

    # Example for plotting a spectrogram using this function
    import audiosegment
    import matplotlib.pyplot as plt

    #...
    seg = audiosegment.from_file("somebodytalking.wav")
    freqs, times, amplitudes = seg.spectrogram(window_length_s=0.03, overlap=0.5)
    amplitudes = 10 * np.log10(amplitudes + 1e-9)

    # Plot
    plt.pcolormesh(times, freqs, amplitudes)
    plt.xlabel("Time in Seconds")
    plt.ylabel("Frequency in Hz")
    plt.show()

.. image:: images/spectrogram.png

:param start_s: The start time. Starts at the beginning if neither this nor `start_sample` is specified.
:param duration_s: The duration of the spectrogram in seconds. Goes to the end if neither this nor
                   `num_samples` is specified.
:param start_sample: The index of the first sample to use. Starts at the beginning if neither this nor
                     `start_s` is specified.
:param num_samples: The number of samples in the spectrogram. Goes to the end if neither this nor
                    `duration_s` is specified.
:param window_length_s: The length of each FFT in seconds. If the total number of samples in the spectrogram
                        is not a multiple of the window length in samples, the last window will be zero-padded.
:param window_length_samples: The length of each FFT in number of samples. If the total number of samples in the
                        spectrogram is not a multiple of the window length in samples, the last window will
                        be zero-padded.
:param overlap: The fraction of each window to overlap.
:param window: See Scipy's spectrogram-function_.
               This parameter is passed as-is directly into the Scipy spectrogram function. It's documentation is reproduced here:
               Desired window to use. If window is a string or tuple, it is passed to get_window to generate the window values,
               which are DFT-even by default. See get_window for a list of windows and required parameters.
               If window is array_like it will be used directly as the window and its length must be
               `window_length_samples`.
               Defaults to a Tukey window with shape parameter of 0.25.
:returns: Three np.ndarrays: The frequency values in Hz (the y-axis in a spectrogram), the time values starting
          at start time and then increasing by `duration_s` each step (the x-axis in a spectrogram), and
          the dB of each time/frequency bin as a 2D array of shape [len(frequency values), len(duration)].
:raises ValueError: If `start_s` and `start_sample` are both specified, if `duration_s` and `num_samples` are both
                    specified, if the first window's duration plus start time lead to running off the end
                    of the AudioSegment, or if `window_length_s` and `window_length_samples` are either
                    both specified or if they are both not specified.

.. _spectrogram-function: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html