shun_iwasawa a35b8f
#ifndef KISSFFT_CLASS_HH
shun_iwasawa a35b8f
#include <complex></complex>
shun_iwasawa a35b8f
#include <vector></vector>
shun_iwasawa a35b8f
shun_iwasawa a35b8f
namespace kissfft_utils {
shun_iwasawa a35b8f
shun_iwasawa a35b8f
template <typename t_scalar=""></typename>
shun_iwasawa a35b8f
struct traits
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    typedef T_scalar scalar_type;
shun_iwasawa a35b8f
    typedef std::complex<scalar_type> cpx_type;</scalar_type>
shun_iwasawa a35b8f
    void fill_twiddles( std::complex<t_scalar> * dst ,int nfft,bool inverse)</t_scalar>
shun_iwasawa a35b8f
    {
shun_iwasawa a35b8f
        T_scalar phinc =  (inverse?2:-2)* acos( (T_scalar) -1)  / nfft;
shun_iwasawa a35b8f
        for (int i=0;i
shun_iwasawa a35b8f
            dst[i] = exp( std::complex<t_scalar>(0,i*phinc) );</t_scalar>
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    void prepare(
shun_iwasawa a35b8f
            std::vector< std::complex<t_scalar> > & dst,</t_scalar>
shun_iwasawa a35b8f
            int nfft,bool inverse, 
shun_iwasawa a35b8f
            std::vector<int> & stageRadix, </int>
shun_iwasawa a35b8f
            std::vector<int> & stageRemainder )</int>
shun_iwasawa a35b8f
    {
shun_iwasawa a35b8f
        _twiddles.resize(nfft);
shun_iwasawa a35b8f
        fill_twiddles( &_twiddles[0],nfft,inverse);
shun_iwasawa a35b8f
        dst = _twiddles;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        //factorize
shun_iwasawa a35b8f
        //start factoring out 4's, then 2's, then 3,5,7,9,...
shun_iwasawa a35b8f
        int n= nfft;
shun_iwasawa a35b8f
        int p=4;
shun_iwasawa a35b8f
        do {
shun_iwasawa a35b8f
            while (n % p) {
shun_iwasawa a35b8f
                switch (p) {
shun_iwasawa a35b8f
                    case 4: p = 2; break;
shun_iwasawa a35b8f
                    case 2: p = 3; break;
shun_iwasawa a35b8f
                    default: p += 2; break;
shun_iwasawa a35b8f
                }
shun_iwasawa a35b8f
                if (p*p>n)
shun_iwasawa a35b8f
                    p=n;// no more factors
shun_iwasawa a35b8f
            }
shun_iwasawa a35b8f
            n /= p;
shun_iwasawa a35b8f
            stageRadix.push_back(p);
shun_iwasawa a35b8f
            stageRemainder.push_back(n);
shun_iwasawa a35b8f
        }while(n>1);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    std::vector<cpx_type> _twiddles;</cpx_type>
shun_iwasawa a35b8f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    const cpx_type twiddle(int i) { return _twiddles[i]; }
shun_iwasawa a35b8f
};
shun_iwasawa a35b8f
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
template 
shun_iwasawa a35b8f
         typename T_traits=kissfft_utils::traits<t_scalar> </t_scalar>
shun_iwasawa a35b8f
         >
shun_iwasawa a35b8f
class kissfft
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    public:
shun_iwasawa a35b8f
        typedef T_traits traits_type;
shun_iwasawa a35b8f
        typedef typename traits_type::scalar_type scalar_type;
shun_iwasawa a35b8f
        typedef typename traits_type::cpx_type cpx_type;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        kissfft(int nfft,bool inverse,const traits_type & traits=traits_type() ) 
shun_iwasawa a35b8f
            :_nfft(nfft),_inverse(inverse),_traits(traits)
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            _traits.prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder);
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        void transform(const cpx_type * src , cpx_type * dst)
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            kf_work(0, dst, src, 1,1);
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    private:
shun_iwasawa a35b8f
        void kf_work( int stage,cpx_type * Fout, const cpx_type * f, size_t fstride,size_t in_stride)
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            int p = _stageRadix[stage];
shun_iwasawa a35b8f
            int m = _stageRemainder[stage];
shun_iwasawa a35b8f
            cpx_type * Fout_beg = Fout;
shun_iwasawa a35b8f
            cpx_type * Fout_end = Fout + p*m;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            if (m==1) {
shun_iwasawa a35b8f
                do{
shun_iwasawa a35b8f
                    *Fout = *f;
shun_iwasawa a35b8f
                    f += fstride*in_stride;
shun_iwasawa a35b8f
                }while(++Fout != Fout_end );
shun_iwasawa a35b8f
            }else{
shun_iwasawa a35b8f
                do{
shun_iwasawa a35b8f
                    // recursive call:
shun_iwasawa a35b8f
                    // DFT of size m*p performed by doing
shun_iwasawa a35b8f
                    // p instances of smaller DFTs of size m, 
shun_iwasawa a35b8f
                    // each one takes a decimated version of the input
shun_iwasawa a35b8f
                    kf_work(stage+1, Fout , f, fstride*p,in_stride);
shun_iwasawa a35b8f
                    f += fstride*in_stride;
shun_iwasawa a35b8f
                }while( (Fout += m) != Fout_end );
shun_iwasawa a35b8f
            }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            Fout=Fout_beg;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            // recombine the p smaller DFTs 
shun_iwasawa a35b8f
            switch (p) {
shun_iwasawa a35b8f
                case 2: kf_bfly2(Fout,fstride,m); break;
shun_iwasawa a35b8f
                case 3: kf_bfly3(Fout,fstride,m); break;
shun_iwasawa a35b8f
                case 4: kf_bfly4(Fout,fstride,m); break;
shun_iwasawa a35b8f
                case 5: kf_bfly5(Fout,fstride,m); break;
shun_iwasawa a35b8f
                default: kf_bfly_generic(Fout,fstride,m,p); break;
shun_iwasawa a35b8f
            }
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        // these were #define macros in the original kiss_fft
shun_iwasawa a35b8f
        void C_ADD( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a+b;}
shun_iwasawa a35b8f
        void C_MUL( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a*b;}
shun_iwasawa a35b8f
        void C_SUB( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a-b;}
shun_iwasawa a35b8f
        void C_ADDTO( cpx_type & c,const cpx_type & a) { c+=a;}
shun_iwasawa a35b8f
        void C_FIXDIV( cpx_type & ,int ) {} // NO-OP for float types
shun_iwasawa a35b8f
        scalar_type S_MUL( const scalar_type & a,const scalar_type & b) { return a*b;}
shun_iwasawa a35b8f
        scalar_type HALF_OF( const scalar_type & a) { return a*.5;}
shun_iwasawa a35b8f
        void C_MULBYSCALAR(cpx_type & c,const scalar_type & a) {c*=a;}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        void kf_bfly2( cpx_type * Fout, const size_t fstride, int m)
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            for (int k=0;k
shun_iwasawa a35b8f
                cpx_type t = Fout[m+k] * _traits.twiddle(k*fstride);
shun_iwasawa a35b8f
                Fout[m+k] = Fout[k] - t;
shun_iwasawa a35b8f
                Fout[k] += t;
shun_iwasawa a35b8f
            }
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        void kf_bfly4( cpx_type * Fout, const size_t fstride, const size_t m)
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            cpx_type scratch[7];
shun_iwasawa a35b8f
            int negative_if_inverse = _inverse * -2 +1;
shun_iwasawa a35b8f
            for (size_t k=0;k
shun_iwasawa a35b8f
                scratch[0] = Fout[k+m] * _traits.twiddle(k*fstride);
shun_iwasawa a35b8f
                scratch[1] = Fout[k+2*m] * _traits.twiddle(k*fstride*2);
shun_iwasawa a35b8f
                scratch[2] = Fout[k+3*m] * _traits.twiddle(k*fstride*3);
shun_iwasawa a35b8f
                scratch[5] = Fout[k] - scratch[1];
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                Fout[k] += scratch[1];
shun_iwasawa a35b8f
                scratch[3] = scratch[0] + scratch[2];
shun_iwasawa a35b8f
                scratch[4] = scratch[0] - scratch[2];
shun_iwasawa a35b8f
                scratch[4] = cpx_type( scratch[4].imag()*negative_if_inverse , -scratch[4].real()* negative_if_inverse );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                Fout[k+2*m]  = Fout[k] - scratch[3];
shun_iwasawa a35b8f
                Fout[k] += scratch[3];
shun_iwasawa a35b8f
                Fout[k+m] = scratch[5] + scratch[4];
shun_iwasawa a35b8f
                Fout[k+3*m] = scratch[5] - scratch[4];
shun_iwasawa a35b8f
            }
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        void kf_bfly3( cpx_type * Fout, const size_t fstride, const size_t m)
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            size_t k=m;
shun_iwasawa a35b8f
            const size_t m2 = 2*m;
shun_iwasawa a35b8f
            cpx_type *tw1,*tw2;
shun_iwasawa a35b8f
            cpx_type scratch[5];
shun_iwasawa a35b8f
            cpx_type epi3;
shun_iwasawa a35b8f
            epi3 = _twiddles[fstride*m];
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            tw1=tw2=&_twiddles[0];
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            do{
shun_iwasawa a35b8f
                C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_MUL(scratch[1],Fout[m] , *tw1);
shun_iwasawa a35b8f
                C_MUL(scratch[2],Fout[m2] , *tw2);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_ADD(scratch[3],scratch[1],scratch[2]);
shun_iwasawa a35b8f
                C_SUB(scratch[0],scratch[1],scratch[2]);
shun_iwasawa a35b8f
                tw1 += fstride;
shun_iwasawa a35b8f
                tw2 += fstride*2;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                Fout[m] = cpx_type( Fout->real() - HALF_OF(scratch[3].real() ) , Fout->imag() - HALF_OF(scratch[3].imag() ) );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_MULBYSCALAR( scratch[0] , epi3.imag() );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_ADDTO(*Fout,scratch[3]);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                Fout[m2] = cpx_type(  Fout[m].real() + scratch[0].imag() , Fout[m].imag() - scratch[0].real() );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_ADDTO( Fout[m] , cpx_type( -scratch[0].imag(),scratch[0].real() ) );
shun_iwasawa a35b8f
                ++Fout;
shun_iwasawa a35b8f
            }while(--k);
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        void kf_bfly5( cpx_type * Fout, const size_t fstride, const size_t m)
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            cpx_type *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
shun_iwasawa a35b8f
            size_t u;
shun_iwasawa a35b8f
            cpx_type scratch[13];
shun_iwasawa a35b8f
            cpx_type * twiddles = &_twiddles[0];
shun_iwasawa a35b8f
            cpx_type *tw;
shun_iwasawa a35b8f
            cpx_type ya,yb;
shun_iwasawa a35b8f
            ya = twiddles[fstride*m];
shun_iwasawa a35b8f
            yb = twiddles[fstride*2*m];
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            Fout0=Fout;
shun_iwasawa a35b8f
            Fout1=Fout0+m;
shun_iwasawa a35b8f
            Fout2=Fout0+2*m;
shun_iwasawa a35b8f
            Fout3=Fout0+3*m;
shun_iwasawa a35b8f
            Fout4=Fout0+4*m;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            tw=twiddles;
shun_iwasawa a35b8f
            for ( u=0; u
shun_iwasawa a35b8f
                C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
shun_iwasawa a35b8f
                scratch[0] = *Fout0;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
shun_iwasawa a35b8f
                C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
shun_iwasawa a35b8f
                C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
shun_iwasawa a35b8f
                C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_ADD( scratch[7],scratch[1],scratch[4]);
shun_iwasawa a35b8f
                C_SUB( scratch[10],scratch[1],scratch[4]);
shun_iwasawa a35b8f
                C_ADD( scratch[8],scratch[2],scratch[3]);
shun_iwasawa a35b8f
                C_SUB( scratch[9],scratch[2],scratch[3]);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_ADDTO( *Fout0, scratch[7]);
shun_iwasawa a35b8f
                C_ADDTO( *Fout0, scratch[8]);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                scratch[5] = scratch[0] + cpx_type(
shun_iwasawa a35b8f
                        S_MUL(scratch[7].real(),ya.real() ) + S_MUL(scratch[8].real() ,yb.real() ),
shun_iwasawa a35b8f
                        S_MUL(scratch[7].imag(),ya.real()) + S_MUL(scratch[8].imag(),yb.real())
shun_iwasawa a35b8f
                        );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                scratch[6] =  cpx_type( 
shun_iwasawa a35b8f
                        S_MUL(scratch[10].imag(),ya.imag()) + S_MUL(scratch[9].imag(),yb.imag()),
shun_iwasawa a35b8f
                        -S_MUL(scratch[10].real(),ya.imag()) - S_MUL(scratch[9].real(),yb.imag()) 
shun_iwasawa a35b8f
                        );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_SUB(*Fout1,scratch[5],scratch[6]);
shun_iwasawa a35b8f
                C_ADD(*Fout4,scratch[5],scratch[6]);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                scratch[11] = scratch[0] + 
shun_iwasawa a35b8f
                    cpx_type(
shun_iwasawa a35b8f
                            S_MUL(scratch[7].real(),yb.real()) + S_MUL(scratch[8].real(),ya.real()),
shun_iwasawa a35b8f
                            S_MUL(scratch[7].imag(),yb.real()) + S_MUL(scratch[8].imag(),ya.real())
shun_iwasawa a35b8f
                            );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                scratch[12] = cpx_type(
shun_iwasawa a35b8f
                        -S_MUL(scratch[10].imag(),yb.imag()) + S_MUL(scratch[9].imag(),ya.imag()),
shun_iwasawa a35b8f
                        S_MUL(scratch[10].real(),yb.imag()) - S_MUL(scratch[9].real(),ya.imag())
shun_iwasawa a35b8f
                        );
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                C_ADD(*Fout2,scratch[11],scratch[12]);
shun_iwasawa a35b8f
                C_SUB(*Fout3,scratch[11],scratch[12]);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
shun_iwasawa a35b8f
            }
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        /* perform the butterfly for one stage of a mixed radix FFT */
shun_iwasawa a35b8f
        void kf_bfly_generic(
shun_iwasawa a35b8f
                cpx_type * Fout,
shun_iwasawa a35b8f
                const size_t fstride,
shun_iwasawa a35b8f
                int m,
shun_iwasawa a35b8f
                int p
shun_iwasawa a35b8f
                )
shun_iwasawa a35b8f
        {
shun_iwasawa a35b8f
            int u,k,q1,q;
shun_iwasawa a35b8f
            cpx_type * twiddles = &_twiddles[0];
shun_iwasawa a35b8f
            cpx_type t;
shun_iwasawa a35b8f
            int Norig = _nfft;
shun_iwasawa a35b8f
            cpx_type scratchbuf[p];
shun_iwasawa a35b8f
shun_iwasawa a35b8f
            for ( u=0; u
shun_iwasawa a35b8f
                k=u;
shun_iwasawa a35b8f
                for ( q1=0 ; q1
shun_iwasawa a35b8f
                    scratchbuf[q1] = Fout[ k  ];
shun_iwasawa a35b8f
                    C_FIXDIV(scratchbuf[q1],p);
shun_iwasawa a35b8f
                    k += m;
shun_iwasawa a35b8f
                }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
                k=u;
shun_iwasawa a35b8f
                for ( q1=0 ; q1
shun_iwasawa a35b8f
                    int twidx=0;
shun_iwasawa a35b8f
                    Fout[ k ] = scratchbuf[0];
shun_iwasawa a35b8f
                    for (q=1;q
shun_iwasawa a35b8f
                        twidx += fstride * k;
shun_iwasawa a35b8f
                        if (twidx>=Norig) twidx-=Norig;
shun_iwasawa a35b8f
                        C_MUL(t,scratchbuf[q] , twiddles[twidx] );
shun_iwasawa a35b8f
                        C_ADDTO( Fout[ k ] ,t);
shun_iwasawa a35b8f
                    }
shun_iwasawa a35b8f
                    k += m;
shun_iwasawa a35b8f
                }
shun_iwasawa a35b8f
            }
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        int _nfft;
shun_iwasawa a35b8f
        bool _inverse;
shun_iwasawa a35b8f
        std::vector<cpx_type> _twiddles;</cpx_type>
shun_iwasawa a35b8f
        std::vector<int> _stageRadix;</int>
shun_iwasawa a35b8f
        std::vector<int> _stageRemainder;</int>
shun_iwasawa a35b8f
        traits_type _traits;
shun_iwasawa a35b8f
};
shun_iwasawa a35b8f
#endif