spectrum.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:pactools 作者: pactools 项目源码 文件源码
def periodogram(self, signals, hold=False):
        """
        Computes the estimation (in dB) for each epoch in a signal

        Parameters
        ----------
        signals : array, shape (n_epochs, n_points)
            Signals from which one computes the power spectrum

        hold : boolean, default = False
            If True, the estimation is appended to the list of previous
            estimations, else, the list is emptied and only the current
            estimation is stored.

        Returns
        -------
        psd : array, shape (n_epochs, n_freq)
            Power spectrum estimated with a Welsh method on each epoch
            n_freq = fft_length // 2 + 1
        """
        fft_length, step = self.check_params()

        signals = np.atleast_2d(signals)
        n_epochs, n_points = signals.shape

        block_length = min(self.block_length, n_points)

        window = self.wfunc(block_length)
        n_epochs, tmax = signals.shape
        n_freq = fft_length // 2 + 1

        psd = np.zeros((n_epochs, n_freq))

        for i, sig in enumerate(signals):
            block = np.arange(block_length)

            # iterate on blocks
            count = 0
            while block[-1] < sig.size:
                psd[i] += np.abs(
                    sp.fft(window * sig[block], fft_length, 0))[:n_freq] ** 2
                count = count + 1
                block = block + step
            if count == 0:
                raise IndexError(
                    'spectrum: first block has %d samples but sig has %d '
                    'samples' % (block[-1] + 1, sig.size))

            # normalize
            if self.donorm:
                scale = 1.0 / (count * (np.sum(window) ** 2))
            else:
                scale = 1.0 / count
            psd[i] *= scale

        if not hold:
            self.psd = []
        self.psd.append(psd)
        return psd
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号