浏览代码

Fix AudioAnalyzeFFT256 magnitude & averaging

dds
PaulStoffregen 11 年前
父节点
当前提交
39fb562527
共有 2 个文件被更改,包括 13 次插入55 次删除
  1. +11
    -54
      analyze_fft256.cpp
  2. +2
    -1
      analyze_fft256.h

+ 11
- 54
analyze_fft256.cpp 查看文件

*/ */


#include "analyze_fft256.h" #include "analyze_fft256.h"
#include "sqrt_integer.h"
#include "utility/dspinst.h"
#include "arm_math.h" #include "arm_math.h"


// TODO: this should be a class member, so more than one FFT can be used // TODO: this should be a class member, so more than one FFT can be used
{ {
// TODO: replace this with static const version // TODO: replace this with static const version
arm_cfft_radix4_init_q15(&fft_inst, 256, 0, 1); arm_cfft_radix4_init_q15(&fft_inst, 256, 0, 1);

//for (int i=0; i<2048; i++) {
//buffer[i] = i * 3;
//}
//__disable_irq();
//ARM_DEMCR |= ARM_DEMCR_TRCENA;
//ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
//uint32_t n = ARM_DWT_CYCCNT;
//arm_cfft_radix2_q15(&fft_inst, buffer);
//n = ARM_DWT_CYCCNT - n;
//__enable_irq();
//cycles = n;
//arm_cmplx_mag_q15(buffer, buffer, 512);

// each audio block is 278525 cycles @ 96 MHz
// 256 point fft2 takes 65408 cycles
// 256 point fft4 takes 49108 cycles
// 128 point cmag takes 10999 cycles
// 1024 point fft2 takes 125948 cycles
// 1024 point fft4 takes 125840 cycles
// 512 point cmag takes 43764 cycles

//state = 0; //state = 0;
//outputflag = false; //outputflag = false;
} }
//window = NULL; //window = NULL;
if (window) apply_window_to_fft_buffer(buffer, window); if (window) apply_window_to_fft_buffer(buffer, window);
arm_cfft_radix4_q15(&fft_inst, buffer); arm_cfft_radix4_q15(&fft_inst, buffer);
// TODO: is this averaging correct? G. Heinzel's paper says we're
// supposed to average the magnitude squared, then do the square
// root at the end after dividing by naverage.
arm_cmplx_mag_q15(buffer, buffer, 128);
// G. Heinzel's paper says we're supposed to average the magnitude
// squared, then do the square root at the end.
if (count == 0) { if (count == 0) {
for (int i=0; i < 128; i++) { for (int i=0; i < 128; i++) {
output[i] = buffer[i];
uint32_t tmp = *((uint32_t *)buffer + i);
uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
sum[i] = magsq / naverage;
} }
} else { } else {
for (int i=0; i < 128; i++) { for (int i=0; i < 128; i++) {
output[i] += buffer[i];
uint32_t tmp = *((uint32_t *)buffer + i);
uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
sum[i] += magsq / naverage;
} }
} }
if (++count == naverage) { if (++count == naverage) {
count = 0; count = 0;
for (int i=0; i < 128; i++) { for (int i=0; i < 128; i++) {
output[i] /= naverage;
output[i] = sqrt_uint32_approx(sum[i]);
} }
outputflag = true; outputflag = true;
} }

release(prevblock); release(prevblock);
prevblock = block; prevblock = block;

#if 0
block = receiveReadOnly();
if (state == 0) {
//Serial.print("0");
if (block == NULL) return;
copy_to_fft_buffer(buffer, block->data);
state = 1;
} else if (state == 1) {
//Serial.print("1");
if (block == NULL) return;
copy_to_fft_buffer(buffer+256, block->data);
arm_cfft_radix4_q15(&fft_inst, buffer);
state = 2;
} else {
//Serial.print("2");
arm_cmplx_mag_q15(buffer, output, 128);
outputflag = true;
if (block == NULL) return;
copy_to_fft_buffer(buffer, block->data);
state = 1;
}
release(block);
#endif
} }





+ 2
- 1
analyze_fft256.h 查看文件

} }
virtual void update(void); virtual void update(void);
//uint32_t cycles; //uint32_t cycles;
int32_t output[128] __attribute__ ((aligned (4)));
uint16_t output[128] __attribute__ ((aligned (4)));
private: private:
void init(void); void init(void);
const int16_t *window; const int16_t *window;
audio_block_t *prevblock; audio_block_t *prevblock;
int16_t buffer[512] __attribute__ ((aligned (4))); int16_t buffer[512] __attribute__ ((aligned (4)));
uint32_t sum[128];
uint8_t count; uint8_t count;
uint8_t naverage; uint8_t naverage;
bool outputflag; bool outputflag;

正在加载...
取消
保存