2022-10-06 19:13:21 +00:00
|
|
|
/* #define DEBUGTRACE_ENABLED */
|
2022-10-04 07:27:27 +00:00
|
|
|
#include "debugtrace.hpp"
|
2022-10-10 17:17:38 +00:00
|
|
|
#include "lasp_ppm.h"
|
2023-06-06 14:05:24 +00:00
|
|
|
#include "lasp_daqdata.h"
|
|
|
|
#include "lasp_daq.h"
|
2022-10-04 07:27:27 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
using Lck = std::scoped_lock<std::mutex>;
|
|
|
|
using rte = std::runtime_error;
|
|
|
|
|
2023-06-07 19:49:07 +00:00
|
|
|
PPMHandler::PPMHandler(SmgrHandle mgr, const d decay_dBps)
|
2023-06-09 08:43:04 +00:00
|
|
|
: ThreadedInDataHandler<PPMHandler>(mgr), _decay_dBps(decay_dBps) {
|
2022-10-10 17:17:38 +00:00
|
|
|
|
2022-10-06 19:13:21 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2023-06-06 14:05:24 +00:00
|
|
|
startThread();
|
2022-10-06 19:13:21 +00:00
|
|
|
}
|
2022-10-10 17:17:38 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
void PPMHandler::inCallback(const DaqData &d) {
|
2022-10-10 17:17:38 +00:00
|
|
|
|
|
|
|
DEBUGTRACE_ENTER;
|
2022-10-16 19:26:06 +00:00
|
|
|
Lck lck(_mtx);
|
2022-10-10 17:17:38 +00:00
|
|
|
|
2022-10-04 07:27:27 +00:00
|
|
|
dmat data = d.toFloat();
|
2022-10-17 17:37:31 +00:00
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
const us nchannels = d.nchannels;
|
|
|
|
assert(data.n_cols == nchannels);
|
2022-10-04 07:27:27 +00:00
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
if (nchannels != _cur_max.size()) {
|
|
|
|
DEBUGTRACE_PRINT("Resizing clip and cur max");
|
|
|
|
_cur_max = vd(nchannels, arma::fill::value(1e-80));
|
|
|
|
_clip_time = vd(nchannels, arma::fill::value(-1));
|
|
|
|
}
|
|
|
|
|
2022-10-17 17:37:31 +00:00
|
|
|
assert(_clip_time.size() == _cur_max.size());
|
2022-10-04 07:27:27 +00:00
|
|
|
|
2022-10-17 17:51:10 +00:00
|
|
|
/// Obtain max abs values for each column, convert this row-vec to a column
|
|
|
|
/// vector, and scale with the max-range for each channel
|
2022-10-17 17:37:31 +00:00
|
|
|
vd maxabs = arma::max(arma::abs(data), 0).as_col() / _max_range;
|
2022-10-04 07:27:27 +00:00
|
|
|
|
2022-10-17 17:37:31 +00:00
|
|
|
/// Find indices for channels that have a clip
|
|
|
|
arma::uvec clips = maxabs > clip_point;
|
2022-10-04 07:27:27 +00:00
|
|
|
|
2022-10-17 17:37:31 +00:00
|
|
|
/// Find channels where the new maximum is higher than the previous one
|
|
|
|
arma::uvec update_max = maxabs > _cur_max;
|
2022-10-04 07:27:27 +00:00
|
|
|
|
|
|
|
for (us i = 0; i < nchannels; i++) {
|
2022-10-17 17:37:31 +00:00
|
|
|
if (clips(i)) {
|
2022-10-04 07:27:27 +00:00
|
|
|
/// Reset clip counter
|
|
|
|
_clip_time(i) = 0;
|
|
|
|
} else if (_clip_time(i) > clip_indication_time) {
|
|
|
|
/// Reset to 'unclipped'
|
|
|
|
_clip_time(i) = -1;
|
2022-10-04 07:48:32 +00:00
|
|
|
} else if (_clip_time(i) >= 0) {
|
2022-10-04 07:27:27 +00:00
|
|
|
/// Add a bit of clip time
|
|
|
|
_clip_time(i) += _dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update_max(i)) {
|
|
|
|
_cur_max(i) = maxabs(i);
|
|
|
|
} else {
|
|
|
|
_cur_max(i) *= _alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::tuple<vd, arma::uvec> PPMHandler::getCurrentValue() const {
|
2022-10-06 19:13:21 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
/* DEBUGTRACE_ENTER; */
|
2022-10-16 19:26:06 +00:00
|
|
|
Lck lck(_mtx);
|
2022-10-04 07:27:27 +00:00
|
|
|
|
2022-10-04 07:48:32 +00:00
|
|
|
arma::uvec clips(_clip_time.size(), arma::fill::zeros);
|
|
|
|
clips.elem(arma::find(_clip_time >= 0)).fill(1);
|
|
|
|
|
|
|
|
return {20 * arma::log10(_cur_max + arma::datum::eps).as_col(), clips};
|
2022-10-04 07:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PPMHandler::reset(const Daq *daq) {
|
2022-10-06 19:13:21 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
Lck lck(_mtx);
|
2022-10-04 07:27:27 +00:00
|
|
|
|
|
|
|
if (daq) {
|
2022-10-06 19:13:21 +00:00
|
|
|
|
2023-06-09 08:43:04 +00:00
|
|
|
DEBUGTRACE_PRINT("New daq found");
|
2022-10-10 17:17:38 +00:00
|
|
|
_cur_max.fill(1e-80);
|
2022-10-16 19:26:06 +00:00
|
|
|
|
2022-10-17 17:37:31 +00:00
|
|
|
const us nchannels = daq->neninchannels();
|
2023-06-09 08:43:04 +00:00
|
|
|
DEBUGTRACE_PRINT(nchannels);
|
2022-10-17 17:37:31 +00:00
|
|
|
_max_range.resize(nchannels);
|
|
|
|
|
|
|
|
dvec ranges = daq->inputRangeForEnabledChannels();
|
|
|
|
assert(ranges.size() == nchannels);
|
|
|
|
for(us i=0;i<daq->neninchannels();i++) {
|
|
|
|
_max_range[i] = ranges[i];
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:17:38 +00:00
|
|
|
_clip_time.fill(-1);
|
2022-10-06 19:13:21 +00:00
|
|
|
|
2022-10-04 07:27:27 +00:00
|
|
|
const d fs = daq->samplerate();
|
2022-10-10 17:17:38 +00:00
|
|
|
/* DEBUGTRACE_PRINT(fs); */
|
2022-10-04 07:27:27 +00:00
|
|
|
_dt = daq->framesPerBlock() / fs;
|
|
|
|
|
2022-10-04 07:48:32 +00:00
|
|
|
_alpha = std::max<d>(d_pow(10, -_dt * _decay_dBps / (20)), 0);
|
2022-10-10 17:17:38 +00:00
|
|
|
/* DEBUGTRACE_PRINT(_alpha); */
|
2022-10-04 07:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PPMHandler::~PPMHandler() {
|
|
|
|
DEBUGTRACE_ENTER;
|
2023-06-06 14:05:24 +00:00
|
|
|
stopThread();
|
2022-10-04 07:27:27 +00:00
|
|
|
}
|