diff options
Diffstat (limited to 'codec2_fft.c')
| -rw-r--r-- | codec2_fft.c | 112 |
1 files changed, 52 insertions, 60 deletions
diff --git a/codec2_fft.c b/codec2_fft.c index 14b8670..7a75062 100644 --- a/codec2_fft.c +++ b/codec2_fft.c | |||
| @@ -61,66 +61,63 @@ static const arm_cfft_instance_f32* arm_fft_cache_get(const arm_cfft_instance_f3 | |||
| 61 | #endif | 61 | #endif |
| 62 | #endif | 62 | #endif |
| 63 | 63 | ||
| 64 | void codec2_fft_free(codec2_fft_cfg cfg) | 64 | void codec2_fft_free(codec2_fft_cfg cfg) { |
| 65 | { | ||
| 66 | #ifdef USE_KISS_FFT | 65 | #ifdef USE_KISS_FFT |
| 67 | KISS_FFT_FREE(cfg); | 66 | KISS_FFT_FREE(cfg); |
| 68 | #else | 67 | #else |
| 69 | FREE(cfg); | 68 | FREE(cfg); |
| 70 | #endif | 69 | #endif |
| 71 | } | 70 | } |
| 72 | 71 | ||
| 73 | codec2_fft_cfg codec2_fft_alloc(int nfft, int inverse_fft, void* mem, size_t* lenmem) | 72 | codec2_fft_cfg codec2_fft_alloc(int nfft, int inverse_fft, void* mem, |
| 74 | { | 73 | size_t* lenmem) { |
| 75 | codec2_fft_cfg retval; | 74 | codec2_fft_cfg retval; |
| 76 | #ifdef USE_KISS_FFT | 75 | #ifdef USE_KISS_FFT |
| 77 | retval = kiss_fft_alloc(nfft, inverse_fft, mem, lenmem); | 76 | retval = kiss_fft_alloc(nfft, inverse_fft, mem, lenmem); |
| 78 | #else | 77 | #else |
| 79 | retval = MALLOC(sizeof(codec2_fft_struct)); | 78 | retval = MALLOC(sizeof(codec2_fft_struct)); |
| 80 | retval->inverse = inverse_fft; | 79 | retval->inverse = inverse_fft; |
| 81 | switch(nfft) | 80 | switch (nfft) { |
| 82 | { | ||
| 83 | case 128: | 81 | case 128: |
| 84 | retval->instance = &arm_cfft_sR_f32_len128; | 82 | retval->instance = &arm_cfft_sR_f32_len128; |
| 85 | break; | 83 | break; |
| 86 | case 256: | 84 | case 256: |
| 87 | retval->instance = &arm_cfft_sR_f32_len256; | 85 | retval->instance = &arm_cfft_sR_f32_len256; |
| 88 | break; | 86 | break; |
| 89 | case 512: | 87 | case 512: |
| 90 | retval->instance = &arm_cfft_sR_f32_len512; | 88 | retval->instance = &arm_cfft_sR_f32_len512; |
| 91 | break; | 89 | break; |
| 92 | // case 1024: | 90 | // case 1024: |
| 93 | // retval->instance = &arm_cfft_sR_f32_len1024; | 91 | // retval->instance = &arm_cfft_sR_f32_len1024; |
| 94 | // break; | 92 | // break; |
| 95 | default: | 93 | default: |
| 96 | abort(); | 94 | abort(); |
| 97 | } | 95 | } |
| 98 | // retval->instance = arm_fft_cache_get(retval->instance); | 96 | // retval->instance = arm_fft_cache_get(retval->instance); |
| 99 | #endif | 97 | #endif |
| 100 | return retval; | 98 | return retval; |
| 101 | } | 99 | } |
| 102 | 100 | ||
| 103 | codec2_fftr_cfg codec2_fftr_alloc(int nfft, int inverse_fft, void* mem, size_t* lenmem) | 101 | codec2_fftr_cfg codec2_fftr_alloc(int nfft, int inverse_fft, void* mem, |
| 104 | { | 102 | size_t* lenmem) { |
| 105 | codec2_fftr_cfg retval; | 103 | codec2_fftr_cfg retval; |
| 106 | #ifdef USE_KISS_FFT | 104 | #ifdef USE_KISS_FFT |
| 107 | retval = kiss_fftr_alloc(nfft, inverse_fft, mem, lenmem); | 105 | retval = kiss_fftr_alloc(nfft, inverse_fft, mem, lenmem); |
| 108 | #else | 106 | #else |
| 109 | retval = MALLOC(sizeof(codec2_fftr_struct)); | 107 | retval = MALLOC(sizeof(codec2_fftr_struct)); |
| 110 | retval->inverse = inverse_fft; | 108 | retval->inverse = inverse_fft; |
| 111 | retval->instance = MALLOC(sizeof(arm_rfft_fast_instance_f32)); | 109 | retval->instance = MALLOC(sizeof(arm_rfft_fast_instance_f32)); |
| 112 | arm_rfft_fast_init_f32(retval->instance,nfft); | 110 | arm_rfft_fast_init_f32(retval->instance, nfft); |
| 113 | // memcpy(&retval->instance->Sint,arm_fft_cache_get(&retval->instance->Sint),sizeof(arm_cfft_instance_f32)); | 111 | // memcpy(&retval->instance->Sint,arm_fft_cache_get(&retval->instance->Sint),sizeof(arm_cfft_instance_f32)); |
| 114 | #endif | 112 | #endif |
| 115 | return retval; | 113 | return retval; |
| 116 | } | 114 | } |
| 117 | void codec2_fftr_free(codec2_fftr_cfg cfg) | 115 | void codec2_fftr_free(codec2_fftr_cfg cfg) { |
| 118 | { | ||
| 119 | #ifdef USE_KISS_FFT | 116 | #ifdef USE_KISS_FFT |
| 120 | KISS_FFT_FREE(cfg); | 117 | KISS_FFT_FREE(cfg); |
| 121 | #else | 118 | #else |
| 122 | FREE(cfg->instance); | 119 | FREE(cfg->instance); |
| 123 | FREE(cfg); | 120 | FREE(cfg); |
| 124 | #endif | 121 | #endif |
| 125 | } | 122 | } |
| 126 | 123 | ||
| @@ -129,30 +126,25 @@ void codec2_fftr_free(codec2_fftr_cfg cfg) | |||
| 129 | // not noticeable | 126 | // not noticeable |
| 130 | // the reduced usage of RAM and increased performance on STM32 platforms | 127 | // the reduced usage of RAM and increased performance on STM32 platforms |
| 131 | // should be worth it. | 128 | // should be worth it. |
| 132 | void codec2_fft_inplace(codec2_fft_cfg cfg, codec2_fft_cpx* inout) | 129 | void codec2_fft_inplace(codec2_fft_cfg cfg, codec2_fft_cpx* inout) { |
| 133 | { | ||
| 134 | |||
| 135 | #ifdef USE_KISS_FFT | 130 | #ifdef USE_KISS_FFT |
| 131 | // decide whether to use the local stack based buffer for in | ||
| 132 | // or to allow kiss_fft to allocate RAM | ||
| 133 | // second part is just to play safe since first method | ||
| 134 | // is much faster and uses less RAM | ||
| 135 | if (cfg->nfft <= 512) { | ||
| 136 | kiss_fft_cpx in[512]; | 136 | kiss_fft_cpx in[512]; |
| 137 | // decide whether to use the local stack based buffer for in | 137 | memcpy(in, inout, cfg->nfft * sizeof(kiss_fft_cpx)); |
| 138 | // or to allow kiss_fft to allocate RAM | 138 | kiss_fft(cfg, in, (kiss_fft_cpx*)inout); |
| 139 | // second part is just to play safe since first method | 139 | } else { |
| 140 | // is much faster and uses less RAM | 140 | kiss_fft(cfg, (kiss_fft_cpx*)inout, (kiss_fft_cpx*)inout); |
| 141 | if (cfg->nfft <= 512) | 141 | } |
| 142 | { | ||
| 143 | memcpy(in,inout,cfg->nfft*sizeof(kiss_fft_cpx)); | ||
| 144 | kiss_fft(cfg, in, (kiss_fft_cpx*)inout); | ||
| 145 | } | ||
| 146 | else | ||
| 147 | { | ||
| 148 | kiss_fft(cfg, (kiss_fft_cpx*)inout, (kiss_fft_cpx*)inout); | ||
| 149 | } | ||
| 150 | #else | 142 | #else |
| 151 | arm_cfft_f32(cfg->instance,(float*)inout,cfg->inverse,1); | 143 | arm_cfft_f32(cfg->instance, (float*)inout, cfg->inverse, 1); |
| 152 | if (cfg->inverse) | 144 | if (cfg->inverse) { |
| 153 | { | 145 | arm_scale_f32((float*)inout, cfg->instance->fftLen, (float*)inout, |
| 154 | arm_scale_f32((float*)inout,cfg->instance->fftLen,(float*)inout,cfg->instance->fftLen*2); | 146 | cfg->instance->fftLen * 2); |
| 155 | } | 147 | } |
| 156 | 148 | ||
| 157 | #endif | 149 | #endif |
| 158 | } | 150 | } |
