annotate peck_fft.h @ 13:3e85a9101f02 default tip

readme
author Peter Meerwald <p.meerwald@bct-electronic.com>
date Thu, 09 Feb 2012 09:44:39 +0100
parents 2d6c49fcafcb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
4
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
13 #define SIMD_SSE2 1
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
14 #define SIMD_NEON4 2
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
15 #define SIMD_NEON2 3
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
16
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
17 #if USE_SIMD == SIMD_SSE2
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
18 #include <xmmintrin.h>
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
19 #define peck_fft_scalar __m128
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
20 #define PECK_FFT_MALLOC(nbytes) _mm_malloc(nbytes, 16)
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
21 #define PECK_FFT_FREE _mm_free
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
22 #elif USE_SIMD == SIMD_NEON4
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
23 #include <arm_neon.h>
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
24 #define peck_fft_scalar float32x4_t
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
25 #define PECK_FFT_MALLOC malloc
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
26 #define PECK_FFT_FREE free
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
27 #elif USE_SIMD == SIMD_NEON2
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
28 #include <arm_neon.h>
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
29 #define peck_fft_scalar float32x2_t
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
30 #define PECK_FFT_MALLOC malloc
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
31 #define PECK_FFT_FREE free
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
32 #else
4
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
33 #define PECK_FFT_MALLOC malloc
2d6c49fcafcb neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 3
diff changeset
34 #define PECK_FFT_FREE free
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
35 #endif
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
36
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
37
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
38 #ifdef FIXED_POINT
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
39 #include <sys/types.h>
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
40 # if (FIXED_POINT == 32)
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
41 # define peck_fft_scalar int32_t
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
42 # else
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
43 # define peck_fft_scalar int16_t
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
44 # endif
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
45 #else
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
46 # ifndef peck_fft_scalar
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
47 /* default is float */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
48 # define peck_fft_scalar float
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
49 # endif
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
50 #endif
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
51
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
52 typedef struct {
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
53 peck_fft_scalar r;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
54 peck_fft_scalar i;
2
3d08140650d8 cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 0
diff changeset
55 } peck_fft_cpx;
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
56
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
57 typedef struct peck_fft_state* peck_fft_cfg;
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
58
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
59 /*
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
60 * peck_fft_alloc()
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
61 *
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
62 * Initialize a FFT (or IFFT) algorithm's cfg buffer.
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
63 *
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
64 * typical usage: peck_fft_cfg mycfg = peck_fft_alloc(1024, 0, NULL, NULL);
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
65 *
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
66 * The return value from fft_alloc() is a cfg buffer used internally
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
67 * by the FFT routine or NULL.
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
68 *
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
69 * 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
70 * The returned value should be free()d when done to avoid memory leaks.
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
71 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
72 * The state can be placed in a user supplied buffer 'mem':
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
73 * 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
74 * then the function places the cfg in mem and the size used in *lenmem
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
75 * and returns mem.
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
76 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
77 * 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
78 * then the function returns NULL and places the minimum cfg
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
79 * buffer size in *lenmem.
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
80 */
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
81
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
82 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
83
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
84 /*
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
85 * peck_fft(cfg, in_buf, out_buf)
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
86 *
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
87 * Perform an FFT on a complex input buffer.
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
88 * For a forward FFT,
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
89 * fin should be f[0], f[1], ... , f[nfft-1]
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
90 * fout will be F[0], F[1], ... , F[nfft-1]
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
91 * Note that each element is complex and can be accessed like
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
92 * f[k].r and f[k].i
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
93 */
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
94 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
95
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
96 /* If peck_fft_alloc allocated a buffer, it is one contiguous
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
97 * buffer and can be simply free()d when no longer needed
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
98 */
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
99 #define peck_fft_free free
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
100
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
101 /*
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
102 * Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
103 * your compiler output to call this before you exit.
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
104 */
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
105 void peck_fft_cleanup(void);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
106
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
107 /*
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
108 * 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
109 */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
110 int peck_fft_next_fast_size(int n);
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
111
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
112 /* for real ffts, we need an even size */
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
113 #define peck_fftr_next_fast_size_real(n) \
3
3b31bd44a09f cleanup
Peter Meerwald <p.meerwald@bct-electronic.com>
parents: 2
diff changeset
114 (peck_fft_next_fast_size(((n)+1) >> 1) << 1)
0
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
115
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
116 #ifdef __cplusplus
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
117 }
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
118 #endif
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
119
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
diff changeset
120 #endif

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.