1 /*
2  * codec2_fft.h
3  *
4  *  Created on: 17.09.2016
5  *      Author: danilo
6  */
7 
8 #ifndef DRIVERS_FREEDV_CODEC2_FFT_H_
9 #define DRIVERS_FREEDV_CODEC2_FFT_H_
10 
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <math.h>
16 
17 #ifndef FDV_ARM_MATH
18   #define USE_KISS_FFT
19 #else
20   #include "arm_math.h"
21   #include "arm_const_structs.h"
22 #endif
23 
24 #include "defines.h"
25 #include "comp.h"
26 
27 
28 typedef COMP    codec2_fft_cpx;
29 #include "kiss_fftr.h"
30 
31 #ifdef USE_KISS_FFT
32     #include "kiss_fft.h"
33     typedef kiss_fftr_cfg codec2_fftr_cfg;
34     typedef kiss_fft_cfg codec2_fft_cfg;
35     typedef kiss_fft_scalar codec2_fft_scalar;
36 #else
37   typedef float32_t codec2_fft_scalar;
38   typedef struct {
39       arm_rfft_fast_instance_f32* instance;
40       int inverse;
41   } codec2_fftr_struct;
42 
43   typedef codec2_fftr_struct* codec2_fftr_cfg;
44 
45   typedef struct {
46         const arm_cfft_instance_f32* instance;
47         int inverse;
48     } codec2_fft_struct;
49   typedef codec2_fft_struct* codec2_fft_cfg;
50 #endif
51 
52 
53 
codec2_fftr(codec2_fftr_cfg cfg,codec2_fft_scalar * in,codec2_fft_cpx * out)54 static inline void codec2_fftr(codec2_fftr_cfg cfg, codec2_fft_scalar* in, codec2_fft_cpx* out)
55 {
56 
57 #ifdef USE_KISS_FFT
58       kiss_fftr(cfg, in, (kiss_fft_cpx*)out);
59 #else
60     arm_rfft_fast_f32(cfg->instance,in,(float*)out,cfg->inverse);
61     out->imag = 0; // remove out[FFT_ENC/2]->real stored in out[0].imag
62 #endif
63 }
64 
codec2_fftri(codec2_fftr_cfg cfg,codec2_fft_cpx * in,codec2_fft_scalar * out)65 static inline void codec2_fftri(codec2_fftr_cfg cfg, codec2_fft_cpx* in, codec2_fft_scalar* out)
66 {
67 #ifdef USE_KISS_FFT
68       kiss_fftri(cfg, (kiss_fft_cpx*)in, out);
69 #else
70     arm_rfft_fast_f32(cfg->instance,(float*)in,out,cfg->inverse);
71     // arm_scale_f32(out,cfg->instance->fftLenRFFT,out,cfg->instance->fftLenRFFT);
72 #endif
73 
74 }
75 
76 codec2_fft_cfg codec2_fft_alloc(int nfft, int inverse_fft, void* mem, size_t* lenmem);
77 codec2_fftr_cfg codec2_fftr_alloc(int nfft, int inverse_fft, void* mem, size_t* lenmem);
78 void codec2_fft_free(codec2_fft_cfg cfg);
79 void codec2_fftr_free(codec2_fftr_cfg cfg);
80 
81 
codec2_fft(codec2_fft_cfg cfg,codec2_fft_cpx * in,codec2_fft_cpx * out)82 static inline void codec2_fft(codec2_fft_cfg cfg, codec2_fft_cpx* in, codec2_fft_cpx* out)
83 {
84 
85 #ifdef USE_KISS_FFT
86       kiss_fft(cfg, (kiss_fft_cpx*)in, (kiss_fft_cpx*)out);
87 #else
88     memcpy(out,in,cfg->instance->fftLen*2*sizeof(float));
89     arm_cfft_f32(cfg->instance,(float*)out,cfg->inverse, 1);
90     // TODO: this is not nice, but for now required to keep changes minimal
91     // however, since main goal is to reduce the memory usage
92     // we should convert to an in place interface
93     // on PC like platforms the overhead of using the "inplace" kiss_fft calls
94     // is neglectable compared to the gain in memory usage on STM32 platforms
95     if (cfg->inverse)
96     {
97         arm_scale_f32((float*)out,cfg->instance->fftLen,(float*)out,cfg->instance->fftLen*2);
98     }
99 #endif
100 }
101 
102 void codec2_fft_inplace(codec2_fft_cfg cfg, codec2_fft_cpx* inout);
103 
104 
105 #endif
106