2022-09-22 08:18:38 +00:00
|
|
|
/* #define DEBUGTRACE_ENABLED */
|
2022-08-11 12:47:44 +00:00
|
|
|
#include "lasp_avpowerspectra.h"
|
2022-10-05 12:57:39 +00:00
|
|
|
#include "debugtrace.hpp"
|
2022-10-16 19:26:06 +00:00
|
|
|
#include "lasp_mathtypes.h"
|
2024-03-12 20:13:13 +00:00
|
|
|
#include <stdexcept>
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-09-22 08:18:38 +00:00
|
|
|
using rte = std::runtime_error;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
PowerSpectra::PowerSpectra(const us nfft, const Window::WindowType w)
|
2022-08-11 12:47:44 +00:00
|
|
|
: PowerSpectra(Window::create(w, nfft)) {}
|
2022-08-07 19:13:45 +00:00
|
|
|
|
|
|
|
PowerSpectra::PowerSpectra(const vd &window)
|
2022-08-11 12:47:44 +00:00
|
|
|
: nfft(window.size()), _fft(nfft), _window(window) {
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
d win_pow = arma::sum(window % window) / window.size();
|
2022-08-11 12:47:44 +00:00
|
|
|
|
|
|
|
/* Scale fft such that power is easily computed */
|
2022-10-05 12:57:39 +00:00
|
|
|
_scale_fac = 2.0 / (win_pow * (d)nfft * (d)nfft);
|
|
|
|
|
2022-10-05 12:58:38 +00:00
|
|
|
DEBUGTRACE_PRINT(nfft);
|
|
|
|
DEBUGTRACE_PRINT(win_pow);
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
ccube PowerSpectra::compute(const dmat &input) {
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-05 12:57:39 +00:00
|
|
|
/// Run very often. Silence this one.
|
|
|
|
/* DEBUGTRACE_ENTER; */
|
|
|
|
|
2022-08-07 19:13:45 +00:00
|
|
|
dmat input_tmp = input;
|
|
|
|
|
|
|
|
// Multiply each column of the inputs element-wise with the window.
|
|
|
|
input_tmp.each_col() %= _window;
|
|
|
|
|
2022-09-03 18:59:14 +00:00
|
|
|
cmat rfft = _fft.fft(input_tmp);
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
ccube output(rfft.n_rows, input.n_cols, input.n_cols);
|
2022-08-07 19:13:45 +00:00
|
|
|
for (us i = 0; i < input.n_cols; i++) {
|
2022-10-05 12:57:39 +00:00
|
|
|
/// This one can be run in parallel without any problem. Note that it is
|
|
|
|
/// the inner loop that is run in parallel.
|
2022-10-06 19:13:21 +00:00
|
|
|
#pragma omp parallel for
|
2022-08-07 19:13:45 +00:00
|
|
|
for (us j = 0; j < input.n_cols; j++) {
|
2022-09-03 18:59:14 +00:00
|
|
|
output.slice(j).col(i) =
|
|
|
|
_scale_fac * (rfft.col(i) % arma::conj(rfft.col(j)));
|
|
|
|
|
|
|
|
output.slice(j).col(i)(0) /= 2;
|
2022-09-22 08:18:38 +00:00
|
|
|
output.slice(j).col(i)(nfft / 2) /= 2;
|
2022-08-07 19:13:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
AvPowerSpectra::AvPowerSpectra(const us nfft, const Window::WindowType w,
|
2022-08-11 12:47:44 +00:00
|
|
|
const d overlap_percentage,
|
2022-10-06 19:13:21 +00:00
|
|
|
const d time_constant_times_fs)
|
2022-08-11 12:47:44 +00:00
|
|
|
: _ps(nfft, w) {
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-08-11 12:47:44 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
if (overlap_percentage >= 100 || overlap_percentage < 0) {
|
2022-09-22 08:18:38 +00:00
|
|
|
throw rte("Overlap percentage should be >= 0 and < 100");
|
2022-08-07 19:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 12:47:44 +00:00
|
|
|
_overlap_keep = (nfft * overlap_percentage) / 100;
|
2022-10-11 12:50:44 +00:00
|
|
|
DEBUGTRACE_PRINT(_overlap_keep);
|
2022-08-11 12:47:44 +00:00
|
|
|
if (_overlap_keep >= nfft) {
|
2022-09-22 08:18:38 +00:00
|
|
|
throw rte("Overlap is too high. Results in no jump. Please "
|
|
|
|
"choose a smaller overlap percentage or a higher nfft");
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
2022-10-06 19:13:21 +00:00
|
|
|
if (time_constant_times_fs < 0) {
|
2022-08-11 12:47:44 +00:00
|
|
|
_mode = Mode::Averaging;
|
2022-10-06 19:13:21 +00:00
|
|
|
} else if (time_constant_times_fs == 0) {
|
2022-08-11 12:47:44 +00:00
|
|
|
_mode = Mode::Spectrogram;
|
|
|
|
} else {
|
|
|
|
_mode = Mode::Leaking;
|
2022-10-06 19:13:21 +00:00
|
|
|
_alpha = d_exp(-static_cast<d>((nfft - _overlap_keep)/time_constant_times_fs));
|
|
|
|
DEBUGTRACE_PRINT(_alpha);
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-11 12:50:44 +00:00
|
|
|
void AvPowerSpectra::reset() {
|
|
|
|
_timeBuf.reset();
|
|
|
|
_est.reset();
|
2022-10-16 16:39:13 +00:00
|
|
|
n_averages=0;
|
2022-08-07 19:13:45 +00:00
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
}
|
2022-10-05 12:57:39 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
ccube AvPowerSpectra::compute(const dmat &timedata) {
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
DEBUGTRACE_PRINT(timedata.n_rows);
|
|
|
|
DEBUGTRACE_PRINT(_ps.nfft);
|
2022-09-22 08:18:38 +00:00
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
_timeBuf.push(timedata);
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
bool run_once = false;
|
|
|
|
while (_timeBuf.size() >= _ps.nfft) {
|
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
DEBUGTRACE_PRINT((int)_mode);
|
2022-10-16 19:26:06 +00:00
|
|
|
|
|
|
|
dmat samples = _timeBuf.pop(_ps.nfft, _overlap_keep);
|
|
|
|
|
2022-08-11 12:47:44 +00:00
|
|
|
switch (_mode) {
|
2022-10-16 19:26:06 +00:00
|
|
|
case (Mode::Spectrogram):
|
2022-10-11 12:50:44 +00:00
|
|
|
DEBUGTRACE_PRINT("Spectrogram mode");
|
2022-10-16 19:26:06 +00:00
|
|
|
_est = _ps.compute(samples);
|
|
|
|
break;
|
|
|
|
case (Mode::Averaging):
|
2022-10-11 12:50:44 +00:00
|
|
|
DEBUGTRACE_PRINT("Averaging mode");
|
2022-08-11 12:47:44 +00:00
|
|
|
n_averages++;
|
|
|
|
if (n_averages == 1) {
|
2022-10-16 19:26:06 +00:00
|
|
|
_est = _ps.compute(samples);
|
2022-08-11 12:47:44 +00:00
|
|
|
} else {
|
2022-10-11 12:50:44 +00:00
|
|
|
_est = (static_cast<d>(n_averages - 1) / n_averages) * _est +
|
2022-10-16 19:26:06 +00:00
|
|
|
_ps.compute(samples) / n_averages;
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
2022-10-16 19:26:06 +00:00
|
|
|
break;
|
|
|
|
case (Mode::Leaking):
|
2022-10-11 12:50:44 +00:00
|
|
|
DEBUGTRACE_PRINT("Leaking mode");
|
2022-08-11 12:47:44 +00:00
|
|
|
if (arma::size(_est) == arma::size(0, 0, 0)) {
|
2022-10-16 19:26:06 +00:00
|
|
|
_est = _ps.compute(samples);
|
2022-08-11 12:47:44 +00:00
|
|
|
} else {
|
2022-10-16 19:26:06 +00:00
|
|
|
_est = _alpha * _est + (1 - _alpha) * _ps.compute(samples);
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
2022-10-16 19:26:06 +00:00
|
|
|
break;
|
2022-08-11 12:47:44 +00:00
|
|
|
} // end switch mode
|
2022-10-16 19:26:06 +00:00
|
|
|
run_once = true;
|
2022-09-22 08:18:38 +00:00
|
|
|
}
|
2022-10-16 19:26:06 +00:00
|
|
|
|
|
|
|
/// Return a copy of current estimator in case we have done one update.
|
|
|
|
/// Othewise, we return an empty ccube.
|
|
|
|
return run_once ? _est : ccube();
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
2022-10-16 19:26:06 +00:00
|
|
|
ccube AvPowerSpectra::get_est() const {
|
|
|
|
return _est;
|
2022-08-07 19:13:45 +00:00
|
|
|
}
|