1 | /* |
2 | * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. |
3 | * This file is part of KISS FFT - https://github.com/mborgerding/kissfft |
4 | * |
5 | * SPDX-License-Identifier: BSD-3-Clause |
6 | * See COPYING file for more information. |
7 | */ |
8 | |
9 | #ifndef KISS_FFT_H |
10 | #define KISS_FFT_H |
11 | |
12 | #include <stdlib.h> |
13 | #include <stdio.h> |
14 | #include <math.h> |
15 | #include <string.h> |
16 | |
17 | // Define KISS_FFT_SHARED macro to properly export symbols |
18 | #ifdef KISS_FFT_SHARED |
19 | # ifdef _WIN32 |
20 | # ifdef KISS_FFT_BUILD |
21 | # define KISS_FFT_API __declspec(dllexport) |
22 | # else |
23 | # define KISS_FFT_API __declspec(dllimport) |
24 | # endif |
25 | # else |
26 | # define KISS_FFT_API __attribute__ ((visibility ("default"))) |
27 | # endif |
28 | #else |
29 | # define KISS_FFT_API |
30 | #endif |
31 | |
32 | #ifdef __cplusplus |
33 | extern "C" { |
34 | #endif |
35 | |
36 | /* |
37 | ATTENTION! |
38 | If you would like a : |
39 | -- a utility that will handle the caching of fft objects |
40 | -- real-only (no imaginary time component ) FFT |
41 | -- a multi-dimensional FFT |
42 | -- a command-line utility to perform ffts |
43 | -- a command-line utility to perform fast-convolution filtering |
44 | |
45 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c |
46 | in the tools/ directory. |
47 | */ |
48 | |
49 | /* User may override KISS_FFT_MALLOC and/or KISS_FFT_FREE. */ |
50 | #ifdef USE_SIMD |
51 | # include <xmmintrin.h> |
52 | # define kiss_fft_scalar __m128 |
53 | # ifndef KISS_FFT_MALLOC |
54 | # define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) |
55 | # define KISS_FFT_ALIGN_CHECK(ptr) |
56 | # define KISS_FFT_ALIGN_SIZE_UP(size) ((size + 15UL) & ~0xFUL) |
57 | # endif |
58 | # ifndef KISS_FFT_FREE |
59 | # define KISS_FFT_FREE _mm_free |
60 | # endif |
61 | #else |
62 | # define KISS_FFT_ALIGN_CHECK(ptr) |
63 | # define KISS_FFT_ALIGN_SIZE_UP(size) (size) |
64 | # ifndef KISS_FFT_MALLOC |
65 | # define KISS_FFT_MALLOC malloc |
66 | # endif |
67 | # ifndef KISS_FFT_FREE |
68 | # define KISS_FFT_FREE free |
69 | # endif |
70 | #endif |
71 | |
72 | |
73 | #ifdef FIXED_POINT |
74 | #include <stdint.h> |
75 | # if (FIXED_POINT == 32) |
76 | # define kiss_fft_scalar int32_t |
77 | # else |
78 | # define kiss_fft_scalar int16_t |
79 | # endif |
80 | #else |
81 | # ifndef kiss_fft_scalar |
82 | /* default is float */ |
83 | # define kiss_fft_scalar float |
84 | # endif |
85 | #endif |
86 | |
87 | typedef struct { |
88 | kiss_fft_scalar r; |
89 | kiss_fft_scalar i; |
90 | }kiss_fft_cpx; |
91 | |
92 | typedef struct kiss_fft_state* kiss_fft_cfg; |
93 | |
94 | /* |
95 | * kiss_fft_alloc |
96 | * |
97 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. |
98 | * |
99 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); |
100 | * |
101 | * The return value from fft_alloc is a cfg buffer used internally |
102 | * by the fft routine or NULL. |
103 | * |
104 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. |
105 | * The returned value should be free()d when done to avoid memory leaks. |
106 | * |
107 | * The state can be placed in a user supplied buffer 'mem': |
108 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, |
109 | * then the function places the cfg in mem and the size used in *lenmem |
110 | * and returns mem. |
111 | * |
112 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), |
113 | * then the function returns NULL and places the minimum cfg |
114 | * buffer size in *lenmem. |
115 | * */ |
116 | |
117 | kiss_fft_cfg KISS_FFT_API kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); |
118 | |
119 | /* |
120 | * kiss_fft(cfg,in_out_buf) |
121 | * |
122 | * Perform an FFT on a complex input buffer. |
123 | * for a forward FFT, |
124 | * fin should be f[0] , f[1] , ... ,f[nfft-1] |
125 | * fout will be F[0] , F[1] , ... ,F[nfft-1] |
126 | * Note that each element is complex and can be accessed like |
127 | f[k].r and f[k].i |
128 | * */ |
129 | void KISS_FFT_API kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); |
130 | |
131 | /* |
132 | A more generic version of the above function. It reads its input from every Nth sample. |
133 | * */ |
134 | void KISS_FFT_API kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); |
135 | |
136 | /* If kiss_fft_alloc allocated a buffer, it is one contiguous |
137 | buffer and can be simply free()d when no longer needed*/ |
138 | #define kiss_fft_free KISS_FFT_FREE |
139 | |
140 | /* |
141 | Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up |
142 | your compiler output to call this before you exit. |
143 | */ |
144 | void KISS_FFT_API kiss_fft_cleanup(void); |
145 | |
146 | |
147 | /* |
148 | * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) |
149 | */ |
150 | int KISS_FFT_API kiss_fft_next_fast_size(int n); |
151 | |
152 | /* for real ffts, we need an even size */ |
153 | #define kiss_fftr_next_fast_size_real(n) \ |
154 | (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) |
155 | |
156 | #ifdef __cplusplus |
157 | } |
158 | #endif |
159 | |
160 | #endif |
161 | |