10#include "debugtrace.hpp"
14using rte = std::runtime_error;
20#define NITER_NEWTON 20
24vd Noise::genSignalUnscaled(
us nframes) {
25 return arma::randn<vd>(nframes);
27void Noise::resetImpl() {}
29Sine::Sine(
const d freq) : omg(2 * arma::datum::pi * freq) { DEBUGTRACE_ENTER; }
33 const d pi = arma::datum::pi;
35 arma::linspace(phase, phase + omg * (nframes - 1) /
_fs, nframes);
36 phase += omg * nframes /
_fs;
37 while (phase > 2 * arma::datum::pi) {
40 return arma::sin(phase_vec);
47 throw rte(
"No signal defined while calling");
49 for (
us i = 0; i < nframes; i++) {
58 : fl_(fl), fu_(fu), Ts(Ts), Tq(Tq), flags(flags) {
59 if (fl <= 0 || fu < fl || Ts <= 0) {
60 throw rte(
"Invalid sweep parameters");
64 "Both forward and backward sweep flag set. Please only set either one "
65 "or none for a continuous sweep");
69 "Both logsweep and linear sweep flag set. Please only set either one.");
72 throw rte(
"Either LinearSweep or LogSweep should be given as flag");
76void Sweep::resetImpl() {
110 if (forward_sweep || backward_sweep) {
113 us K = (
us)(Dt * (fl * Ns + 0.5 * (Ns - 1) * (fu - fl)));
114 d eps_num = ((
d)K) / Dt - fl * Ns - 0.5 * (Ns - 1) * (fu - fl);
115 d eps = eps_num / (0.5 * (Ns - 1));
119 for (
us n = 0; n < Ns; n++) {
121 d fn = fl + ((
d)n) / Ns * (fu + eps - fl);
122 phase += 2 * arma::datum::pi * Dt * fn;
132 const us Nf = Ns / 2;
133 const us Nb = Ns - Nf;
136 d phih = 2 *
number_pi * Dt * (fl * Nf + 0.5 * (Nf - 1) * (fu - fl));
139 (
us)(phih / (2 *
number_pi) + Dt * (fu * Nb - (Nb - 1) * (fu - fl)));
141 d eps_num1 = (K - phih / (2 *
number_pi)) / Dt;
142 d eps_num2 = -fu * Nb + (Nb - 1) * (fu - fl);
143 d eps = (eps_num1 + eps_num2) / (0.5 * (Nb + 1));
149 for (
us n = 0; n <= Ns; n++) {
157 fn = fl + ((
d)n) / Nf * (fu - fl);
159 fn = fu - ((
d)n - Nf) / Nb * (fu + eps - fl);
170 DEBUGTRACE_PRINT(
"Log sweep");
171 if (forward_sweep || backward_sweep) {
173 DEBUGTRACE_PRINT(
"Forward or backward sweep");
175 us K = (
us)(Dt * fl * (k1 - 1) / (
d_pow(k1, 1.0 / Ns) - 1));
180 for (
us iter = 0; iter < 10; iter++) {
181 E = 1 + K / (Dt * fl) * (
d_pow(k, 1.0 / Ns) - 1) - k;
182 d dEdk = K / (Dt * fl) *
d_pow(k, 1.0 / Ns) / (Ns * k) - 1;
187 DEBUGTRACE_PRINT(k1);
191 for (
us n = 0; n < Ns; n++) {
193 d fn = fl *
d_pow(k, ((d)n) / Ns);
198 DEBUGTRACE_PRINT(
"Continuous sweep");
200 const us Nf = Ns / 2;
201 const us Nb = Ns - Nf;
202 const d k1 = (fu / fl);
206 Dt * fu * (1 / k1 - 1) / (
d_pow(1 / k1, 1.0 / Nb) - 1));
214 E = (k - 1) / (
d_pow(k, 1.0 / Nf) - 1) +
215 (k - 1) / (1 -
d_pow(k, -1.0 / Nb)) - K / Dt / fl;
219 d dEdk1 = 1 / (
d_pow(k, 1.0 / Nf) - 1);
220 d dEdk2 = (1 / k - 1) / (
d_pow(k, -1.0 / Nb) - 1);
221 d dEdk3 = -1 / (k * (
d_pow(k, -1.0 / Nb) - 1));
222 d dEdk4 =
d_pow(k, -1.0 / Nb) * (1 / k - 1) /
224 d dEdk5 = -
d_pow(k, 1.0 / Nf) * (k - 1) /
226 d dEdk = dEdk1 + dEdk2 + dEdk3 + dEdk4 + dEdk5;
233 DEBUGTRACE_PRINT(k1);
237 for (
us n = 0; n <= Ns; n++) {
245 fn = fl *
d_pow(k, ((d)n) / Nf);
247 fn = fl * k *
d_pow(1 / k, ((d)n - Nf) / Nb);
257 DEBUGTRACE_PRINT(phase);
Noise()
Constructs a noise generator. If no filter is used, the output will be white noise....
virtual vd genSignalUnscaled(const us nframes) override final
Sine(const d freq_Hz)
Create a sine wave generator.
virtual vd genSignalUnscaled(const us nframes) override final
static constexpr int BackwardSweep
static constexpr int LogSweep
Sweep(const d fl, const d fu, const d Ts, const d Tq, const us sweep_flags)
static constexpr int LinearSweep
static constexpr int ForwardSweep
size_t us
We often use boolean values.