live_utils.py 文件源码

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

项目:Interactivity 作者: treeoftenere 项目源码 文件源码
def fft_continuous(data, n=None, psd=False, log='log', fs=None,
                   window='hamming'):
    """Apply the Fast Fourier Transform on continuous data.

    Apply the Fast Fourier Transform algorithm on continuous data to get
    the spectrum.
    Steps:
        1- Demeaning
        2- Apply hamming window
        3- Compute FFT
        4- Grab lower half

    Args:
        data (numpy.ndarray): shape (`n_samples`, `n_channels`). Data for
            which to get the FFT

    Keyword Args:
        n (int): length of the FFT. If longer than `n_samples`, zero-padding
            is used; if smaller, then the signal is cropped. If None, use
            the same number as the number of samples
        psd (bool): if True, return the Power Spectral Density
        log (string): can be 'log' (log10(x)), 'log+1' (log10(x+1)) or None
        fs (float): Sampling rate of `data`.
        window (string): if 'no_window' do not use a window before
            applying the FFT. Otherwise, use as the window function.
            Currently only supports 'hamming'.

    Returns:
        (numpy.ndarray) Fourier Transform of the original signal
        (numpy.ndarray): array of frequency bins
    """
    if data.ndim == 1:
        data = data.reshape((-1, 1))
    [n_samples, n_channels] = data.shape

    data = data - data.mean(axis=0)
    if window.lower() == 'hamming':
        H = np.hamming(n_samples).reshape((-1, 1))
    elif window.lower() == 'no_window':
        H = np.ones(n_samples).reshape((-1, 1))
    else:
        raise ValueError('window value {} is not supported'.format(window))
    L = np.min([n_samples, n]) if n else n_samples
    Y = np.fft.fft(data * H, n, axis=0) / L
    freq_bins = (fs * np.arange(0, Y.shape[0] / 2 + 1) / Y.shape[0]) \
        if fs is not None else None

    out = Y[0:int(Y.shape[0] / 2) + 1, :]
    out[:, 0] = 2 * out[:, 0]

    if psd:
        out = np.abs(out) ** 2
    if log == 'log':
        out = np.log10(out)
    elif log == 'log+1':
        out = np.log10(out + 1)

    return out, freq_bins
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号