diff --git a/src/lasp/device/lasp_streammgr.cpp b/src/lasp/device/lasp_streammgr.cpp index 1d07dad..e79d9ba 100644 --- a/src/lasp/device/lasp_streammgr.cpp +++ b/src/lasp/device/lasp_streammgr.cpp @@ -11,6 +11,7 @@ using std::cerr; using std::endl; using rte = std::runtime_error; + InDataHandler::InDataHandler(StreamMgr &mgr) : _mgr(mgr) { DEBUGTRACE_ENTER; } void InDataHandler::start() { DEBUGTRACE_ENTER; @@ -59,6 +60,7 @@ void StreamMgr::checkRightThread() const { void StreamMgr::rescanDAQDevices(bool background, std::function callback) { DEBUGTRACE_ENTER; + auto& pool = getPool(); checkRightThread(); if (!_devices_mtx.try_lock()) { @@ -74,7 +76,7 @@ void StreamMgr::rescanDAQDevices(bool background, if (!background) { rescanDAQDevices_impl(callback); } else { - /* pool.push_task(&StreamMgr::rescanDAQDevices_impl, this, callback); */ + pool.push_task(&StreamMgr::rescanDAQDevices_impl, this, callback); } } void StreamMgr::rescanDAQDevices_impl(std::function callback) { diff --git a/src/lasp/device/lasp_streammgr.h b/src/lasp/device/lasp_streammgr.h index cc16cb2..ce155a1 100644 --- a/src/lasp/device/lasp_streammgr.h +++ b/src/lasp/device/lasp_streammgr.h @@ -13,56 +13,56 @@ class StreamMgr; class InDataHandler { - protected: - StreamMgr &_mgr; +protected: + StreamMgr &_mgr; #if LASP_DEBUG == 1 - std::atomic stopCalled{false}; + std::atomic stopCalled{false}; #endif - public: - virtual ~InDataHandler(); +public: + virtual ~InDataHandler(); - /** - * @brief When constructed, the handler is added to the stream manager, which - * will call the handlers's inCallback() until stop() is called. - * - * @param mgr Stream manager. - */ - InDataHandler(StreamMgr &mgr); + /** + * @brief When constructed, the handler is added to the stream manager, which + * will call the handlers's inCallback() until stop() is called. + * + * @param mgr Stream manager. + */ + InDataHandler(StreamMgr &mgr); - /** - * @brief This function is called when input data from a DAQ is available. - * - * @param daqdata Input data from DAQ - * - * @return true if no error. False to stop the stream from running. - */ - virtual bool inCallback(const DaqData &daqdata) = 0; + /** + * @brief This function is called when input data from a DAQ is available. + * + * @param daqdata Input data from DAQ + * + * @return true if no error. False to stop the stream from running. + */ + virtual bool inCallback(const DaqData &daqdata) = 0; - /** - * @brief Reset in-data handler. - * - * @param daq New DAQ configuration of inCallback(). If nullptr is given, - * it means that the stream is stopped. - */ - virtual void reset(const Daq* daq = nullptr) = 0; + /** + * @brief Reset in-data handler. + * + * @param daq New DAQ configuration of inCallback(). If nullptr is given, + * it means that the stream is stopped. + */ + virtual void reset(const Daq *daq = nullptr) = 0; - /** - * @brief This function should be called from the constructor of the - * implementation of InDataHandler. It will start the stream's calling of - * inCallback(). - */ - void start(); + /** + * @brief This function should be called from the constructor of the + * implementation of InDataHandler. It will start the stream's calling of + * inCallback(). + */ + void start(); - /** - * @brief This function should be called from the destructor of derived - * classes, to disable the calls to inCallback(), such that proper - * destruction of the object is allowed and no other threads call methods - * from the object. It removes the inCallback() from the callback list of the - * StreamMgr(). **Failing to call this function results in deadlocks, errors - * like "pure virtual function called", or other**. - */ - void stop(); + /** + * @brief This function should be called from the destructor of derived + * classes, to disable the calls to inCallback(), such that proper + * destruction of the object is allowed and no other threads call methods + * from the object. It removes the inCallback() from the callback list of the + * StreamMgr(). **Failing to call this function results in deadlocks, errors + * like "pure virtual function called", or other**. + */ + void stop(); }; /** @@ -152,8 +152,8 @@ class StreamMgr { * @param callback Function to call when complete. */ void - rescanDAQDevices(bool background = false, - std::function callback = std::function()); + rescanDAQDevices(bool background = false, + std::function callback = std::function()); /** * @brief Start a stream based on given configuration. @@ -173,13 +173,13 @@ class StreamMgr { return getStreamStatus(type).runningOK(); } bool isStreamRunning(const StreamType type) const { - switch(type) { - case(StreamType::input): - return bool(_inputStream); - break; - case(StreamType::output): - return bool(_outputStream); - break; + switch (type) { + case (StreamType::input): + return bool(_inputStream); + break; + case (StreamType::output): + return bool(_outputStream); + break; } return false; } @@ -225,7 +225,7 @@ class StreamMgr { */ void setSiggen(std::shared_ptr s); - private: +private: bool inCallback(const DaqData &data); bool outCallback(DaqData &data); diff --git a/src/lasp/lasp_slm.py b/src/lasp/lasp_slm.py index 3fae3b9..f00af31 100644 --- a/src/lasp/lasp_slm.py +++ b/src/lasp/lasp_slm.py @@ -125,7 +125,7 @@ class SLM: if prefilter is not None: self.slm = cppSLM.fromBiquads(fs, level_ref_value, dsfac, tw[0], - prefilter, sos) + prefilter.flatten(), sos) else: self.slm = cppSLM.fromBiquads(fs, level_ref_value, dsfac, tw[0], @@ -145,11 +145,7 @@ class SLM: data: one-dimensional input data """ - assert data.ndim == 2 - assert data.shape[1] == 1, "invalid number of channels, should be 1" - - if data.shape[0] == 0: - return {} + assert data.ndim == 1 levels = self.slm.run(data) @@ -197,10 +193,10 @@ class SLM: logging.debug(output['mid']) if self.include_overall and self.fbdesigner is not None: - output['overall'] = dat[-1,0] - output['y'] = np.asarray(dat[:-1,0]) + output['overall'] = dat[-1] + output['y'] = np.asarray(dat[:-1]) else: - output['y'] = np.asarray(dat[:,0]) + output['y'] = np.asarray(dat[:]) return output def Leq(self): diff --git a/src/lasp/pybind11/lasp_dsp_pybind.cpp b/src/lasp/pybind11/lasp_dsp_pybind.cpp index f301a3e..85fd8ef 100644 --- a/src/lasp/pybind11/lasp_dsp_pybind.cpp +++ b/src/lasp/pybind11/lasp_dsp_pybind.cpp @@ -1,15 +1,16 @@ -#include +#include "arma_npy.h" #include "lasp_avpowerspectra.h" #include "lasp_biquadbank.h" #include "lasp_fft.h" -#include "lasp_streammgr.h" #include "lasp_filter.h" #include "lasp_slm.h" +#include "lasp_streammgr.h" #include "lasp_window.h" #include #include using std::cerr; +using std::endl; namespace py = pybind11; /** @@ -23,6 +24,7 @@ namespace py = pybind11; * * @param m The Python module to add classes and methods to */ + void init_dsp(py::module &m) { py::class_ fft(m, "Fft"); @@ -53,22 +55,30 @@ void init_dsp(py::module &m) { py::class_> sbq(m, "SeriesBiquad", filter); sbq.def(py::init()); - sbq.def("filter", [](SeriesBiquad &s, const vd &input) { - vd res = input; + sbq.def("filter", [](SeriesBiquad &s, dpyarray input) { + vd res = NpyToCol(input); s.filter(res); - return res; + return ColToNpy(res); }); /// BiquadBank py::class_> bqb(m, "BiquadBank"); bqb.def(py::init()); - bqb.def("setGains", &BiquadBank::setGains); - bqb.def("filter", &BiquadBank::filter); + bqb.def("setGains", [](BiquadBank &b, dpyarray gains) { + b.setGains(NpyToCol(gains)); + }); + bqb.def("filter", [](BiquadBank &b, dpyarray input) { + vd inout = NpyToCol(input); + b.filter(inout); + return ColToNpy(inout); + }); /// PowerSpectra py::class_ ps(m, "PowerSpectra"); ps.def(py::init()); - ps.def("compute", &PowerSpectra::compute); + ps.def("compute", [](PowerSpectra &ps, dpyarray input) { + return CubeToNpy(ps.compute(NpyToMat(input))); + }); /// AvPowerSpectra py::class_ aps(m, "AvPowerSpectra"); @@ -77,29 +87,37 @@ void init_dsp(py::module &m) { py::arg("windowType") = Window::WindowType::Hann, py::arg("overlap_percentage") = 50.0, py::arg("time_constant") = -1); - aps.def("compute", [](AvPowerSpectra &aps, const dmat &timedata) { - std::optional res = aps.compute(timedata); - return res.value_or(arma::cx_cube(0, 0, 0)); + aps.def("compute", [](AvPowerSpectra &aps, dpyarray timedata) { + std::optional res = + aps.compute(NpyToMat(timedata)); + + return CubeToNpy(res.value_or(cube(0, 0, 0))); }); py::class_ slm(m, "cppSLM"); - slm.def_static( - "fromBiquads", - py::overload_cast( - &SLM::fromBiquads)); - slm.def_static( - "fromBiquads", - py::overload_cast(&SLM::fromBiquads)); - slm.def("run", &SLM::run); - slm.def_readonly("Pm", &SLM::Pm); - slm.def_readonly("Pmax", &SLM::Pmax); - slm.def_readonly("Ppeak", &SLM::Ppeak); - slm.def("Lpeak", &SLM::Lpeak); - slm.def("Leq", &SLM::Leq); - slm.def("Lmax", &SLM::Lmax); - slm.def_static("suggestedDownSamplingFac", &SLM::suggestedDownSamplingFac); + slm.def_static("fromBiquads", [](const d fs, const d Lref, const us ds, + const d tau, dpyarray bandpass) { + return SLM::fromBiquads(fs, Lref, ds, tau, NpyToMat(bandpass)); + }); + slm.def_static("fromBiquads", [](const d fs, const d Lref, const us ds, + const d tau, dpyarray prefilter, + py::array_t bandpass) { + return SLM::fromBiquads(fs, Lref, ds, tau, NpyToCol(prefilter), + NpyToMat(bandpass)); + }); + + slm.def("run", [](SLM &slm, dpyarray in) { + return MatToNpy(slm.run(NpyToCol(in))); + }); + slm.def("Pm", [](const SLM &slm) { return ColToNpy(slm.Pm); }); + slm.def("Pmax", [](const SLM &slm) { return ColToNpy(slm.Pmax); }); + slm.def("Ppeak", [](const SLM &slm) { return ColToNpy(slm.Ppeak); }); + + slm.def("Leq", [](const SLM &slm) { return ColToNpy(slm.Leq()); }); + slm.def("Lmax", [](const SLM &slm) { return ColToNpy(slm.Lmax()); }); + slm.def("Lpeak", [](const SLM &slm) { return ColToNpy(slm.Lpeak()); }); + slm.def_static("suggestedDownSamplingFac", &SLM::suggestedDownSamplingFac); } /** @} */ diff --git a/src/lasp/pybind11/lasp_pyindatahandler.cpp b/src/lasp/pybind11/lasp_pyindatahandler.cpp index 046620d..e607143 100644 --- a/src/lasp/pybind11/lasp_pyindatahandler.cpp +++ b/src/lasp/pybind11/lasp_pyindatahandler.cpp @@ -1,6 +1,6 @@ -#include #include #define DEBUGTRACE_ENABLED +#include "arma_npy.h" #include "debugtrace.hpp" #include "lasp_ppm.h" #include "lasp_rtaps.h" @@ -25,7 +25,8 @@ namespace py = pybind11; * * @return Numpy array */ -template py::array_t getPyArrayNoCpy(const DaqData &d) { +template +py::array_t getPyArrayNoCpy(const DaqData &d) { // https://github.com/pybind/pybind11/issues/323 // // When a valid object is passed as 'base', it tells pybind not to take @@ -57,7 +58,8 @@ template py::array_t getPyArrayNoCpy(const DaqDa ); } -template py::array_t dmat_to_ndarray(const DaqData &d) { +template +py::array_t dmat_to_ndarray(const DaqData &d) { // https://github.com/pybind/pybind11/issues/323 // // When a valid object is passed as 'base', it tells pybind not to take @@ -183,10 +185,11 @@ void init_datahandler(py::module &m) { ppm.def(py::init()); ppm.def("getCurrentValue", [](const PPMHandler &ppm) { + std::tuple tp = ppm.getCurrentValue(); - return py::make_tuple(carma::col_to_arr(std::get<0>(tp)), - carma::col_to_arr(std::get<1>(tp))); + return py::make_tuple(ColToNpy(std::get<0>(tp)), + ColToNpy(std::get<1>(tp))); }); /// Real time Aps @@ -214,8 +217,8 @@ void init_datahandler(py::module &m) { rtaps.def("getCurrentValue", [](RtAps &rt) { std::unique_ptr val = rt.getCurrentValue(); if (val) { - return carma::cube_to_arr(std::move(*val)); + return CubeToNpy (*val); } - return carma::cube_to_arr(cube(1, 0, 0)); + return CubeToNpy(cube(1,0,0)); }); } diff --git a/src/lasp/pybind11/lasp_siggen.cpp b/src/lasp/pybind11/lasp_siggen.cpp index 5b1eaab..ecad69f 100644 --- a/src/lasp/pybind11/lasp_siggen.cpp +++ b/src/lasp/pybind11/lasp_siggen.cpp @@ -1,4 +1,3 @@ -#include #include "lasp_avpowerspectra.h" #include "lasp_biquadbank.h" #include "lasp_fft.h" @@ -33,7 +32,7 @@ void init_siggen(py::module &m) { siggen.def("setMute", &Siggen::setMute); siggen.def("setLevel", &Siggen::setLevel); siggen.def("setLevel", &Siggen::setLevel, py::arg("newLevel"), py::arg("dB") = true); - siggen.def("genSignal", &Siggen::genSignal); + /* siggen.def("genSignal", &Siggen::genSignal); */ siggen.def("setFilter", &Siggen::setFilter); py::class_> noise(m, "Noise", siggen);