Added possibility to get the periodic sequence to Python using the getSequence() method on class Periodic. Added a check that at least one of LogSweep or LinSweep should be activated.

This commit is contained in:
Anne de Jong 2022-10-21 23:12:47 +02:00
parent 30992add71
commit 6eaf5db691
3 changed files with 48 additions and 35 deletions

View File

@ -6,10 +6,10 @@
// Signal generators implementation // Signal generators implementation
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
/* #define DEBUGTRACE_ENABLED */ /* #define DEBUGTRACE_ENABLED */
#include "debugtrace.hpp"
#include "lasp_siggen_impl.h" #include "lasp_siggen_impl.h"
#include "debugtrace.hpp" #include "debugtrace.hpp"
#include "lasp_mathtypes.h" #include "lasp_mathtypes.h"
#include <cassert>
using rte = std::runtime_error; using rte = std::runtime_error;
@ -26,13 +26,13 @@ vd Noise::genSignalUnscaled(us nframes) {
} }
void Noise::resetImpl() {} void Noise::resetImpl() {}
Sine::Sine(const d freq) : omg(2 * arma::datum::pi * freq) { DEBUGTRACE_ENTER;} 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; */
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);
phase += omg * nframes / _fs; phase += omg * nframes / _fs;
while (phase > 2 * arma::datum::pi) { while (phase > 2 * arma::datum::pi) {
phase -= 2 * pi; phase -= 2 * pi;
@ -43,10 +43,10 @@ vd Sine::genSignalUnscaled(const us nframes) {
vd Periodic::genSignalUnscaled(const us nframes) { vd Periodic::genSignalUnscaled(const us nframes) {
vd res(nframes); vd res(nframes);
if(_signal.size() == 0) { if (_signal.size() == 0) {
throw rte("No signal defined while calling"); throw rte("No signal defined while calling");
} }
for(us i=0;i<nframes;i++) { for (us i = 0; i < nframes; i++) {
res(i) = _signal[_cur_pos]; res(i) = _signal[_cur_pos];
_cur_pos++; _cur_pos++;
_cur_pos %= _signal.size(); _cur_pos %= _signal.size();
@ -55,20 +55,23 @@ vd Periodic::genSignalUnscaled(const us nframes) {
} }
Sweep::Sweep(const d fl, const d fu, const d Ts, const d Tq, const us flags) Sweep::Sweep(const d fl, const d fu, const d Ts, const d Tq, const us flags)
: fl_(fl), fu_(fu), Ts(Ts), Tq(Tq), flags(flags) { : fl_(fl), fu_(fu), Ts(Ts), Tq(Tq), flags(flags) {
if (fl <= 0 || fu < fl || Ts <= 0) { if (fl <= 0 || fu < fl || Ts <= 0) {
throw std::runtime_error("Invalid sweep parameters"); throw rte("Invalid sweep parameters");
}
if ((flags & ForwardSweep) && (flags & BackwardSweep)) {
throw std::runtime_error(
"Both forward and backward sweep flag set. Please only set either one "
"or none for a continuous sweep");
}
if ((flags & LinearSweep) && (flags & LogSweep)) {
throw std::runtime_error(
"Both logsweep and linear sweep flag set. Please only set either one.");
}
} }
if ((flags & ForwardSweep) && (flags & BackwardSweep)) {
throw rte(
"Both forward and backward sweep flag set. Please only set either one "
"or none for a continuous sweep");
}
if ((flags & LinearSweep) && (flags & LogSweep)) {
throw rte(
"Both logsweep and linear sweep flag set. Please only set either one.");
}
if (!((flags & LinearSweep) || (flags & LogSweep))) {
throw rte("Either LinearSweep or LogSweep should be given as flag");
}
}
void Sweep::resetImpl() { void Sweep::resetImpl() {
@ -133,7 +136,7 @@ void Sweep::resetImpl() {
d phih = 2 * number_pi * Dt * (fl * Nf + 0.5 * (Nf - 1) * (fu - fl)); d phih = 2 * number_pi * Dt * (fl * Nf + 0.5 * (Nf - 1) * (fu - fl));
us K = us K =
(us)(phih / (2 * number_pi) + Dt * (fu * Nb - (Nb - 1) * (fu - fl))); (us)(phih / (2 * number_pi) + Dt * (fu * Nb - (Nb - 1) * (fu - fl)));
d eps_num1 = (K - phih / (2 * number_pi)) / Dt; d eps_num1 = (K - phih / (2 * number_pi)) / Dt;
d eps_num2 = -fu * Nb + (Nb - 1) * (fu - fl); d eps_num2 = -fu * Nb + (Nb - 1) * (fu - fl);
@ -198,9 +201,9 @@ void Sweep::resetImpl() {
const us Nb = N - Nf; const us Nb = N - Nf;
const d k1 = (fu / fl); const d k1 = (fu / fl);
const d phif1 = const d phif1 =
2 * number_pi * Dt * fl * (k1 - 1) / (d_pow(k1, 1.0 / Nf) - 1); 2 * number_pi * Dt * fl * (k1 - 1) / (d_pow(k1, 1.0 / Nf) - 1);
const us K = (us)(phif1 / (2 * number_pi) + const us K = (us)(phif1 / (2 * number_pi) +
Dt * fu * (1 / k1 - 1) / (d_pow(1 / k1, 1.0 / Nb) - 1)); Dt * fu * (1 / k1 - 1) / (d_pow(1 / k1, 1.0 / Nb) - 1));
d E; d E;
d k = k1; d k = k1;
@ -209,7 +212,7 @@ void Sweep::resetImpl() {
* continuous */ * continuous */
for (us iter = 0; iter < NITER_NEWTON; iter++) { for (us iter = 0; iter < NITER_NEWTON; iter++) {
E = (k - 1) / (d_pow(k, 1.0 / Nf) - 1) + E = (k - 1) / (d_pow(k, 1.0 / Nf) - 1) +
(k - 1) / (1 - d_pow(k, -1.0 / Nb)) - K / Dt / fl; (k - 1) / (1 - d_pow(k, -1.0 / Nb)) - K / Dt / fl;
DEBUGTRACE_PRINT(E); DEBUGTRACE_PRINT(E);
/* All parts of the derivative of above error E to k */ /* All parts of the derivative of above error E to k */
@ -217,14 +220,13 @@ void Sweep::resetImpl() {
d dEdk2 = (1 / k - 1) / (d_pow(k, -1.0 / Nb) - 1); d dEdk2 = (1 / k - 1) / (d_pow(k, -1.0 / Nb) - 1);
d dEdk3 = -1 / (k * (d_pow(k, -1.0 / Nb) - 1)); d dEdk3 = -1 / (k * (d_pow(k, -1.0 / Nb) - 1));
d dEdk4 = d_pow(k, -1.0 / Nb) * (1 / k - 1) / d dEdk4 = d_pow(k, -1.0 / Nb) * (1 / k - 1) /
(Nb * d_pow(d_pow(k, -1.0 / Nb) - 1, 2)); (Nb * d_pow(d_pow(k, -1.0 / Nb) - 1, 2));
d dEdk5 = -d_pow(k, 1.0 / Nf) * (k - 1) / d dEdk5 = -d_pow(k, 1.0 / Nf) * (k - 1) /
(Nf * k * d_pow(d_pow(k, 1.0 / Nf) - 1, 2)); (Nf * k * d_pow(d_pow(k, 1.0 / Nf) - 1, 2));
d dEdk = dEdk1 + dEdk2 + dEdk3 + dEdk4 + dEdk5; d dEdk = dEdk1 + dEdk2 + dEdk3 + dEdk4 + dEdk5;
/* Iterate! */ /* Iterate! */
k -= E / dEdk; k -= E / dEdk;
} }
DEBUGTRACE_PRINT(K); DEBUGTRACE_PRINT(K);
@ -254,5 +256,10 @@ void Sweep::resetImpl() {
/* This should be a very small number!! */ /* This should be a very small number!! */
DEBUGTRACE_PRINT(phase); DEBUGTRACE_PRINT(phase);
} }
} // End of log sweep
else {
// Either log or linear sweep had to be given as flags.
assert(false);
} }
} }

View File

@ -64,6 +64,12 @@ class Periodic: public Siggen {
vd _signal { 1, arma::fill::zeros}; vd _signal { 1, arma::fill::zeros};
us _cur_pos = 0; us _cur_pos = 0;
public: public:
/**
* @brief Return copy of the generated sequence.
*
* @return As stated above
*/
vd getSequence() const { return _signal; }
virtual vd genSignalUnscaled(const us nframes) override final; virtual vd genSignalUnscaled(const us nframes) override final;
~Periodic() = default; ~Periodic() = default;

View File

@ -1,10 +1,6 @@
#include "lasp_avpowerspectra.h"
#include "lasp_biquadbank.h"
#include "lasp_fft.h"
#include "lasp_siggen.h" #include "lasp_siggen.h"
#include "arma_npy.h"
#include "lasp_siggen_impl.h" #include "lasp_siggen_impl.h"
#include "lasp_slm.h"
#include "lasp_window.h"
#include <iostream> #include <iostream>
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
@ -24,25 +20,30 @@ namespace py = pybind11;
*/ */
void init_siggen(py::module &m) { void init_siggen(py::module &m) {
/// Siggen /// Siggen
py::class_<Siggen, std::shared_ptr<Siggen>> siggen(m, "Siggen"); py::class_<Siggen, std::shared_ptr<Siggen>> siggen(m, "Siggen");
siggen.def("setLevel", &Siggen::setLevel, siggen.def("setLevel", &Siggen::setLevel,
"Set the level of the signal generator"); "Set the level of the signal generator");
siggen.def("setDCOffset", &Siggen::setDCOffset); siggen.def("setDCOffset", &Siggen::setDCOffset);
siggen.def("setMute", &Siggen::setMute); siggen.def("setMute", &Siggen::setMute);
siggen.def("setLevel", &Siggen::setLevel); siggen.def("setLevel", &Siggen::setLevel);
siggen.def("setLevel", &Siggen::setLevel, py::arg("newLevel"), py::arg("dB") = true); siggen.def("setLevel", &Siggen::setLevel, py::arg("newLevel"),
py::arg("dB") = true);
siggen.def("reset", &Siggen::reset);
siggen.def("setFilter", &Siggen::setFilter); siggen.def("setFilter", &Siggen::setFilter);
py::class_<Noise,std::shared_ptr<Noise>> noise(m, "Noise", siggen); py::class_<Noise, std::shared_ptr<Noise>> noise(m, "Noise", siggen);
noise.def(py::init<>()); noise.def(py::init<>());
py::class_<Sine, std::shared_ptr<Sine>> sine(m, "Sine", siggen); py::class_<Sine, std::shared_ptr<Sine>> sine(m, "Sine", siggen);
sine.def(py::init<const d>()); sine.def(py::init<const d>());
sine.def("setFreq", &Sine::setFreq); sine.def("setFreq", &Sine::setFreq);
py::class_<Periodic, std::shared_ptr<Periodic>> periodic(m, "Periodic", siggen); py::class_<Periodic, std::shared_ptr<Periodic>> periodic(m, "Periodic",
siggen);
periodic.def("getSequence",
[](const Sweep &s) { return ColToNpy<d>(s.getSequence()); });
py::class_<Sweep, std::shared_ptr<Sweep>> sweep(m, "Sweep", periodic); py::class_<Sweep, std::shared_ptr<Sweep>> sweep(m, "Sweep", periodic);
sweep.def(py::init<const d, const d, const d, const d, const us>()); sweep.def(py::init<const d, const d, const d, const d, const us>());
@ -50,6 +51,5 @@ void init_siggen(py::module &m) {
sweep.def_readonly_static("BackwardSweep", &Sweep::BackwardSweep); sweep.def_readonly_static("BackwardSweep", &Sweep::BackwardSweep);
sweep.def_readonly_static("LinearSweep", &Sweep::LinearSweep); sweep.def_readonly_static("LinearSweep", &Sweep::LinearSweep);
sweep.def_readonly_static("LogSweep", &Sweep::LogSweep); sweep.def_readonly_static("LogSweep", &Sweep::LogSweep);
} }
/** @} */ /** @} */