@set_fft_params<EOL>def spectrogram(timeseries, method_func, **kwargs):
from ...spectrogram import Spectrogram<EOL>sampling = timeseries.sample_rate.to('<STR_LIT>').value<EOL>nproc = kwargs.pop('<STR_LIT>', <NUM_LIT:1>)<EOL>nfft = kwargs.pop('<STR_LIT>')<EOL>noverlap = kwargs.pop('<STR_LIT>')<EOL>nstride = nfft - noverlap<EOL>if noverlap >= nfft:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>def _psd(series):<EOL><INDENT>"""<STR_LIT>"""<EOL>return method_func(series, nfft=nfft, **kwargs)[<NUM_LIT:1>]<EOL><DEDENT>chunks = []<EOL>x = <NUM_LIT:0><EOL>while x + nfft <= timeseries.size:<EOL><INDENT>y = min(timeseries.size, x + nfft)<EOL>chunks.append((x, y))<EOL>x += nstride<EOL><DEDENT>tschunks = (timeseries.value[i:j] for i, j in chunks)<EOL>psds = mp_utils.multiprocess_with_queues(nproc, _psd, tschunks)<EOL>numtimes = <NUM_LIT:1> + int((timeseries.size - nstride) / nstride)<EOL>numfreqs = int(nfft / <NUM_LIT:2> + <NUM_LIT:1>)<EOL>data = numpy.zeros((numtimes, numfreqs), dtype=timeseries.dtype)<EOL>data[:len(psds)] = psds<EOL>unit = fft_utils.scale_timeseries_unit(<EOL>timeseries.unit, scaling=kwargs.get('<STR_LIT>', '<STR_LIT>'))<EOL>out = Spectrogram(numpy.empty((numtimes, numfreqs),<EOL>dtype=timeseries.dtype),<EOL>copy=False, dt=nstride * timeseries.dt, t0=timeseries.t0,<EOL>f0=<NUM_LIT:0>, df=sampling/nfft, unit=unit,<EOL>name=timeseries.name, channel=timeseries.channel)<EOL>density = nfft // nstride<EOL>weights = get_window('<STR_LIT>', density)<EOL>for i in range(numtimes):<EOL><INDENT>x = max(<NUM_LIT:0>, i+<NUM_LIT:1>-density)<EOL>y = min(i+<NUM_LIT:1>, numtimes-density+<NUM_LIT:1>)<EOL>if x == <NUM_LIT:0>:<EOL><INDENT>wgt = weights[-y:]<EOL><DEDENT>elif y == numtimes - density + <NUM_LIT:1>:<EOL><INDENT>wgt = weights[:y-x]<EOL><DEDENT>else:<EOL><INDENT>wgt = weights<EOL><DEDENT>out.value[i, :] = numpy.average(data[x:y], axis=<NUM_LIT:0>, weights=wgt)<EOL><DEDENT>return out<EOL>
Generate a spectrogram using a method function

    Each time bin of the resulting spectrogram is a PSD estimate using
    a single FFT