2022-09-03 18:59:14 +00:00
|
|
|
/* #define DEBUGTRACE_ENABLED */
|
2022-08-11 12:47:44 +00:00
|
|
|
#include "lasp_timebuffer.h"
|
2022-10-11 12:50:44 +00:00
|
|
|
#include "debugtrace.hpp"
|
2022-08-11 12:47:44 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <deque>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2022-09-22 08:18:38 +00:00
|
|
|
using rte = std::runtime_error;
|
2022-08-11 12:47:44 +00:00
|
|
|
|
|
|
|
class TimeBufferImp {
|
|
|
|
/**
|
|
|
|
* @brief Storage in a double-ended queue of armadillo row vectors.
|
|
|
|
*/
|
|
|
|
std::deque<arma::rowvec> _storage;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void reset() {
|
2022-09-03 18:59:14 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2022-08-11 12:47:44 +00:00
|
|
|
_storage.clear();
|
|
|
|
}
|
|
|
|
void push(const dmat &mat) {
|
2022-09-22 08:18:38 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2022-10-11 12:50:44 +00:00
|
|
|
#if LASP_DEBUG == 1
|
|
|
|
if (!_storage.empty()) {
|
|
|
|
if (mat.n_cols != _storage.front().n_cols) {
|
2022-09-22 08:18:38 +00:00
|
|
|
throw rte("Invalid number of channels in mat");
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (us i = 0; i < mat.n_rows; i++) {
|
|
|
|
_storage.push_back(mat.row(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
dmat pop(const us nframes, const us keep) {
|
2022-09-03 18:59:14 +00:00
|
|
|
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
if (keep >= nframes) {
|
|
|
|
throw rte("keep should be < nframes");
|
|
|
|
}
|
2022-10-16 19:26:06 +00:00
|
|
|
if (nframes > n_frames()) {
|
|
|
|
throw rte("Requested more than currently in storage");
|
|
|
|
}
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
assert(!_storage.empty());
|
2022-10-11 12:50:44 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
dmat res(nframes, _storage.front().n_cols);
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
us j = 0;
|
|
|
|
for (us i = 0; i < nframes; i++) {
|
|
|
|
if (i + keep < nframes) {
|
|
|
|
// Just pop elements and copy over
|
|
|
|
res.row(i) = _storage.front();
|
|
|
|
_storage.pop_front();
|
2022-10-11 12:50:44 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
} else {
|
2022-08-11 12:47:44 +00:00
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
// Suppose keep == 0, then we never arrive here
|
|
|
|
// Suppose keep == 1, then storage[0] is copyied over.
|
|
|
|
// Suppose keep == 2, then storage[0[ and storage[1] is copyied over.
|
|
|
|
// Etc.
|
|
|
|
res.row(i) = _storage[j];
|
|
|
|
j++;
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 19:26:06 +00:00
|
|
|
return res;
|
2022-08-11 12:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Counts the number of available frames in the queue
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
us n_frames() const { return _storage.size(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
TimeBuffer::TimeBuffer() : _imp(std::make_unique<TimeBufferImp>()) {}
|
2022-10-16 19:26:06 +00:00
|
|
|
dmat TimeBuffer::pop(const us n_rows, const us keep) {
|
2022-08-11 12:47:44 +00:00
|
|
|
return _imp->pop(n_rows, keep);
|
|
|
|
}
|
2022-10-11 12:50:44 +00:00
|
|
|
TimeBuffer::~TimeBuffer() {}
|
|
|
|
void TimeBuffer::reset() { _imp->reset(); }
|
2022-08-11 12:47:44 +00:00
|
|
|
void TimeBuffer::push(const dmat &dat) { _imp->push(dat); }
|
2022-10-16 19:26:06 +00:00
|
|
|
us TimeBuffer::size() const { return _imp->n_frames(); }
|