FFT
- stride.utils.fft.magnitude_spectrum(signal, dt, db=True)[source]
Calculate magnitude spectrum of a signal.
Uses an FFT to decompose the signal into frequency components. Only the non-negative frequency components are returned.
This function uses the forward normalisation of the FFT. See numpy FFT documentation for more information: https://numpy.org/devdocs/reference/routines.fft.html#implementation-details
When the input signal is composed only of waves with frequencies that correspond to periods that are integer multiples of dt, then the magnitude terms returned will exactly match the amplitude of the complex wave decomposition. Note that for frequencies other than 0 or the Nyquist frequency, this amplitude is equal to half of the amplitude of a sinusoidal wave because \(\cos(2\pi f) = \frac{1}{2}(e^{i2\pi f} + e^{-i2\pi f})\).
- Parameters:
signal (ndarray) – Signal or array of signals.
dt (float) – Discretisation step for the time axis.
db (bool, optional) – Whether to calculate the spectrum in decibels, defaults to True.
- Returns:
1-dimensional array – Frequencies of the spectrum
ndarray – Magnitude spectrum of the signal or signals.
Examples
The following code extracts the amplitude of a sinusoidal waveform at a given target frequency (which is neither 0 nor the Nyquist frequency). Note that in order for the amplitudes to be exactly correct, the frequency needs exist within the returned freqs (that is, the period corresponds to an integer multiple of dt).
>>> freqs, signal_magnitude = fft.magnitude_spectrum(y, dt, db=False) >>> assert target_frequency in freqs >>> target_freq_idx = np.argmin(np.abs(freqs - target_frequency)) >>> waveform_amplitude = 2 * signal_magnitude[..., target_freq_idx]
- stride.utils.fft.bandwidth(signal, dt, cutoff=-10)[source]
Calculate the bandwidth of a signal at a given dB level.
- Parameters:
signal (ndarray) – Signal or array of signals.
dt (float) – Discretisation step for the time axis.
cutoff (float) – dB level to calculate bandwidth.
- Returns:
float – Min frequency in the BW.
float – Centre frequency in the BW.
float – Max frequency in the BW.