Skip to content

Commit 75c845f

Browse files
committed
Initial commit. Shows in the LCD the FFT
1 parent c51300f commit 75c845f

File tree

14 files changed

+1505
-0
lines changed

14 files changed

+1505
-0
lines changed

include/README

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

include/_kiss_fft_guts.h

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
/* kiss_fft.h
10+
defines kiss_fft_scalar as either short or a float type
11+
and defines
12+
typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
13+
#include "kiss_fft.h"
14+
#include <limits.h>
15+
16+
#define MAXFACTORS 32
17+
/* e.g. an fft of length 128 has 4 factors
18+
as far as kissfft is concerned
19+
4*4*4*2
20+
*/
21+
22+
struct kiss_fft_state{
23+
int nfft;
24+
int inverse;
25+
int factors[2*MAXFACTORS];
26+
kiss_fft_cpx twiddles[1];
27+
};
28+
29+
/*
30+
Explanation of macros dealing with complex math:
31+
32+
C_MUL(m,a,b) : m = a*b
33+
C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
34+
C_SUB( res, a,b) : res = a - b
35+
C_SUBFROM( res , a) : res -= a
36+
C_ADDTO( res , a) : res += a
37+
* */
38+
#ifdef FIXED_POINT
39+
#if (FIXED_POINT==32)
40+
# define FRACBITS 31
41+
# define SAMPPROD int64_t
42+
#define SAMP_MAX 2147483647
43+
#else
44+
# define FRACBITS 15
45+
# define SAMPPROD int32_t
46+
#define SAMP_MAX 32767
47+
#endif
48+
49+
#define SAMP_MIN -SAMP_MAX
50+
51+
#if defined(CHECK_OVERFLOW)
52+
# define CHECK_OVERFLOW_OP(a,op,b) \
53+
if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
54+
fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
55+
#endif
56+
57+
58+
# define smul(a,b) ( (SAMPPROD)(a)*(b) )
59+
# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
60+
61+
# define S_MUL(a,b) sround( smul(a,b) )
62+
63+
# define C_MUL(m,a,b) \
64+
do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
65+
(m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
66+
67+
# define DIVSCALAR(x,k) \
68+
(x) = sround( smul( x, SAMP_MAX/k ) )
69+
70+
# define C_FIXDIV(c,div) \
71+
do { DIVSCALAR( (c).r , div); \
72+
DIVSCALAR( (c).i , div); }while (0)
73+
74+
# define C_MULBYSCALAR( c, s ) \
75+
do{ (c).r = sround( smul( (c).r , s ) ) ;\
76+
(c).i = sround( smul( (c).i , s ) ) ; }while(0)
77+
78+
#else /* not FIXED_POINT*/
79+
80+
# define S_MUL(a,b) ( (a)*(b) )
81+
#define C_MUL(m,a,b) \
82+
do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
83+
(m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
84+
# define C_FIXDIV(c,div) /* NOOP */
85+
# define C_MULBYSCALAR( c, s ) \
86+
do{ (c).r *= (s);\
87+
(c).i *= (s); }while(0)
88+
#endif
89+
90+
#ifndef CHECK_OVERFLOW_OP
91+
# define CHECK_OVERFLOW_OP(a,op,b) /* noop */
92+
#endif
93+
94+
#define C_ADD( res, a,b)\
95+
do { \
96+
CHECK_OVERFLOW_OP((a).r,+,(b).r)\
97+
CHECK_OVERFLOW_OP((a).i,+,(b).i)\
98+
(res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
99+
}while(0)
100+
#define C_SUB( res, a,b)\
101+
do { \
102+
CHECK_OVERFLOW_OP((a).r,-,(b).r)\
103+
CHECK_OVERFLOW_OP((a).i,-,(b).i)\
104+
(res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
105+
}while(0)
106+
#define C_ADDTO( res , a)\
107+
do { \
108+
CHECK_OVERFLOW_OP((res).r,+,(a).r)\
109+
CHECK_OVERFLOW_OP((res).i,+,(a).i)\
110+
(res).r += (a).r; (res).i += (a).i;\
111+
}while(0)
112+
113+
#define C_SUBFROM( res , a)\
114+
do {\
115+
CHECK_OVERFLOW_OP((res).r,-,(a).r)\
116+
CHECK_OVERFLOW_OP((res).i,-,(a).i)\
117+
(res).r -= (a).r; (res).i -= (a).i; \
118+
}while(0)
119+
120+
121+
#ifdef FIXED_POINT
122+
# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
123+
# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
124+
# define HALF_OF(x) ((x)>>1)
125+
#elif defined(USE_SIMD)
126+
# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
127+
# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
128+
# define HALF_OF(x) ((x)*_mm_set1_ps(.5))
129+
#else
130+
# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
131+
# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
132+
# define HALF_OF(x) ((x)*.5)
133+
#endif
134+
135+
#define kf_cexp(x,phase) \
136+
do{ \
137+
(x)->r = KISS_FFT_COS(phase);\
138+
(x)->i = KISS_FFT_SIN(phase);\
139+
}while(0)
140+
141+
142+
/* a debugging function */
143+
#define pcpx(c)\
144+
fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )
145+
146+
147+
#ifdef KISS_FFT_USE_ALLOCA
148+
// define this to allow use of alloca instead of malloc for temporary buffers
149+
// Temporary buffers are used in two case:
150+
// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5
151+
// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform.
152+
#include <alloca.h>
153+
#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes)
154+
#define KISS_FFT_TMP_FREE(ptr)
155+
#else
156+
#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes)
157+
#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr)
158+
#endif

include/kiss_fft.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/*
22+
ATTENTION!
23+
If you would like a :
24+
-- a utility that will handle the caching of fft objects
25+
-- real-only (no imaginary time component ) FFT
26+
-- a multi-dimensional FFT
27+
-- a command-line utility to perform ffts
28+
-- a command-line utility to perform fast-convolution filtering
29+
30+
Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c
31+
in the tools/ directory.
32+
*/
33+
34+
#ifdef USE_SIMD
35+
# include <xmmintrin.h>
36+
# define kiss_fft_scalar __m128
37+
#define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16)
38+
#define KISS_FFT_FREE _mm_free
39+
#else
40+
#define KISS_FFT_MALLOC malloc
41+
#define KISS_FFT_FREE free
42+
#endif
43+
44+
45+
#ifdef FIXED_POINT
46+
#include <sys/types.h>
47+
# if (FIXED_POINT == 32)
48+
# define kiss_fft_scalar int32_t
49+
# else
50+
# define kiss_fft_scalar int16_t
51+
# endif
52+
#else
53+
# ifndef kiss_fft_scalar
54+
/* default is float */
55+
# define kiss_fft_scalar float
56+
# endif
57+
#endif
58+
59+
typedef struct {
60+
kiss_fft_scalar r;
61+
kiss_fft_scalar i;
62+
}kiss_fft_cpx;
63+
64+
typedef struct kiss_fft_state* kiss_fft_cfg;
65+
66+
/*
67+
* kiss_fft_alloc
68+
*
69+
* Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
70+
*
71+
* typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL);
72+
*
73+
* The return value from fft_alloc is a cfg buffer used internally
74+
* by the fft routine or NULL.
75+
*
76+
* If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc.
77+
* The returned value should be free()d when done to avoid memory leaks.
78+
*
79+
* The state can be placed in a user supplied buffer 'mem':
80+
* If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
81+
* then the function places the cfg in mem and the size used in *lenmem
82+
* and returns mem.
83+
*
84+
* If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
85+
* then the function returns NULL and places the minimum cfg
86+
* buffer size in *lenmem.
87+
* */
88+
89+
kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem);
90+
91+
/*
92+
* kiss_fft(cfg,in_out_buf)
93+
*
94+
* Perform an FFT on a complex input buffer.
95+
* for a forward FFT,
96+
* fin should be f[0] , f[1] , ... ,f[nfft-1]
97+
* fout will be F[0] , F[1] , ... ,F[nfft-1]
98+
* Note that each element is complex and can be accessed like
99+
f[k].r and f[k].i
100+
* */
101+
void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
102+
103+
/*
104+
A more generic version of the above function. It reads its input from every Nth sample.
105+
* */
106+
void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride);
107+
108+
/* If kiss_fft_alloc allocated a buffer, it is one contiguous
109+
buffer and can be simply free()d when no longer needed*/
110+
#define kiss_fft_free KISS_FFT_FREE
111+
112+
/*
113+
Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up
114+
your compiler output to call this before you exit.
115+
*/
116+
void kiss_fft_cleanup(void);
117+
118+
119+
/*
120+
* Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5)
121+
*/
122+
int kiss_fft_next_fast_size(int n);
123+
124+
/* for real ffts, we need an even size */
125+
#define kiss_fftr_next_fast_size_real(n) \
126+
(kiss_fft_next_fast_size( ((n)+1)>>1)<<1)
127+
128+
#ifdef __cplusplus
129+
}
130+
#endif
131+
132+
#endif

include/kiss_fftnd.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2003-2004, 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_FFTND_H
10+
#define KISS_FFTND_H
11+
12+
#include "kiss_fft.h"
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
typedef struct kiss_fftnd_state * kiss_fftnd_cfg;
19+
20+
kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem);
21+
void kiss_fftnd(kiss_fftnd_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
22+
23+
#ifdef __cplusplus
24+
}
25+
#endif
26+
#endif

0 commit comments

Comments
 (0)