ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:54:45 2017 +0800
Revision:
47:9e361da97763
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 47:9e361da97763 1 /*
TMBOY 47:9e361da97763 2 Copyright (c) 2003-2004, Mark Borgerding
TMBOY 47:9e361da97763 3
TMBOY 47:9e361da97763 4 All rights reserved.
TMBOY 47:9e361da97763 5
TMBOY 47:9e361da97763 6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
TMBOY 47:9e361da97763 7
TMBOY 47:9e361da97763 8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
TMBOY 47:9e361da97763 9 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
TMBOY 47:9e361da97763 10 * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
TMBOY 47:9e361da97763 11
TMBOY 47:9e361da97763 12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
TMBOY 47:9e361da97763 13 */
TMBOY 47:9e361da97763 14
TMBOY 47:9e361da97763 15 #define MIN(a,b) ((a)<(b) ? (a):(b))
TMBOY 47:9e361da97763 16 #define MAX(a,b) ((a)>(b) ? (a):(b))
TMBOY 47:9e361da97763 17
TMBOY 47:9e361da97763 18 /* kiss_fft.h
TMBOY 47:9e361da97763 19 defines kiss_fft_scalar as either short or a float type
TMBOY 47:9e361da97763 20 and defines
TMBOY 47:9e361da97763 21 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
TMBOY 47:9e361da97763 22 #include "kiss_fft.h"
TMBOY 47:9e361da97763 23 #include "math_approx.h"
TMBOY 47:9e361da97763 24
TMBOY 47:9e361da97763 25 #define MAXFACTORS 32
TMBOY 47:9e361da97763 26 /* e.g. an fft of length 128 has 4 factors
TMBOY 47:9e361da97763 27 as far as kissfft is concerned
TMBOY 47:9e361da97763 28 4*4*4*2
TMBOY 47:9e361da97763 29 */
TMBOY 47:9e361da97763 30
TMBOY 47:9e361da97763 31 struct kiss_fft_state{
TMBOY 47:9e361da97763 32 int nfft;
TMBOY 47:9e361da97763 33 int inverse;
TMBOY 47:9e361da97763 34 int factors[2*MAXFACTORS];
TMBOY 47:9e361da97763 35 kiss_fft_cpx twiddles[1];
TMBOY 47:9e361da97763 36 };
TMBOY 47:9e361da97763 37
TMBOY 47:9e361da97763 38 /*
TMBOY 47:9e361da97763 39 Explanation of macros dealing with complex math:
TMBOY 47:9e361da97763 40
TMBOY 47:9e361da97763 41 C_MUL(m,a,b) : m = a*b
TMBOY 47:9e361da97763 42 C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
TMBOY 47:9e361da97763 43 C_SUB( res, a,b) : res = a - b
TMBOY 47:9e361da97763 44 C_SUBFROM( res , a) : res -= a
TMBOY 47:9e361da97763 45 C_ADDTO( res , a) : res += a
TMBOY 47:9e361da97763 46 * */
TMBOY 47:9e361da97763 47 #ifdef FIXED_POINT
TMBOY 47:9e361da97763 48 #include "misc.h"
TMBOY 47:9e361da97763 49 # define FRACBITS 15
TMBOY 47:9e361da97763 50 # define SAMPPROD spx_int32_t
TMBOY 47:9e361da97763 51 #define SAMP_MAX 32767
TMBOY 47:9e361da97763 52
TMBOY 47:9e361da97763 53 #define SAMP_MIN -SAMP_MAX
TMBOY 47:9e361da97763 54
TMBOY 47:9e361da97763 55 #if defined(CHECK_OVERFLOW)
TMBOY 47:9e361da97763 56 # define CHECK_OVERFLOW_OP(a,op,b) \
TMBOY 47:9e361da97763 57 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
TMBOY 47:9e361da97763 58 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
TMBOY 47:9e361da97763 59 #endif
TMBOY 47:9e361da97763 60
TMBOY 47:9e361da97763 61
TMBOY 47:9e361da97763 62 # define smul(a,b) ( (SAMPPROD)(a)*(b) )
TMBOY 47:9e361da97763 63 # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
TMBOY 47:9e361da97763 64
TMBOY 47:9e361da97763 65 # define S_MUL(a,b) sround( smul(a,b) )
TMBOY 47:9e361da97763 66
TMBOY 47:9e361da97763 67 # define C_MUL(m,a,b) \
TMBOY 47:9e361da97763 68 do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
TMBOY 47:9e361da97763 69 (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
TMBOY 47:9e361da97763 70
TMBOY 47:9e361da97763 71 # define C_MUL4(m,a,b) \
TMBOY 47:9e361da97763 72 do{ (m).r = PSHR32( smul((a).r,(b).r) - smul((a).i,(b).i),17 ); \
TMBOY 47:9e361da97763 73 (m).i = PSHR32( smul((a).r,(b).i) + smul((a).i,(b).r),17 ); }while(0)
TMBOY 47:9e361da97763 74
TMBOY 47:9e361da97763 75 # define DIVSCALAR(x,k) \
TMBOY 47:9e361da97763 76 (x) = sround( smul( x, SAMP_MAX/k ) )
TMBOY 47:9e361da97763 77
TMBOY 47:9e361da97763 78 # define C_FIXDIV(c,div) \
TMBOY 47:9e361da97763 79 do { DIVSCALAR( (c).r , div); \
TMBOY 47:9e361da97763 80 DIVSCALAR( (c).i , div); }while (0)
TMBOY 47:9e361da97763 81
TMBOY 47:9e361da97763 82 # define C_MULBYSCALAR( c, s ) \
TMBOY 47:9e361da97763 83 do{ (c).r = sround( smul( (c).r , s ) ) ;\
TMBOY 47:9e361da97763 84 (c).i = sround( smul( (c).i , s ) ) ; }while(0)
TMBOY 47:9e361da97763 85
TMBOY 47:9e361da97763 86 #else /* not FIXED_POINT*/
TMBOY 47:9e361da97763 87
TMBOY 47:9e361da97763 88 # define S_MUL(a,b) ( (a)*(b) )
TMBOY 47:9e361da97763 89 #define C_MUL(m,a,b) \
TMBOY 47:9e361da97763 90 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
TMBOY 47:9e361da97763 91 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
TMBOY 47:9e361da97763 92
TMBOY 47:9e361da97763 93 #define C_MUL4(m,a,b) C_MUL(m,a,b)
TMBOY 47:9e361da97763 94
TMBOY 47:9e361da97763 95 # define C_FIXDIV(c,div) /* NOOP */
TMBOY 47:9e361da97763 96 # define C_MULBYSCALAR( c, s ) \
TMBOY 47:9e361da97763 97 do{ (c).r *= (s);\
TMBOY 47:9e361da97763 98 (c).i *= (s); }while(0)
TMBOY 47:9e361da97763 99 #endif
TMBOY 47:9e361da97763 100
TMBOY 47:9e361da97763 101 #ifndef CHECK_OVERFLOW_OP
TMBOY 47:9e361da97763 102 # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
TMBOY 47:9e361da97763 103 #endif
TMBOY 47:9e361da97763 104
TMBOY 47:9e361da97763 105 #define C_ADD( res, a,b)\
TMBOY 47:9e361da97763 106 do { \
TMBOY 47:9e361da97763 107 CHECK_OVERFLOW_OP((a).r,+,(b).r)\
TMBOY 47:9e361da97763 108 CHECK_OVERFLOW_OP((a).i,+,(b).i)\
TMBOY 47:9e361da97763 109 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
TMBOY 47:9e361da97763 110 }while(0)
TMBOY 47:9e361da97763 111 #define C_SUB( res, a,b)\
TMBOY 47:9e361da97763 112 do { \
TMBOY 47:9e361da97763 113 CHECK_OVERFLOW_OP((a).r,-,(b).r)\
TMBOY 47:9e361da97763 114 CHECK_OVERFLOW_OP((a).i,-,(b).i)\
TMBOY 47:9e361da97763 115 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
TMBOY 47:9e361da97763 116 }while(0)
TMBOY 47:9e361da97763 117 #define C_ADDTO( res , a)\
TMBOY 47:9e361da97763 118 do { \
TMBOY 47:9e361da97763 119 CHECK_OVERFLOW_OP((res).r,+,(a).r)\
TMBOY 47:9e361da97763 120 CHECK_OVERFLOW_OP((res).i,+,(a).i)\
TMBOY 47:9e361da97763 121 (res).r += (a).r; (res).i += (a).i;\
TMBOY 47:9e361da97763 122 }while(0)
TMBOY 47:9e361da97763 123
TMBOY 47:9e361da97763 124 #define C_SUBFROM( res , a)\
TMBOY 47:9e361da97763 125 do {\
TMBOY 47:9e361da97763 126 CHECK_OVERFLOW_OP((res).r,-,(a).r)\
TMBOY 47:9e361da97763 127 CHECK_OVERFLOW_OP((res).i,-,(a).i)\
TMBOY 47:9e361da97763 128 (res).r -= (a).r; (res).i -= (a).i; \
TMBOY 47:9e361da97763 129 }while(0)
TMBOY 47:9e361da97763 130
TMBOY 47:9e361da97763 131
TMBOY 47:9e361da97763 132 #ifdef FIXED_POINT
TMBOY 47:9e361da97763 133 # define KISS_FFT_COS(phase) floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
TMBOY 47:9e361da97763 134 # define KISS_FFT_SIN(phase) floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))
TMBOY 47:9e361da97763 135 # define HALF_OF(x) ((x)>>1)
TMBOY 47:9e361da97763 136 #elif defined(USE_SIMD)
TMBOY 47:9e361da97763 137 # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
TMBOY 47:9e361da97763 138 # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
TMBOY 47:9e361da97763 139 # define HALF_OF(x) ((x)*_mm_set1_ps(.5))
TMBOY 47:9e361da97763 140 #else
TMBOY 47:9e361da97763 141 # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
TMBOY 47:9e361da97763 142 # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
TMBOY 47:9e361da97763 143 # define HALF_OF(x) ((x)*.5)
TMBOY 47:9e361da97763 144 #endif
TMBOY 47:9e361da97763 145
TMBOY 47:9e361da97763 146 #define kf_cexp(x,phase) \
TMBOY 47:9e361da97763 147 do{ \
TMBOY 47:9e361da97763 148 (x)->r = KISS_FFT_COS(phase);\
TMBOY 47:9e361da97763 149 (x)->i = KISS_FFT_SIN(phase);\
TMBOY 47:9e361da97763 150 }while(0)
TMBOY 47:9e361da97763 151 #define kf_cexp2(x,phase) \
TMBOY 47:9e361da97763 152 do{ \
TMBOY 47:9e361da97763 153 (x)->r = spx_cos_norm((phase));\
TMBOY 47:9e361da97763 154 (x)->i = spx_cos_norm((phase)-32768);\
TMBOY 47:9e361da97763 155 }while(0)
TMBOY 47:9e361da97763 156
TMBOY 47:9e361da97763 157
TMBOY 47:9e361da97763 158 /* a debugging function */
TMBOY 47:9e361da97763 159 #define pcpx(c)\
TMBOY 47:9e361da97763 160 fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )