LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_ppm.cpp
Go to the documentation of this file.
1/* #define DEBUGTRACE_ENABLED */
2#include "debugtrace.hpp"
3#include "lasp_ppm.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
14PPMHandler::PPMHandler(SmgrHandle mgr, const d decay_dBps)
15 : ThreadedInDataHandler<PPMHandler>(mgr), _decay_dBps(decay_dBps) {
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 != _cur_max.size()) {
32 DEBUGTRACE_PRINT("Resizing clip and cur max");
33 _cur_max = vd(nchannels, arma::fill::value(1e-80));
34 _clip_time = vd(nchannels, arma::fill::value(-1));
35 }
36
37 assert(_clip_time.size() == _cur_max.size());
38
41 vd maxabs = arma::max(arma::abs(data), 0).as_col() / _max_range;
42
44 arma::uvec clips = maxabs > clip_point;
45
47 arma::uvec update_max = maxabs > _cur_max;
48
49 for (us i = 0; i < nchannels; i++) {
50 if (clips(i)) {
52 _clip_time(i) = 0;
53 } else if (_clip_time(i) > clip_indication_time) {
55 _clip_time(i) = -1;
56 } else if (_clip_time(i) >= 0) {
58 _clip_time(i) += _dt;
59 }
60
61 if (update_max(i)) {
62 _cur_max(i) = maxabs(i);
63 } else {
64 _cur_max(i) *= _alpha;
65 }
66 }
67}
68
69std::tuple<vd, arma::uvec> PPMHandler::getCurrentValue() const {
70
71 /* DEBUGTRACE_ENTER; */
72 Lck lck(_mtx);
73
74 arma::uvec clips(_clip_time.size(), arma::fill::zeros);
75 clips.elem(arma::find(_clip_time >= 0)).fill(1);
76
77 return {20 * arma::log10(_cur_max + arma::datum::eps).as_col(), clips};
78}
79
80void PPMHandler::reset(const Daq *daq) {
81
82 DEBUGTRACE_ENTER;
83 Lck lck(_mtx);
84
85 if (daq) {
86
87 DEBUGTRACE_PRINT("New daq found");
88 _cur_max.fill(1e-80);
89
90 const us nchannels = daq->neninchannels();
91 DEBUGTRACE_PRINT(nchannels);
92 _max_range.resize(nchannels);
93
94 dvec ranges = daq->inputRangeForEnabledChannels();
95 assert(ranges.size() == nchannels);
96 for(us i=0;i<daq->neninchannels();i++) {
97 _max_range[i] = ranges[i];
98 }
99
100 _clip_time.fill(-1);
101
102 const d fs = daq->samplerate();
103 /* DEBUGTRACE_PRINT(fs); */
104 _dt = daq->framesPerBlock() / fs;
105
106 _alpha = std::max<d>(d_pow(10, -_dt * _decay_dBps / (20)), 0);
107 /* DEBUGTRACE_PRINT(_alpha); */
108 }
109}
110
112 DEBUGTRACE_ENTER;
113 stopThread();
114}
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
Digital Peak Programme Meter (PPM). Let the latest maximum flow away with a certain amount of dB/s....
Definition lasp_ppm.h:25
void inCallback(const DaqData &d)
Implements callback on thread.
Definition lasp_ppm.cpp:21
PPMHandler(SmgrHandle mgr, const d decay_dBps=20.0)
Constructs Peak Programme Meter.
Definition lasp_ppm.cpp:14
void reset(const Daq *)
Definition lasp_ppm.cpp:80
std::tuple< vd, arma::uvec > getCurrentValue() const
Get the current values of the PPM. Returns an array of levels in dB and a vector of True's.
Definition lasp_ppm.cpp:69
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
#define d_pow
arma::Mat< d > dmat
std::scoped_lock< std::mutex > lck
size_t us
We often use boolean values.
Definition lasp_types.h:29