ex

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers _kiss_fft_guts.h Source File

_kiss_fft_guts.h

00001 /*
00002 Copyright (c) 2003-2004, Mark Borgerding
00003 
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * 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.
00010     * 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.
00011 
00012 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.
00013 */
00014 
00015 #define MIN(a,b) ((a)<(b) ? (a):(b))
00016 #define MAX(a,b) ((a)>(b) ? (a):(b))
00017 
00018 /* kiss_fft.h
00019    defines kiss_fft_scalar as either short or a float type
00020    and defines
00021    typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
00022 #include "kiss_fft.h"
00023 #include "math_approx.h"
00024 
00025 #define MAXFACTORS 32
00026 /* e.g. an fft of length 128 has 4 factors 
00027  as far as kissfft is concerned
00028  4*4*4*2
00029  */
00030 
00031 struct kiss_fft_state{
00032     int nfft;
00033     int inverse;
00034     int factors[2*MAXFACTORS];
00035     kiss_fft_cpx twiddles[1];
00036 };
00037 
00038 /*
00039   Explanation of macros dealing with complex math:
00040 
00041    C_MUL(m,a,b)         : m = a*b
00042    C_FIXDIV( c , div )  : if a fixed point impl., c /= div. noop otherwise
00043    C_SUB( res, a,b)     : res = a - b
00044    C_SUBFROM( res , a)  : res -= a
00045    C_ADDTO( res , a)    : res += a
00046  * */
00047 #ifdef FIXED_POINT
00048 #include "misc.h"
00049 # define FRACBITS 15
00050 # define SAMPPROD spx_int32_t 
00051 #define SAMP_MAX 32767
00052 
00053 #define SAMP_MIN -SAMP_MAX
00054 
00055 #if defined(CHECK_OVERFLOW)
00056 #  define CHECK_OVERFLOW_OP(a,op,b)  \
00057     if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
00058         fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) );  }
00059 #endif
00060 
00061 
00062 #   define smul(a,b) ( (SAMPPROD)(a)*(b) )
00063 #   define sround( x )  (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
00064 
00065 #   define S_MUL(a,b) sround( smul(a,b) )
00066 
00067 #   define C_MUL(m,a,b) \
00068       do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
00069           (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
00070 
00071 #   define C_MUL4(m,a,b) \
00072                do{ (m).r = PSHR32( smul((a).r,(b).r) - smul((a).i,(b).i),17 ); \
00073                (m).i = PSHR32( smul((a).r,(b).i) + smul((a).i,(b).r),17 ); }while(0)
00074 
00075 #   define DIVSCALAR(x,k) \
00076     (x) = sround( smul(  x, SAMP_MAX/k ) )
00077 
00078 #   define C_FIXDIV(c,div) \
00079     do {    DIVSCALAR( (c).r , div);  \
00080         DIVSCALAR( (c).i  , div); }while (0)
00081 
00082 #   define C_MULBYSCALAR( c, s ) \
00083     do{ (c).r =  sround( smul( (c).r , s ) ) ;\
00084         (c).i =  sround( smul( (c).i , s ) ) ; }while(0)
00085 
00086 #else  /* not FIXED_POINT*/
00087 
00088 #   define S_MUL(a,b) ( (a)*(b) )
00089 #define C_MUL(m,a,b) \
00090     do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
00091         (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
00092 
00093 #define C_MUL4(m,a,b) C_MUL(m,a,b)
00094 
00095 #   define C_FIXDIV(c,div) /* NOOP */
00096 #   define C_MULBYSCALAR( c, s ) \
00097     do{ (c).r *= (s);\
00098         (c).i *= (s); }while(0)
00099 #endif
00100 
00101 #ifndef CHECK_OVERFLOW_OP
00102 #  define CHECK_OVERFLOW_OP(a,op,b) /* noop */
00103 #endif
00104 
00105 #define  C_ADD( res, a,b)\
00106     do { \
00107         CHECK_OVERFLOW_OP((a).r,+,(b).r)\
00108         CHECK_OVERFLOW_OP((a).i,+,(b).i)\
00109         (res).r=(a).r+(b).r;  (res).i=(a).i+(b).i; \
00110     }while(0)
00111 #define  C_SUB( res, a,b)\
00112     do { \
00113         CHECK_OVERFLOW_OP((a).r,-,(b).r)\
00114         CHECK_OVERFLOW_OP((a).i,-,(b).i)\
00115         (res).r=(a).r-(b).r;  (res).i=(a).i-(b).i; \
00116     }while(0)
00117 #define C_ADDTO( res , a)\
00118     do { \
00119         CHECK_OVERFLOW_OP((res).r,+,(a).r)\
00120         CHECK_OVERFLOW_OP((res).i,+,(a).i)\
00121         (res).r += (a).r;  (res).i += (a).i;\
00122     }while(0)
00123 
00124 #define C_SUBFROM( res , a)\
00125     do {\
00126         CHECK_OVERFLOW_OP((res).r,-,(a).r)\
00127         CHECK_OVERFLOW_OP((res).i,-,(a).i)\
00128         (res).r -= (a).r;  (res).i -= (a).i; \
00129     }while(0)
00130 
00131 
00132 #ifdef FIXED_POINT
00133 #  define KISS_FFT_COS(phase)  floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
00134 #  define KISS_FFT_SIN(phase)  floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))
00135 #  define HALF_OF(x) ((x)>>1)
00136 #elif defined(USE_SIMD)
00137 #  define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
00138 #  define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
00139 #  define HALF_OF(x) ((x)*_mm_set1_ps(.5))
00140 #else
00141 #  define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
00142 #  define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
00143 #  define HALF_OF(x) ((x)*.5)
00144 #endif
00145 
00146 #define  kf_cexp(x,phase) \
00147     do{ \
00148         (x)->r = KISS_FFT_COS(phase);\
00149         (x)->i = KISS_FFT_SIN(phase);\
00150     }while(0)
00151 #define  kf_cexp2(x,phase) \
00152                do{ \
00153                (x)->r = spx_cos_norm((phase));\
00154                (x)->i = spx_cos_norm((phase)-32768);\
00155 }while(0)
00156 
00157 
00158 /* a debugging function */
00159 #define pcpx(c)\
00160     fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )