2022-08-07 19:13:45 +00:00
|
|
|
#pragma once
|
|
|
|
#include "lasp_fft.h"
|
|
|
|
#include "lasp_mathtypes.h"
|
2022-08-11 12:47:44 +00:00
|
|
|
#include "lasp_timebuffer.h"
|
2022-08-07 19:13:45 +00:00
|
|
|
#include "lasp_window.h"
|
2022-08-11 12:47:44 +00:00
|
|
|
#include <memory>
|
2022-08-07 19:13:45 +00:00
|
|
|
#include <optional>
|
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
/** \defgroup dsp Digital Signal Processing utilities
|
2022-10-01 17:59:35 +00:00
|
|
|
* These are classes and functions used for processing raw signal data, to
|
|
|
|
* obtain statistics.
|
|
|
|
*
|
|
|
|
*
|
2022-09-03 18:59:14 +00:00
|
|
|
* @{
|
2022-09-22 08:18:38 +00:00
|
|
|
*/
|
|
|
|
|
2022-10-06 19:13:21 +00:00
|
|
|
/**
|
2022-09-22 08:18:38 +00:00
|
|
|
* @brief Computes single-sided cross-power spectra for a group of channels.
|
|
|
|
* Only a single block of length fft, no averaging. This class should normally
|
|
|
|
* not be used. Please refer to AvPowerSpectra instead.
|
2022-08-07 19:13:45 +00:00
|
|
|
*/
|
|
|
|
class PowerSpectra {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief The FFT length
|
|
|
|
*/
|
|
|
|
us nfft;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* @brief Instance to compute actual FFT's
|
|
|
|
*/
|
|
|
|
Fft _fft;
|
|
|
|
|
|
|
|
vd _window;
|
2022-10-05 12:57:39 +00:00
|
|
|
d _scale_fac;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Initalize power spectra computer
|
|
|
|
*
|
|
|
|
* @param nfft The fft length
|
|
|
|
* @param w The window type
|
|
|
|
*/
|
|
|
|
PowerSpectra(const us nfft, const Window::WindowType w);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initalize power spectra computer
|
|
|
|
*
|
|
|
|
* @param window Uses the window length to compute fft length, and uses the
|
|
|
|
* window shape as the actual window.
|
|
|
|
*/
|
|
|
|
PowerSpectra(const vd &window);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Computes the spectra. Data is normalized by the (spectral) power of
|
|
|
|
* the window, to compensate for the effect of the window.
|
|
|
|
*
|
|
|
|
* @param input Input data, first axis is assumed the sample, second axis the
|
|
|
|
* channel. Input first dimension should be equal to nfft, otherwise a
|
|
|
|
* runtime error is thrown.
|
|
|
|
*
|
|
|
|
* @return Cross-power-spectra. Cubic array with size indices C_fij, where
|
|
|
|
* f is the frequency index, i is the row and j is the column. An element
|
|
|
|
* can be accessed by: C(f, i, j). Storage is such that frequency components
|
|
|
|
* are contiguous.
|
|
|
|
*/
|
2022-10-11 12:50:44 +00:00
|
|
|
ccube compute(const dmat &input);
|
2022-08-07 19:13:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Estimate cross-power spectra using Welch' method of spectral
|
|
|
|
* estimation. The exact amount of overlap in Welch' method is rounded up to a
|
|
|
|
* certain amount of samples.
|
|
|
|
*/
|
|
|
|
class AvPowerSpectra {
|
|
|
|
|
|
|
|
enum class Mode {
|
|
|
|
Averaging = 0, // Averaging all time date
|
|
|
|
Leaking = 1, // Exponential weighting of an "instantaneous cps"
|
|
|
|
Spectrogram = 2 // Instantenous spectrum, no averaging
|
|
|
|
};
|
|
|
|
|
|
|
|
Mode _mode;
|
2022-08-11 12:47:44 +00:00
|
|
|
d _alpha; // Only valid in case of 'Leaking'
|
|
|
|
us n_averages = 0; // Only valid in case of 'Averaging'
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
PowerSpectra _ps;
|
|
|
|
/**
|
|
|
|
* @brief Current estimate of cross-spectral density
|
|
|
|
*/
|
2022-10-11 12:50:44 +00:00
|
|
|
ccube _est;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Buffer of storage of time data.
|
|
|
|
*/
|
2022-08-11 12:47:44 +00:00
|
|
|
TimeBuffer _timeBuf;
|
2022-08-07 19:13:45 +00:00
|
|
|
/**
|
2022-08-11 12:47:44 +00:00
|
|
|
* @brief The amount of samples to keep in the overlap
|
2022-08-07 19:13:45 +00:00
|
|
|
*/
|
2022-08-11 12:47:44 +00:00
|
|
|
us _overlap_keep;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Initalize averaged power spectra computer. If a time constant is
|
|
|
|
* given > 0, it is used in a kind of exponential weighting.
|
|
|
|
*
|
|
|
|
* @param nfft The fft length
|
|
|
|
* @param w The window type.
|
|
|
|
* @param overlap_percentage A number 0 < overlap_percentage <= 100. It
|
|
|
|
* determines the amount of overlap used in Welch' method. A typical value is
|
|
|
|
* 50 %, i.e. 50.
|
2022-10-06 19:13:21 +00:00
|
|
|
* @param fs_tau Value should either be < 0, indicating that the
|
2022-08-07 19:13:45 +00:00
|
|
|
* estimate is averages over all time data.
|
|
|
|
* For a value = 0 the instantaneous power spectrum is returned, which can be
|
|
|
|
* interpreted as the spectrogram. For a value > 0 a exponential forgetting is
|
|
|
|
* used, where the value is used as the time constant such that the decay
|
2022-10-06 19:13:21 +00:00
|
|
|
* follows approximately the trend exp(-n/fs_tau), where n is the
|
2022-08-07 19:13:45 +00:00
|
|
|
* sample number in the power spectra. To choose 'fast' time weighting, set
|
|
|
|
* time_constant to the value of fs*0.125, where fs denotes the sampling
|
|
|
|
* frequency.
|
|
|
|
**/
|
|
|
|
AvPowerSpectra(const us nfft = 2048,
|
|
|
|
const Window::WindowType w = Window::WindowType::Hann,
|
|
|
|
const d overlap_percentage = 50.,
|
2022-10-06 19:13:21 +00:00
|
|
|
const d fs_tau = -1);
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
AvPowerSpectra(const AvPowerSpectra &) = delete;
|
|
|
|
AvPowerSpectra &operator=(const AvPowerSpectra &) = delete;
|
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
/**
|
|
|
|
* @brief Reset to empty state. Clears the time buffer and sets estimator to
|
|
|
|
* empty.
|
|
|
|
*/
|
|
|
|
void reset();
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-06 19:13:21 +00:00
|
|
|
* @brief Compute an update of the power spectra based on given time data.
|
2022-08-07 19:13:45 +00:00
|
|
|
* Note that the number of channels is determined from the first time this
|
|
|
|
* function is called. If a later call has an incompatible number of
|
|
|
|
* channels, a runtime error is thrown.
|
|
|
|
*
|
|
|
|
* @param timedata
|
|
|
|
*
|
2022-10-16 19:26:06 +00:00
|
|
|
* @return a copy of the latest estimate of the power spectra. an
|
2022-10-06 19:13:21 +00:00
|
|
|
* update is only given if the amount of new time data is enough to compute a
|
2022-10-16 19:26:06 +00:00
|
|
|
* new estimate. if no new estimate is available, it returns an empty ccube.
|
|
|
|
* Note that the latest available estimate can be obtained using get_est().
|
|
|
|
* */
|
|
|
|
ccube compute(const dmat &timedata);
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the latest estimate of cps (cross-power spectra.
|
|
|
|
*
|
2022-10-16 19:26:06 +00:00
|
|
|
* @return a copy of the latest estimate of the power spectra. an
|
|
|
|
* update is only given if the amount of new time data is enough to compute a
|
|
|
|
* new estimate. If no estimate is available, it returns an empty ccube.
|
|
|
|
* */
|
|
|
|
ccube get_est() const;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The overlap is rounded to a certain amount of time samples. This
|
|
|
|
* function returns that value.
|
|
|
|
*
|
|
|
|
* @return The amount of samples in overlapping.
|
|
|
|
*/
|
2022-08-11 12:47:44 +00:00
|
|
|
us exactOverlapSamples() const { return _ps.nfft - _overlap_keep; }
|
2022-08-07 19:13:45 +00:00
|
|
|
};
|
2022-09-03 18:59:14 +00:00
|
|
|
/** @} */
|