copy

Author: Smashed Transistors
License: LGPL
Github: tiar/PIW/copy.axo

Inlets

None

Outlets

None

Attributes

combo size

combo location

objref gen

Declaration
#if attr_location == 0
#define LOCATION
#else
#define LOCATION __attribute__((section(".sdram")))
#endif
static const uint32_t LENGTHPOW = attr_size;
static const uint32_t LENGTH = 1 << attr_size;
static const uint32_t LENGTHMASK = (1 << attr_size) - 1;
static const uint32_t BITS = 32;
static const uint32_t GAIN = 7; // q20 to q27

int32_t *v;
float *fv; // in place
int32_t *array;
int32_t *Iv;

void prepare(void) {
  // pre integrate
  float moy = 0.0f;
  for (int i = 0; i < LENGTH; i++) {
    moy += fv[i];
  }
  moy *= 1.0f / LENGTH;
  float M = 0.0f;
  for (int i = 0; i < LENGTH; i++) {
    fv[i] -= moy;
    if (fabs(fv[i]) > M)
      M = fabs(fv[i]);
  }
  // normalise
  if (M != 0.0f) {
    M = 1.0f / M;
    for (int i = 0; i < LENGTH; i++) {
      fv[i] *= M;
    }
  }
  // convert
  for (int i = 0; i < LENGTH; i++) {
    v[i] = arm::float_to_q(fv[i], 27 - GAIN);
  }
  v[LENGTH] = v[0]; // for easy dv: dv = v[i+1] - v[i]
  // integrate (checked)
  int32_t I1 = 0;
  for (int i = 0; i < LENGTH; i++) {
    Iv[i] = I1;
    I1 += v[i] + (v[i + 1] - v[i]) / 2;
  }
}
Init
static int32_t v_[attr_poly][LENGTH + 1] LOCATION;
static int32_t Iv_[attr_poly][LENGTH] LOCATION;
v = &v_[parent->polyIndex][0];
fv = (float *)v; // in place
array = v;
Iv = &Iv_[parent->polyIndex][0];

float coef = ((float)attr_gen.LENGTH) / LENGTH;
for (int i = 0; i < LENGTH; i++) {
  fv[i] = (float)attr_gen.v[(int)(i * coef)];
}
prepare();

Privacy

© 2024 Zrna Research