0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
1 #ifndef PECK_FFT_H
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
2 #define PECK_FFT_H
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
3
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
4 #include <stdlib.h>
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
5 #include <stdio.h>
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
6 #include <math.h>
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
7 #include <string.h>
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
8
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
9 #ifdef __cplusplus
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
10 extern "C" {
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
11 #endif
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
12
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
13 #ifdef USE_SIMD
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
14 # include <xmmintrin.h>
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
15 # define peck_fft_scalar __m128
|
2
|
16 #define PECK_FFT_MALLOC(nbytes) _mm_malloc(nbytes, 16)
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
17 #define PECK_FFT_FREE _mm_free
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
18 #else
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
19 #define PECK_FFT_MALLOC malloc
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
20 #define PECK_FFT_FREE free
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
21 #endif
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
22
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
23
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
24 #ifdef FIXED_POINT
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
25 #include <sys/types.h>
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
26 # if (FIXED_POINT == 32)
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
27 # define peck_fft_scalar int32_t
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
28 # else
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
29 # define peck_fft_scalar int16_t
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
30 # endif
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
31 #else
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
32 # ifndef peck_fft_scalar
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
33 /* default is float */
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
34 # define peck_fft_scalar float
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
35 # endif
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
36 #endif
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
37
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
38 typedef struct {
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
39 peck_fft_scalar r;
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
40 peck_fft_scalar i;
|
2
|
41 } peck_fft_cpx;
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
42
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
43 typedef struct peck_fft_state* peck_fft_cfg;
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
44
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
45 /*
|
3
|
46 * peck_fft_alloc()
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
47 *
|
3
|
48 * Initialize a FFT (or IFFT) algorithm's cfg buffer.
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
49 *
|
3
|
50 * typical usage: peck_fft_cfg mycfg = peck_fft_alloc(1024, 0, NULL, NULL);
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
51 *
|
3
|
52 * The return value from fft_alloc() is a cfg buffer used internally
|
|
53 * by the FFT routine or NULL.
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
54 *
|
3
|
55 * If lenmem is NULL, then peck_fft_alloc() will allocate a cfg buffer using malloc.
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
56 * The returned value should be free()d when done to avoid memory leaks.
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
57 *
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
58 * The state can be placed in a user supplied buffer 'mem':
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
59 * If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
60 * then the function places the cfg in mem and the size used in *lenmem
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
61 * and returns mem.
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
62 *
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
63 * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
64 * then the function returns NULL and places the minimum cfg
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
65 * buffer size in *lenmem.
|
3
|
66 */
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
67
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
68 peck_fft_cfg peck_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem);
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
69
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
70 /*
|
3
|
71 * peck_fft(cfg, in_buf, out_buf)
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
72 *
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
73 * Perform an FFT on a complex input buffer.
|
3
|
74 * For a forward FFT,
|
|
75 * fin should be f[0], f[1], ... , f[nfft-1]
|
|
76 * fout will be F[0], F[1], ... , F[nfft-1]
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
77 * Note that each element is complex and can be accessed like
|
3
|
78 * f[k].r and f[k].i
|
|
79 */
|
|
80 void peck_fft(peck_fft_cfg cfg, const peck_fft_cpx *fin, peck_fft_cpx *fout);
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
81
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
82 /* If peck_fft_alloc allocated a buffer, it is one contiguous
|
3
|
83 * buffer and can be simply free()d when no longer needed
|
|
84 */
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
85 #define peck_fft_free free
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
86
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
87 /*
|
3
|
88 * Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up
|
|
89 * your compiler output to call this before you exit.
|
|
90 */
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
91 void peck_fft_cleanup(void);
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
92
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
93 /*
|
3
|
94 * Returns the smallest integer k, such that k>=n and k has only 'fast' factors (2,3,5)
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
95 */
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
96 int peck_fft_next_fast_size(int n);
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
97
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
98 /* for real ffts, we need an even size */
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
99 #define peck_fftr_next_fast_size_real(n) \
|
3
|
100 (peck_fft_next_fast_size(((n)+1) >> 1) << 1)
|
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
101
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
102 #ifdef __cplusplus
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
103 }
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
104 #endif
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
105
|
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff
changeset
|
106 #endif
|