rfft 128

spectral analyzer display using 128 input points fft
Author: Johannes Taelman
License: BSD
Github: spectral/rfft 128.axo

Inlets

frac32buffer input

bool32 hold

Outlets

None

Displays

frac4ubyte.vbar v0

frac4ubyte.vbar v1

frac4ubyte.vbar v2

frac4ubyte.vbar v3

frac4ubyte.vbar v4

frac4ubyte.vbar v5

frac4ubyte.vbar v6

frac4ubyte.vbar v7

frac4ubyte.vbar v8

frac4ubyte.vbar v9

frac4ubyte.vbar v10

frac4ubyte.vbar v11

frac4ubyte.vbar v12

frac4ubyte.vbar v13

frac4ubyte.vbar v14

frac4ubyte.vbar v15

Declaration
int32_t inbuf[128];
int32_t outbuf[128];
int32_t fftbuf[256];
int32_t hanning_q31[128];
arm_rfft_instance_q31 rfft;
arm_cfft_radix4_instance_q31 cfft;
int32_t state;
msg_t ThreadX2() {
  int i;
  int n = 128;
  arm_rfft_init_q31(&rfft, 128, 0, 1);
  for (i = 0; i < n; i++) {
    hanning_q31[i] =
        (int32_t)(0.5f * 2147483647.0f * (1.0f - cosf(2.0f * PI * i / n)));
  }
  while (!chThdShouldTerminate()) {
    chThdSleepMilliseconds(20);
    if (state == 128) {
      arm_mult_q31(hanning_q31, &inbuf[0], &inbuf[0], n);
      arm_rfft_q31(&rfft, &inbuf[0], &fftbuf[0]);
      arm_cmplx_mag_q31(&fftbuf[0], outbuf, n / 2);
      // reduce to packed 8bit
      for (i = 0; i < n / 8; i++) {
        int32_t ni;
        uint8_t *nc;
        nc = (uint8_t *)&ni;
        nc[0] = 0xFF & (outbuf[i * 4] >> 19);
        nc[1] = 0xFF & (outbuf[i * 4 + 1] >> 19);
        nc[2] = 0xFF & (outbuf[i * 4 + 2] >> 19);
        nc[3] = 0xFF & (outbuf[i * 4 + 3] >> 19);
        outbuf[i] = ni;
      }
      state = 129;
    }
  }
}
static msg_t ThreadX(void *arg) { ((attr_parent *)arg)->ThreadX2(); }
WORKING_AREA(waThreadX, 4096);
Thread *Thd;
Init
int i;
for (i = 0; i < 128; i++)
  inbuf[i] = 0;
for (i = 0; i < 128; i++)
  outbuf[i] = 0;
state = 0;
Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
                        (void *)this);
Control Rate
if (state < 128) {
  int i;
  for (i = 0; i < 16; i++)
    inbuf[state++] = inlet_in[i];
} else if (state == 129) {
  state = 0;
  if (!inlet_hold) {
    disp_v0 = outbuf[0];
    disp_v1 = outbuf[1];
    disp_v2 = outbuf[2];
    disp_v3 = outbuf[3];
    disp_v4 = outbuf[4];
    disp_v5 = outbuf[5];
    disp_v6 = outbuf[6];
    disp_v7 = outbuf[7];
    disp_v8 = outbuf[8];
    disp_v9 = outbuf[9];
    disp_v10 = outbuf[10];
    disp_v11 = outbuf[11];
    disp_v12 = outbuf[12];
    disp_v13 = outbuf[13];
    disp_v14 = outbuf[14];
    disp_v15 = outbuf[15];
  }
}
Dispose
chThdTerminate(Thd);

Privacy

© 2024 Zrna Research