Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 1 | #ifndef KISS_FFT_H |
| 2 | #define KISS_FFT_H |
| 3 | |
| 4 | #include <stdlib.h> |
| 5 | #include <math.h> |
| 6 | #include "arch.h" |
| 7 | |
| 8 | #ifdef __cplusplus |
| 9 | extern "C" { |
| 10 | #endif |
| 11 | |
| 12 | /* |
| 13 | ATTENTION! |
| 14 | If you would like a : |
| 15 | -- a utility that will handle the caching of fft objects |
| 16 | -- real-only (no imaginary time component ) FFT |
| 17 | -- a multi-dimensional FFT |
| 18 | -- a command-line utility to perform ffts |
| 19 | -- a command-line utility to perform fast-convolution filtering |
| 20 | |
| 21 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c |
| 22 | in the tools/ directory. |
| 23 | */ |
| 24 | |
| 25 | #ifdef USE_SIMD |
| 26 | # include <xmmintrin.h> |
| 27 | # define kiss_fft_scalar __m128 |
| 28 | #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes) |
| 29 | #else |
| 30 | #define KISS_FFT_MALLOC celt_alloc |
| 31 | #endif |
| 32 | |
| 33 | |
| 34 | #ifdef FIXED_POINT |
| 35 | #include "arch.h" |
Jean-Marc Valin | 25649c1 | 2008-02-22 16:19:39 +1100 | [diff] [blame] | 36 | #ifdef DOUBLE_PRECISION |
| 37 | # define kiss_fft_scalar celt_int32_t |
Jean-Marc Valin | 9ced5d0 | 2008-02-24 13:46:30 +1100 | [diff] [blame] | 38 | # define kiss_twiddle_scalar celt_int32_t |
Jean-Marc Valin | f93747c | 2008-03-05 17:20:30 +1100 | [diff] [blame] | 39 | # define KF_SUFFIX _celt_double |
Jean-Marc Valin | 25649c1 | 2008-02-22 16:19:39 +1100 | [diff] [blame] | 40 | #else |
Jean-Marc Valin | af8402e | 2008-02-22 12:13:59 +1100 | [diff] [blame] | 41 | # define kiss_fft_scalar celt_int16_t |
Jean-Marc Valin | 9ced5d0 | 2008-02-24 13:46:30 +1100 | [diff] [blame] | 42 | # define kiss_twiddle_scalar celt_int16_t |
Jean-Marc Valin | f93747c | 2008-03-05 17:20:30 +1100 | [diff] [blame] | 43 | # define KF_SUFFIX _celt_single |
Jean-Marc Valin | 25649c1 | 2008-02-22 16:19:39 +1100 | [diff] [blame] | 44 | #endif |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 45 | #else |
| 46 | # ifndef kiss_fft_scalar |
| 47 | /* default is float */ |
| 48 | # define kiss_fft_scalar float |
Jean-Marc Valin | 9ced5d0 | 2008-02-24 13:46:30 +1100 | [diff] [blame] | 49 | # define kiss_twiddle_scalar float |
Jean-Marc Valin | f93747c | 2008-03-05 17:20:30 +1100 | [diff] [blame] | 50 | # define KF_SUFFIX _celt_single |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 51 | # endif |
| 52 | #endif |
| 53 | |
Jean-Marc Valin | 300cb5b | 2008-03-03 17:52:11 +1100 | [diff] [blame] | 54 | |
| 55 | /* This adds a suffix to all the kiss_fft functions so we |
| 56 | can easily link with more than one copy of the fft */ |
Jean-Marc Valin | 300cb5b | 2008-03-03 17:52:11 +1100 | [diff] [blame] | 57 | #define CAT_SUFFIX(a,b) a ## b |
| 58 | #define SUF(a,b) CAT_SUFFIX(a, b) |
| 59 | |
| 60 | #define kiss_fft_alloc SUF(kiss_fft_alloc,KF_SUFFIX) |
| 61 | #define kf_work SUF(kf_work,KF_SUFFIX) |
| 62 | #define ki_work SUF(ki_work,KF_SUFFIX) |
| 63 | #define kiss_fft SUF(kiss_fft,KF_SUFFIX) |
| 64 | #define kiss_ifft SUF(kiss_ifft,KF_SUFFIX) |
| 65 | #define kiss_fft_stride SUF(kiss_fft_stride,KF_SUFFIX) |
| 66 | #define kiss_ifft_stride SUF(kiss_ifft_stride,KF_SUFFIX) |
| 67 | |
| 68 | |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 69 | typedef struct { |
| 70 | kiss_fft_scalar r; |
| 71 | kiss_fft_scalar i; |
| 72 | }kiss_fft_cpx; |
| 73 | |
Jean-Marc Valin | 9ced5d0 | 2008-02-24 13:46:30 +1100 | [diff] [blame] | 74 | typedef struct { |
| 75 | kiss_twiddle_scalar r; |
| 76 | kiss_twiddle_scalar i; |
| 77 | }kiss_twiddle_cpx; |
| 78 | |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 79 | typedef struct kiss_fft_state* kiss_fft_cfg; |
| 80 | |
Jean-Marc Valin | e6b7465 | 2008-02-20 18:01:08 +1100 | [diff] [blame] | 81 | /** |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 82 | * kiss_fft_alloc |
| 83 | * |
| 84 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. |
| 85 | * |
| 86 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); |
| 87 | * |
| 88 | * The return value from fft_alloc is a cfg buffer used internally |
| 89 | * by the fft routine or NULL. |
| 90 | * |
| 91 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. |
| 92 | * The returned value should be free()d when done to avoid memory leaks. |
| 93 | * |
| 94 | * The state can be placed in a user supplied buffer 'mem': |
| 95 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, |
| 96 | * then the function places the cfg in mem and the size used in *lenmem |
| 97 | * and returns mem. |
| 98 | * |
| 99 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), |
| 100 | * then the function returns NULL and places the minimum cfg |
| 101 | * buffer size in *lenmem. |
| 102 | * */ |
| 103 | |
Jean-Marc Valin | 6211c90 | 2008-02-08 14:21:20 +1100 | [diff] [blame] | 104 | kiss_fft_cfg kiss_fft_alloc(int nfft,void * mem,size_t * lenmem); |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 105 | |
Jean-Marc Valin | 3794264 | 2008-03-01 22:55:27 +1100 | [diff] [blame] | 106 | void kf_work(kiss_fft_cpx * Fout,const kiss_fft_cpx * f,const size_t fstride, |
| 107 | int in_stride,int * factors,const kiss_fft_cfg st,int N,int s2,int m2); |
| 108 | |
Jean-Marc Valin | 42667b0 | 2008-02-25 09:47:25 +1100 | [diff] [blame] | 109 | /** Internal function. Can be useful when you want to do the bit-reversing yourself */ |
| 110 | void ki_work(kiss_fft_cpx * Fout, const kiss_fft_cpx * f, const size_t fstride, |
| 111 | int in_stride,int * factors,const kiss_fft_cfg st,int N,int s2,int m2); |
| 112 | |
Jean-Marc Valin | e6b7465 | 2008-02-20 18:01:08 +1100 | [diff] [blame] | 113 | /** |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 114 | * kiss_fft(cfg,in_out_buf) |
| 115 | * |
| 116 | * Perform an FFT on a complex input buffer. |
| 117 | * for a forward FFT, |
| 118 | * fin should be f[0] , f[1] , ... ,f[nfft-1] |
| 119 | * fout will be F[0] , F[1] , ... ,F[nfft-1] |
| 120 | * Note that each element is complex and can be accessed like |
| 121 | f[k].r and f[k].i |
| 122 | * */ |
| 123 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); |
Jean-Marc Valin | 6211c90 | 2008-02-08 14:21:20 +1100 | [diff] [blame] | 124 | void kiss_ifft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 125 | |
Jean-Marc Valin | e6b7465 | 2008-02-20 18:01:08 +1100 | [diff] [blame] | 126 | /** |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 127 | A more generic version of the above function. It reads its input from every Nth sample. |
| 128 | * */ |
| 129 | void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); |
Jean-Marc Valin | 6211c90 | 2008-02-08 14:21:20 +1100 | [diff] [blame] | 130 | void kiss_ifft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 131 | |
Jean-Marc Valin | e6b7465 | 2008-02-20 18:01:08 +1100 | [diff] [blame] | 132 | /** If kiss_fft_alloc allocated a buffer, it is one contiguous |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 133 | buffer and can be simply free()d when no longer needed*/ |
| 134 | #define kiss_fft_free celt_free |
| 135 | |
Jean-Marc Valin | 4d0a7d0 | 2008-02-07 12:24:26 +1100 | [diff] [blame] | 136 | |
| 137 | #ifdef __cplusplus |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | #endif |