More locks on signal generator.
Some checks failed
Building, testing and releasing LASP if it has a tag / Build-Test-Ubuntu (push) Failing after 1m33s
Building, testing and releasing LASP if it has a tag / Release-Ubuntu (push) Has been skipped

This commit is contained in:
Anne de Jong 2024-03-04 15:49:29 +01:00
parent b61e836f35
commit 26eef040a4
4 changed files with 28 additions and 31 deletions

View File

@ -10,12 +10,12 @@ using rte = std::runtime_error;
inline d level_amp(d level_dB) { return pow(10, level_dB / 20); } inline d level_amp(d level_dB) { return pow(10, level_dB / 20); }
using mutexlock = std::scoped_lock<std::mutex>; using slock = std::scoped_lock<std::recursive_mutex>;
vd Siggen::genSignal(const us nframes) { vd Siggen::genSignal(const us nframes) {
DEBUGTRACE_ENTER; DEBUGTRACE_ENTER;
mutexlock lck(_mtx); slock lck(_mtx);
DEBUGTRACE_PRINT(nframes); DEBUGTRACE_PRINT(nframes);
vd signal(nframes, arma::fill::value(_dc_offset)); vd signal(nframes, arma::fill::value(_dc_offset));
@ -52,7 +52,7 @@ vd Siggen::genSignal(const us nframes) {
return signal; return signal;
} }
void Siggen::setInterruptPeriod(const d newPeriod) { void Siggen::setInterruptPeriod(const d newPeriod) {
mutexlock lck(_mtx); slock lck(_mtx);
if (newPeriod == 0) { if (newPeriod == 0) {
throw rte("Interruption period cannot be 0"); throw rte("Interruption period cannot be 0");
} }
@ -65,7 +65,7 @@ void Siggen::setInterruptPeriod(const d newPeriod) {
void Siggen::setFilter(const std::string &name, void Siggen::setFilter(const std::string &name,
std::shared_ptr<Filter> filter) { std::shared_ptr<Filter> filter) {
DEBUGTRACE_ENTER; DEBUGTRACE_ENTER;
mutexlock lck(_mtx); slock lck(_mtx);
if (filter) { if (filter) {
_filters[name] = filter; _filters[name] = filter;
} else if (_filters.find(name) != _filters.end()) { } else if (_filters.find(name) != _filters.end()) {
@ -74,17 +74,17 @@ void Siggen::setFilter(const std::string &name,
} }
void Siggen::setDCOffset(const d offset) { void Siggen::setDCOffset(const d offset) {
DEBUGTRACE_ENTER; DEBUGTRACE_ENTER;
mutexlock lck(_mtx); slock lck(_mtx);
_dc_offset = offset; _dc_offset = offset;
} }
void Siggen::setLevel(const d level, bool dB) { void Siggen::setLevel(const d level, bool dB) {
DEBUGTRACE_ENTER; DEBUGTRACE_ENTER;
mutexlock lck(_mtx); slock lck(_mtx);
_level_linear = dB ? level_amp(level) : level; _level_linear = dB ? level_amp(level) : level;
} }
void Siggen::reset(const d newFs) { void Siggen::reset(const d newFs) {
DEBUGTRACE_ENTER; DEBUGTRACE_ENTER;
mutexlock lck(_mtx); slock lck(_mtx);
_fs = newFs; _fs = newFs;
for (auto &f : _filters) { for (auto &f : _filters) {
assert(f.second); assert(f.second);

View File

@ -27,7 +27,7 @@ private:
bool _muted = false; bool _muted = false;
protected: protected:
std::mutex _mtx; mutable std::recursive_mutex _mtx;
d _fs = 0; d _fs = 0;
/** /**
* @brief Interuption of period the signal. If set, the signal will be * @brief Interuption of period the signal. If set, the signal will be

View File

@ -7,11 +7,14 @@
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/* #define DEBUGTRACE_ENABLED */ /* #define DEBUGTRACE_ENABLED */
#include "lasp_siggen_impl.h" #include "lasp_siggen_impl.h"
#include "debugtrace.hpp"
#include "lasp_mathtypes.h"
#include <cassert> #include <cassert>
#include "debugtrace.hpp"
#include "lasp_mathtypes.h"
using rte = std::runtime_error; using rte = std::runtime_error;
using slock = std::scoped_lock<std::recursive_mutex>;
DEBUGTRACE_VARIABLES; DEBUGTRACE_VARIABLES;
@ -30,6 +33,7 @@ Sine::Sine(const d freq) : omg(2 * arma::datum::pi * freq) { DEBUGTRACE_ENTER; }
vd Sine::genSignalUnscaled(const us nframes) { vd Sine::genSignalUnscaled(const us nframes) {
/* DEBUGTRACE_ENTER; */ /* DEBUGTRACE_ENTER; */
slock lck(_mtx);
const d pi = arma::datum::pi; const d pi = arma::datum::pi;
vd phase_vec = vd phase_vec =
arma::linspace(phase, phase + omg * (nframes - 1) / _fs, nframes); arma::linspace(phase, phase + omg * (nframes - 1) / _fs, nframes);
@ -41,8 +45,8 @@ vd Sine::genSignalUnscaled(const us nframes) {
} }
vd Periodic::genSignalUnscaled(const us nframes) { vd Periodic::genSignalUnscaled(const us nframes) {
vd res(nframes); vd res(nframes);
slock lck(_mtx);
if (_signal.size() == 0) { if (_signal.size() == 0) {
throw rte("No signal defined while calling"); throw rte("No signal defined while calling");
} }
@ -74,15 +78,15 @@ Sweep::Sweep(const d fl, const d fu, const d Ts, const d Tq, const us flags)
} }
void Sweep::resetImpl() { void Sweep::resetImpl() {
DEBUGTRACE_ENTER; DEBUGTRACE_ENTER;
slock lck(_mtx);
_cur_pos = 0; _cur_pos = 0;
bool forward_sweep = flags & ForwardSweep; bool forward_sweep = flags & ForwardSweep;
bool backward_sweep = flags & BackwardSweep; bool backward_sweep = flags & BackwardSweep;
const d Dt = 1 / _fs; // Deltat const d Dt = 1 / _fs; // Deltat
// Estimate N, the number of samples in the sweep part (non-quiescent part): // Estimate N, the number of samples in the sweep part (non-quiescent part):
const us Ns = (us)(Ts * _fs); const us Ns = (us)(Ts * _fs);
@ -166,7 +170,6 @@ void Sweep::resetImpl() {
/* dVARTRACE(15, phase); */ /* dVARTRACE(15, phase); */
} }
} else if (flags & LogSweep) { } else if (flags & LogSweep) {
DEBUGTRACE_PRINT("Log sweep"); DEBUGTRACE_PRINT("Log sweep");
if (forward_sweep || backward_sweep) { if (forward_sweep || backward_sweep) {
/* Forward or backward sweep */ /* Forward or backward sweep */
@ -194,7 +197,6 @@ void Sweep::resetImpl() {
phase += 2 * number_pi * Dt * fn; phase += 2 * number_pi * Dt * fn;
} }
} else { } else {
DEBUGTRACE_PRINT("Continuous sweep"); DEBUGTRACE_PRINT("Continuous sweep");
const us Nf = Ns / 2; const us Nf = Ns / 2;
@ -249,17 +251,15 @@ void Sweep::resetImpl() {
/* dbgassert(fn >= 0, "BUG"); */ /* dbgassert(fn >= 0, "BUG"); */
phase += 2 * number_pi * Dt * fn; phase += 2 * number_pi * Dt * fn;
while (phase > 2 * number_pi) while (phase > 2 * number_pi) phase -= 2 * number_pi;
phase -= 2 * number_pi;
/* dVARTRACE(17, phase); */ /* dVARTRACE(17, phase); */
} }
/* This should be a very small number!! */ /* This should be a very small number!! */
DEBUGTRACE_PRINT(phase); DEBUGTRACE_PRINT(phase);
} }
} // End of log sweep } // End of log sweep
else { else {
// Either log or linear sweep had to be given as flags. // Either log or linear sweep had to be given as flags.
assert(false); assert(false);
} }
} }

View File

@ -7,7 +7,7 @@
*/ */
/** /**
* \addtogroup siggen * \addtogroup siggen
* @{ * @{
*/ */
@ -18,8 +18,8 @@ class Noise : public Siggen {
d level_linear; d level_linear;
virtual vd genSignalUnscaled(const us nframes) override; virtual vd genSignalUnscaled(const us nframes) override;
void resetImpl() override; void resetImpl() override;
public:
public:
/** /**
* @brief Constructs a noise generator. If no filter is used, the output will * @brief Constructs a noise generator. If no filter is used, the output will
* be white noise. By default, the output will be standard deviation = 1 * be white noise. By default, the output will be standard deviation = 1
@ -28,7 +28,6 @@ class Noise : public Siggen {
*/ */
Noise(); Noise();
~Noise() = default; ~Noise() = default;
}; };
/** /**
@ -37,13 +36,12 @@ class Noise : public Siggen {
class Sine : public Siggen { class Sine : public Siggen {
d phase = 0; d phase = 0;
d omg; d omg;
protected:
void resetImpl() override final { phase=0; } protected:
void resetImpl() override final { phase = 0; }
virtual vd genSignalUnscaled(const us nframes) override final; virtual vd genSignalUnscaled(const us nframes) override final;
public: public:
/** /**
* @brief Create a sine wave generator * @brief Create a sine wave generator
* *
@ -51,7 +49,7 @@ class Sine : public Siggen {
*/ */
Sine(const d freq_Hz); Sine(const d freq_Hz);
~Sine() = default; ~Sine() = default;
void setFreq(const d newFreq) { omg = 2*arma::datum::pi*newFreq; } ; void setFreq(const d newFreq) { omg = 2 * arma::datum::pi * newFreq; };
}; };
/** /**
@ -86,8 +84,7 @@ class Sweep : public Periodic {
void resetImpl() override; void resetImpl() override;
public: public:
static constexpr int ForwardSweep = 1 << 0; static constexpr int ForwardSweep = 1 << 0;
static constexpr int BackwardSweep = 1 << 1; static constexpr int BackwardSweep = 1 << 1;
static constexpr int LinearSweep = 1 << 2; static constexpr int LinearSweep = 1 << 2;
@ -103,11 +100,11 @@ class Sweep : public Periodic {
* avoid temporal aliasing in case of measuring impulse responses. * avoid temporal aliasing in case of measuring impulse responses.
* @param[in] sweep_flags: Sweep period [s] * @param[in] sweep_flags: Sweep period [s]
*/ */
Sweep(const d fl, const d fu, const d Ts, const d Tq, Sweep(const d fl, const d fu, const d Ts, const d Tq, const us sweep_flags);
const us sweep_flags);
~Sweep() = default; ~Sweep() = default;
}; };
/** @} */ /** @} */
/** @} */ /** @} */