LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_clip.cpp
Go to the documentation of this file.
1/* #define DEBUGTRACE_ENABLED */
2#include "debugtrace.hpp"
3#include "lasp_clip.h"
4#include "lasp_daqdata.h"
5#include "lasp_daq.h"
6#include <mutex>
7
8using std::cerr;
9using std::endl;
10
11using Lck = std::scoped_lock<std::mutex>;
12using rte = std::runtime_error;
13
16
17 DEBUGTRACE_ENTER;
19 }
20
22
23 DEBUGTRACE_ENTER;
24 Lck lck(_mtx);
25
26 dmat data = d.toFloat();
27
28 const us nchannels = d.nchannels;
29 assert(data.n_cols == nchannels);
30
31 if (nchannels != _clip_time.size()) {
32 DEBUGTRACE_PRINT("Resizing clip indication");
33 _clip_time = vd(nchannels, arma::fill::value(-1));
34 }
35
38 vd maxabs = arma::max(arma::abs(data), 0).as_col() / _max_range;
39
41 arma::uvec clips = maxabs > clip_point;
42
43 for (us i = 0; i < nchannels; i++) {
44 if (clips(i)) {
46 _clip_time(i) = 0;
47 } else if (_clip_time(i) > clip_indication_time) {
49 _clip_time(i) = -1;
50 } else if (_clip_time(i) >= 0) {
52 _clip_time(i) += _dt;
53 }
54 }
55}
56
57arma::uvec ClipHandler::getCurrentValue() const {
58
59 DEBUGTRACE_ENTER;
60 Lck lck(_mtx);
61
62 arma::uvec clips(_clip_time.size(), arma::fill::zeros);
63 clips.elem(arma::find(_clip_time >= 0)).fill(1);
64
65 return {clips};
66}
67
68void ClipHandler::reset(const Daq *daq) {
69
70 DEBUGTRACE_ENTER;
71 Lck lck(_mtx);
72
73 if (daq) {
74
75 const us nchannels = daq->neninchannels();
76 _max_range.resize(nchannels);
77
78 dvec ranges = daq->inputRangeForEnabledChannels();
79 assert(ranges.size() == nchannels);
80 for(us i=0;i<daq->neninchannels();i++) {
81 _max_range[i] = ranges[i];
82 }
83
84 _clip_time.fill(-1);
85
86 const d fs = daq->samplerate();
87 /* DEBUGTRACE_PRINT(fs); */
88 _dt = daq->framesPerBlock() / fs;
89 }
90}
91
93 DEBUGTRACE_ENTER;
94 stopThread();
95}
ClipHandler(SmgrHandle mgr)
Constructs Clipping indicator.
Definition lasp_clip.cpp:14
arma::uvec getCurrentValue() const
Get the current values of the clip handler. Returns a vector of of True's.
Definition lasp_clip.cpp:57
void inCallback(const DaqData &)
Definition lasp_clip.cpp:21
void reset(const Daq *)
Definition lasp_clip.cpp:68
Data coming from / going to DAQ. Non-interleaved format, which means data in buffer is ordered by cha...
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
us framesPerBlock() const
The number of frames that is send in a block of DaqData.
Definition lasp_daq.h:207
dvec inputRangeForEnabledChannels(const bool include_monitor=true) const
Returns the input range for each channel. Maximum allowed absolute value of the signal that can pass ...
Definition lasp_daq.cpp:90
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...
std::runtime_error rte
Definition lasp_daq.cpp:16
std::scoped_lock< std::mutex > Lck
std::vector< double > dvec
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