LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_rtsignalviewer.cpp
Go to the documentation of this file.
1/* #define DEBUGTRACE_ENABLED */
2#include "debugtrace.hpp"
3#include "lasp_daqdata.h"
4#include "lasp_daq.h"
6#include <algorithm>
7#include <mutex>
8
9using std::cerr;
10using std::endl;
11using Lck = std::scoped_lock<std::mutex>;
12using rte = std::runtime_error;
13
14RtSignalViewer::RtSignalViewer(SmgrHandle mgr, const d approx_time_hist,
15 const us resolution, const us channel)
16 : ThreadedInDataHandler(mgr), _approx_time_hist(approx_time_hist),
17 _resolution(resolution), _channel(channel) {
18
19 DEBUGTRACE_ENTER;
20
21 if (approx_time_hist <= 0) {
22 throw rte("Invalid time history. Should be > 0");
23 }
24 if (resolution <= 1) {
25 throw rte("Invalid resolution. Should be > 1");
26 }
28 }
29
31
32 DEBUGTRACE_ENTER;
33
34 Lck lck(_sv_mtx);
35
37 _tb.push(data.toFloat(_channel) / _sens(_channel));
38
39 _dat.col(0) += _nsamples_per_point / _fs;
41 while (_tb.size() > _nsamples_per_point) {
42 // Roll forward time column
43 _dat.col(0) += _nsamples_per_point / _fs;
44 vd newvals = _tb.pop(_nsamples_per_point);
45 d newmax = newvals.max();
46 d newmin = newvals.min();
47
48 vd mincol = _dat.col(1);
49 vd maxcol = _dat.col(2);
50 _dat(arma::span(0, _resolution-2),1) = mincol(arma::span(1, _resolution-1));
51 _dat(arma::span(0, _resolution-2),2) = maxcol(arma::span(1, _resolution-1));
52 _dat(_resolution-1, 1) = newmin;
53 _dat(_resolution-1, 2) = newmax;
54 }
55}
56
60void RtSignalViewer::reset(const Daq *daq) {
61
62 DEBUGTRACE_ENTER;
63 Lck lck(_sv_mtx);
64
65 // Reset time buffer
66 _tb.reset();
67
68 if (daq) {
69 _fs = daq->samplerate();
70
72 _sens.resize(daq->neninchannels());
73 us i = 0;
74 for (const auto &ch : daq->enabledInChannels()) {
75 _sens[i] = ch.sensitivity;
76 i++;
77 }
78
80 _nsamples_per_point =
81 std::max<us>(static_cast<us>(_approx_time_hist / _resolution * _fs), 1);
82
83 const d dt_points = _nsamples_per_point / _fs;
84 const d tend = dt_points * _resolution;
85
86 // Initialize data array
87 _dat.resize(_resolution, 3);
88 _dat.col(0) = arma::linspace(0, tend, _resolution);
89 _dat.col(1).zeros();
90 _dat.col(2).zeros();
91
92 } else {
93 _sens.zeros();
94 _fs = 0;
95 _dat.reset();
96 }
97}
98
100
101 DEBUGTRACE_ENTER;
102 Lck lck(_sv_mtx);
103 return _dat;
104}
std::vector< DaqChannel > enabledInChannels(const bool include_monitor=true) const
Return list of enabled input channels.
Data coming from / going to DAQ. Non-interleaved format, which means data in buffer is ordered by cha...
arma::Mat< d > toFloat() const
Convert samples to floating point values and return a nframes x nchannels array of floats....
Base cass for all DAQ (Data Acquisition) interfaces. A DAQ can be a custom device,...
Definition lasp_daq.h:29
us neninchannels(bool include_monitorchannels=true) const
Returns the number of enabled input channels.
Definition lasp_daq.cpp:99
double samplerate() const
Returns current sample rate.
Definition lasp_daq.cpp:78
void inCallback(const DaqData &)
void reset(const Daq *)
dmat getCurrentValue() const
Returns a 2D array, with:
RtSignalViewer(SmgrHandle mgr, const d approx_time_hist, const us resolution, const us channel)
void startThread()
This method should be called from the derived class' constructor, to start the thread and data is inc...
void stopThread()
This method SHOULD be called from all classes that derive on ThreadedInDataHandler....
A bit of curiously recurring template pattern, to connect the specific handlers and connect the prope...
dmat pop(const us nframes, const us keep=0)
Pop frames from the buffer. Throws a runtime error if more frames are requested than actually stored.
us size() const
Returns current size of stored amount of frames.
void reset()
Reset (empties) the time buffer.
void push(const dmat &mat)
Put samples in the buffer. Number of channels should match other frames, otherwise things go wrong.
std::runtime_error rte
Definition lasp_daq.cpp:16
std::scoped_lock< std::mutex > Lck
std::shared_ptr< StreamMgr > SmgrHandle
arma::Col< d > vd
arma::Mat< d > dmat
std::scoped_lock< std::mutex > lck
size_t us
We often use boolean values.
Definition lasp_types.h:29