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
评论列表
文章目录