Added logarithmic sweep. For now, chosen to not implement the hyperbolic sweep

This commit is contained in:
Anne de Jong 2021-02-19 10:23:51 +01:00
parent 1c5a5f04f2
commit d72a35bfb5
3 changed files with 392 additions and 239 deletions

View File

@ -5,19 +5,24 @@
// Description:
// Signal generator implementation
//////////////////////////////////////////////////////////////////////
#define TRACERPLUS (-5)
/* #define TRACERPLUS (-5) */
#include "lasp_siggen.h"
#include "lasp_alloc.h"
#include "lasp_assert.h"
#include "lasp_mat.h"
/** The fixed number of Newton iterations t.b.d. for tuning the sweep start and
* stop frequency in logarithmic sweeps */
#define NITER_NEWTON 20
/** The number of Bytes of space for the signal-specific data in the Siggen
* structure */
#define PRIVATE_SIZE 64
typedef enum {
SINEWAVE = 0,
NOISE,
SWEEP,
} SignalType;
typedef struct Siggen {
@ -33,14 +38,10 @@ typedef struct {
} SinewaveSpecific;
typedef struct {
d fl;
d fu;
d Ts;
d phase;
d tau;
bool pos;
us flags;
} SweepSpecific;
us N;
vd data;
us index;
} PeriodicSpecific;
typedef struct {
d V1, V2, S;
@ -96,39 +97,238 @@ Siggen* Siggen_Noise_create(const d fs, const d level_dB, Sosfilterbank* colorfi
return noise;
}
Siggen* Siggen_Sweep_create(const d fs,const d fl,const d fu,
Siggen* Siggen_Sweep_create(const d fs,const d fl_,const d fu_,
const d Ts, const us flags, const d level_dB) {
fsTRACE(15);
Siggen* sweep = Siggen_create(SWEEP, fs, level_dB);
dbgassert(sizeof(SweepSpecific) <= sizeof(sweep->private_data),
dbgassert(sizeof(PeriodicSpecific) <= sizeof(sweep->private_data),
"Allocated memory too small");
bool forward_sweep = flags & SWEEP_FLAG_FORWARD;
bool backward_sweep = flags & SWEEP_FLAG_BACKWARD;
// Set pointer to inplace data storage
SweepSpecific* sp = (SweepSpecific*) sweep->private_data;
if(fl < 0 || fu < 0 || Ts <= 0) {
dbgassert(!(forward_sweep && backward_sweep), "Both forward and backward flag set");
PeriodicSpecific* sp = (PeriodicSpecific*) sweep->private_data;
if(fl_ < 0 || fu_ < 0 || Ts <= 0) {
return NULL;
}
sp->flags = flags;
sp->fl = fl;
sp->fu = fu;
sp->Ts = Ts;
sp->phase = 0;
sp->pos = flags & SWEEP_FLAG_BACKWARD ? false: true;
if(flags & SWEEP_FLAG_BACKWARD) {
sp->tau = Ts;
} else {
sp->tau = 0;
const d Dt = 1/fs; // Deltat
// Estimate N:
const us N = (us) (Ts*fs);
iVARTRACE(15, N);
sp->data = vd_alloc(N);
sp->index = 0;
sp->N = N;
vd* data = &(sp->data);
// Obtain flags and expand
d phase = 0;
d fl, fu;
/* Swap fl and fu for a backward sweep */
if(backward_sweep) {
fu = fl_;
fl = fu_;
}
else {
/* Case of continuous sweep, or forward sweep */
fl = fl_;
fu = fu_;
}
/* Linear sweep */
if(flags & SWEEP_FLAG_LINEAR) {
TRACE(15, "linear sweep");
if(forward_sweep || backward_sweep) {
/* Forward or backward sweep */
TRACE(15, "Forward or backward sweep");
us K = (us) (Dt*(fl*N+0.5*(N-1)*(fu-fl)));
d eps_num = ((d) K)/Dt - fl*N-0.5*(N-1)*(fu-fl);
d eps = eps_num/(0.5*(N-1));
iVARTRACE(15, K);
dVARTRACE(15, eps);
for(us n = 0; n<N; n++) {
setvecval(data, n, d_sin(phase));
d fn = fl + ((d) n)/N*(fu + eps -fl);
phase += 2*number_pi*Dt*fn;
}
}
else {
/* Continous sweep */
TRACE(15, "continuous sweep");
iVARTRACE(17, N);
dVARTRACE(17, fl);
dVARTRACE(17, fu);
const us Nf = N/2;
const us Nb = N-Nf;
/* Phi halfway */
d phih = 2*number_pi*Dt*(fl*Nf+0.5*(Nf-1)*(fu-fl));
us K = (us) (phih/(2*number_pi) + Dt*(fu*Nb - (Nb-1)*(fu-fl)));
d eps_num1 = (K- phih/(2*number_pi))/Dt;
d eps_num2 = -fu*Nb + (Nb-1)*(fu-fl);
d eps = (eps_num1+eps_num2)/(0.5*(Nb+1));
iVARTRACE(15, K);
dVARTRACE(15, eps);
for(us n = 0; n<=N; n++) {
/* iVARTRACE(17, n); */
if(n<N) {
setvecval(data, n, d_sin(phase));
}
d fn;
if(n <= Nf) {
fn = fl + ((d) n)/Nf*(fu -fl);
} else {
fn = fu - ((d) n - Nf)/Nb*(fu + eps - fl);
}
dbgassert(fn >= 0, "BUG");
phase += 2*number_pi*Dt*fn;
/* dVARTRACE(17, phase); */
/* setvecval(data, n, fn); */
/* setvecval(data, n, phase); */
}
/* This should be a very small number!! */
dVARTRACE(15, phase);
}
}
else if(flags & SWEEP_FLAG_EXPONENTIAL) {
TRACE(15, "exponential sweep");
if(forward_sweep || backward_sweep) {
/* Forward or backward sweep */
TRACE(15, "Forward or backward sweep");
d k1 = (fu/fl);
us K = (us) (Dt*fl*(k1-1)/(d_pow(k1,1.0/N)-1));
d k = k1;
/* Iterate k to the right solution */
d E;
for(us iter=0;iter< 10; iter++) {
E = 1 + K/(Dt*fl)*(d_pow(k,1.0/N)-1) - k;
d dEdk = K/(Dt*fl)*d_pow(k,1.0/N)/(N*k)-1;
k -= E/dEdk;
}
iVARTRACE(15, K);
dVARTRACE(15, k1);
dVARTRACE(15, k);
dVARTRACE(15, E);
for(us n = 0; n<N; n++) {
setvecval(data, n, d_sin(phase));
d fn = fl*d_pow(k,((d) n)/N);
phase += 2*number_pi*Dt*fn;
}
} else {
TRACE(15, "Continuous sweep");
const us Nf = N/2;
const us Nb = N-Nf;
const d k1 = (fu/fl);
const d phif1 = 2*number_pi*Dt*fl*(k1-1)/(d_pow(k1,1.0/Nf)-1);
const us K = (us) (phif1/(2*number_pi) + Dt*fu*(1/k1-1)/(d_pow(1/k1,1.0/Nb)-1));
d E;
d k = k1;
/* Newton iterations to converge k to the value such that the sweep is
* continuous */
for(us iter=0;iter<NITER_NEWTON; iter++) {
E = (k-1)/(d_pow(k,1.0/Nf)-1) + (k-1)/(1-d_pow(k,-1.0/Nb)) - K/Dt/fl;
dVARTRACE(15, E);
/* All parts of the derivative of above error E to k */
d dEdk1 = 1/(d_pow(k,1.0/Nf)-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 dEdk4 = d_pow(k,-1.0/Nb)*(1/k-1)/(Nb*d_pow(d_pow(k, -1.0/Nb)-1,2));
d dEdk5 = -d_pow(k,1.0/Nf)*(k-1)/(Nf*k*d_pow(d_pow(k,1.0/Nf)-1,2));
d dEdk = dEdk1+dEdk2+dEdk3+dEdk4+dEdk5;
/* Iterate! */
k -= E/dEdk;
dVARTRACE(15, k);
}
iVARTRACE(15, K);
dVARTRACE(15, k1);
dVARTRACE(15, k);
dVARTRACE(15, E);
for(us n = 0; n<=N; n++) {
/* iVARTRACE(17, n); */
if(n<N) {
setvecval(data, n, d_sin(phase));
}
d fn;
if(n <= Nf) {
fn = fl * d_pow(k, ((d) n)/Nf);
} else {
fn = fl*k * d_pow(1/k, ((d) n - Nf)/Nb);
}
dbgassert(fn >= 0, "BUG");
phase += 2*number_pi*Dt*fn;
while(phase > 2*number_pi) phase -= 2*number_pi;
/* dVARTRACE(17, phase); */
/* setvecval(data, n, fn); */
/* setvecval(data, n, phase); */
}
/* This should be a very small number!! */
dVARTRACE(15, phase);
}
}
/* sp->pos = false; */
/* sp->tau = Ts/2; */
feTRACE(15);
return sweep;
}
static void Siggen_periodic_free(PeriodicSpecific* ps) {
assertvalidptr(ps);
fsTRACE(15);
vd_free(&(ps->data));
feTRACE(15);
}
us Siggen_getN(const Siggen* siggen) {
fsTRACE(15);
assertvalidptr(siggen);
switch(siggen->signaltype) {
case SINEWAVE:
break;
case NOISE:
break;
case SWEEP:
return ((PeriodicSpecific*) siggen->private_data)->N;
break;
default:
dbgassert(false, "Not implementend signal type");
}
feTRACE(15);
return 0;
}
void Siggen_free(Siggen* siggen) {
fsTRACE(15);
assertvalidptr(siggen);
@ -137,6 +337,7 @@ void Siggen_free(Siggen* siggen) {
switch(siggen->signaltype) {
case SWEEP:
/* Sweep specific stuff here */
Siggen_periodic_free((PeriodicSpecific*) siggen->private_data);
break;
case SINEWAVE:
/* Sweep specific stuff here */
@ -154,7 +355,7 @@ void Siggen_free(Siggen* siggen) {
}
static void Sinewave_genSignal(Siggen* siggen, SinewaveSpecific* sine, vd* samples) {
fsTRACE(15);
fsTRACE(14);
assertvalidptr(sine);
d ts = 1/siggen->fs;
d omg = sine->omg;
@ -165,84 +366,23 @@ static void Sinewave_genSignal(Siggen* siggen, SinewaveSpecific* sine, vd* sampl
curtime = curtime + ts;
}
sine->curtime = curtime;
feTRACE(15);
feTRACE(14);
}
static void Sweep_genSignal(Siggen* siggen, SweepSpecific* sweep,
vd* samples) {
fsTRACE(15);
assertvalidptr(sweep);
static void Periodic_genSignal(Siggen* siggen, PeriodicSpecific* sweep, vd* samples) {
fsTRACE(10);
const d fl = sweep->fl;
const d fu = sweep->fu;
const d deltat = 1/siggen->fs;
const d Ts = sweep->Ts;
const d Thalf = Ts/2;
dVARTRACE(15, deltat);
// Load state
d tau = sweep->tau;
bool pos = sweep->pos;
// Obtain flags and expand
us flags = sweep->flags;
bool forward_sweep = flags & SWEEP_FLAG_FORWARD;
bool backward_sweep = flags & SWEEP_FLAG_BACKWARD;
dbgassert(!(forward_sweep && backward_sweep), "Both forward and backward flag set");
d k, Treverse;
if(forward_sweep || backward_sweep) {
k = (fu - fl)/Ts;
Treverse = Ts;
}
else {
k = (fu - fl)/Thalf;
Treverse = Ts/2;
for(us i=0; i<samples->n_rows; i++) {
d* data = getvdval(&(sweep->data), sweep->index);
setvecval(samples, i, siggen->level_amp*(*data));
sweep->index++;
sweep->index %= sweep->N;
}
/* const d k = 0; */
d phase = sweep->phase;
d curfreq;
for(us i =0; i< samples->n_rows; i++) {
curfreq = fl + k*tau;
phase = phase + 2*number_pi*curfreq*deltat;
// Subtract some to avoid possible overflow. Don't know whether such a
// thing really happens
if(phase > 2*number_pi)
phase = phase - 2*number_pi;
if(pos) {
tau = tau + deltat;
if(tau >= Treverse) {
if(forward_sweep) { tau = 0; }
else if(backward_sweep) { dbgassert(false, "BUG"); }
else { pos = false; }
}
} else {
/* dbgassert(false, "cannot get here"); */
tau = tau - deltat;
if(tau <= 0) {
if(backward_sweep) { tau = Treverse; }
else if(forward_sweep) { dbgassert(false, "BUG"); }
else { pos = true; }
}
}
setvecval(samples, i, siggen->level_amp*d_sin(phase));
}
// Store state
sweep->phase = phase;
sweep->pos = pos;
sweep->tau = tau;
feTRACE(15);
feTRACE(10);
}
static void noise_genSignal(Siggen* siggen, NoiseSpecific* wn, vd* samples) {
fsTRACE(15);
d X;
@ -288,7 +428,7 @@ static void noise_genSignal(Siggen* siggen, NoiseSpecific* wn, vd* samples) {
void Siggen_genSignal(Siggen* siggen,vd* samples) {
fsTRACE(15);
fsTRACE(10);
assertvalidptr(siggen);
assert_vx(samples);
@ -305,8 +445,8 @@ void Siggen_genSignal(Siggen* siggen,vd* samples) {
samples);
break;
case SWEEP:
Sweep_genSignal(siggen,
(SweepSpecific*) siggen->private_data,
Periodic_genSignal(siggen,
(PeriodicSpecific*) siggen->private_data,
samples);
break;
default:
@ -314,7 +454,7 @@ void Siggen_genSignal(Siggen* siggen,vd* samples) {
}
feTRACE(15);
feTRACE(10);
}

View File

@ -12,6 +12,15 @@
#include "lasp_mat.h"
#include "lasp_sosfilterbank.h"
// Define this flag to repeat a forward sweep only, or backward only. If not
// set, we do a continuous sweep
#define SWEEP_FLAG_FORWARD 1
#define SWEEP_FLAG_BACKWARD 2
// Types of sweeps
#define SWEEP_FLAG_LINEAR 4
#define SWEEP_FLAG_EXPONENTIAL 8
typedef struct Siggen Siggen;
/**
@ -37,15 +46,15 @@ Siggen* Siggen_Sinewave_create(const d fs,const d freq,const d level_dB);
*/
Siggen* Siggen_Noise_create(const d fs, const d level_dB, Sosfilterbank* colorfilter);
// Define this flag to repeat a forward sweep only, or backward only. If not
// set, we do a continuous sweep
#define SWEEP_FLAG_FORWARD 1
#define SWEEP_FLAG_BACKWARD 2
// Types of sweeps
#define SWEEP_FLAG_LINEAR 4
#define SWEEP_FLAG_EXPONENTIAL 8
#define SWEEP_FLAG_HYPERBOLIC 16
/**
* Obtain the repetition period for a periodic excitation.
* @param[in] Siggen* Signal generator handle
*
* @param[out] N The amount of samples in one period, returns 0 if the signal
* does not repeat.
*/
us Siggen_getN(const Siggen*);
/**
* Create a forward sweep
@ -65,7 +74,7 @@ Siggen* Siggen_Sweep_create(const d fs,const d fl,const d fu,
/**
* Obtain a new piece of signal
*
* @param[in] Siggen* Signal generator private data
* @param[in] Siggen* Signal generator handle
* @param[out] samples Samples to fill. Vector should be pre-allocated
*/
void Siggen_genSignal(Siggen*,vd* samples);

View File

@ -635,6 +635,7 @@ cdef extern from "lasp_siggen.h":
c_Siggen* Siggen_Sweep_create(d fs, d fl,
d fu, d Ts,us sweep_flags,
d level_dB)
us Siggen_getN(const c_Siggen*)
void Siggen_genSignal(c_Siggen*, vd* samples) nogil
void Siggen_free(c_Siggen*)
@ -675,6 +676,9 @@ cdef class Siggen:
return output
def getN(self):
return Siggen_getN(self._siggen)
def progress(self):
"""
TODO: Should be implemented to return the current position in the