shun_iwasawa a35b8f
/*
shun_iwasawa a35b8f
Copyright (c) 2003-2004, Mark Borgerding
shun_iwasawa a35b8f
shun_iwasawa a35b8f
All rights reserved.
shun_iwasawa a35b8f
shun_iwasawa a35b8f
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
shun_iwasawa a35b8f
    * 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.
shun_iwasawa a35b8f
    * 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.
shun_iwasawa a35b8f
shun_iwasawa a35b8f
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.
shun_iwasawa a35b8f
*/
shun_iwasawa a35b8f
shun_iwasawa a35b8f
#include "kiss_fftndr.h"
shun_iwasawa a35b8f
#include "_kiss_fft_guts.h"
shun_iwasawa a35b8f
#define MAX(x,y) ( ( (x)<(y) )?(y):(x) )
shun_iwasawa a35b8f
shun_iwasawa a35b8f
struct kiss_fftndr_state
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int dimReal;
shun_iwasawa a35b8f
    int dimOther;
shun_iwasawa a35b8f
    kiss_fftr_cfg cfg_r;
shun_iwasawa a35b8f
    kiss_fftnd_cfg cfg_nd;
shun_iwasawa a35b8f
    void * tmpbuf;
shun_iwasawa a35b8f
};
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static int prod(const int *dims, int ndims)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int x=1;
shun_iwasawa a35b8f
    while (ndims--) 
shun_iwasawa a35b8f
        x *= *dims++;
shun_iwasawa a35b8f
    return x;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kiss_fftndr_cfg st = NULL;
shun_iwasawa a35b8f
    size_t nr=0 , nd=0,ntmp=0;
shun_iwasawa a35b8f
    int dimReal = dims[ndims-1];
shun_iwasawa a35b8f
    int dimOther = prod(dims,ndims-1);
shun_iwasawa a35b8f
    size_t memneeded;
shun_iwasawa a35b8f
    
shun_iwasawa a35b8f
    (void)kiss_fftr_alloc(dimReal,inverse_fft,NULL,&nr);
shun_iwasawa a35b8f
    (void)kiss_fftnd_alloc(dims,ndims-1,inverse_fft,NULL,&nd);
shun_iwasawa a35b8f
    ntmp =
shun_iwasawa a35b8f
        MAX( 2*dimOther , dimReal+2) * sizeof(kiss_fft_scalar)  // freq buffer for one pass
shun_iwasawa a35b8f
        + dimOther*(dimReal+2) * sizeof(kiss_fft_scalar);  // large enough to hold entire input in case of in-place
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    memneeded = sizeof( struct kiss_fftndr_state ) + nr + nd + ntmp;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    if (lenmem==NULL) {
shun_iwasawa a35b8f
        st = (kiss_fftndr_cfg) malloc(memneeded);
shun_iwasawa a35b8f
    }else{
shun_iwasawa a35b8f
        if (*lenmem >= memneeded)
shun_iwasawa a35b8f
            st = (kiss_fftndr_cfg)mem;
shun_iwasawa a35b8f
        *lenmem = memneeded; 
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    if (st==NULL)
shun_iwasawa a35b8f
        return NULL;
shun_iwasawa a35b8f
    memset( st , 0 , memneeded);
shun_iwasawa a35b8f
    
shun_iwasawa a35b8f
    st->dimReal = dimReal;
shun_iwasawa a35b8f
    st->dimOther = dimOther;
shun_iwasawa a35b8f
    st->cfg_r = kiss_fftr_alloc( dimReal,inverse_fft,st+1,&nr);
shun_iwasawa a35b8f
    st->cfg_nd = kiss_fftnd_alloc(dims,ndims-1,inverse_fft, ((char*) st->cfg_r)+nr,&nd);
shun_iwasawa a35b8f
    st->tmpbuf = (char*)st->cfg_nd + nd;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    return st;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
void kiss_fftndr(kiss_fftndr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int k1,k2;
shun_iwasawa a35b8f
    int dimReal = st->dimReal;
shun_iwasawa a35b8f
    int dimOther = st->dimOther;
shun_iwasawa a35b8f
    int nrbins = dimReal/2+1;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf; 
shun_iwasawa a35b8f
    kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    // timedata is N0 x N1 x ... x Nk real
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    // take a real chunk of data, fft it and place the output at correct intervals
shun_iwasawa a35b8f
    for (k1=0;k1
shun_iwasawa a35b8f
        kiss_fftr( st->cfg_r, timedata + k1*dimReal , tmp1 ); // tmp1 now holds nrbins complex points
shun_iwasawa a35b8f
        for (k2=0;k2
shun_iwasawa a35b8f
           tmp2[ k2*dimOther+k1 ] = tmp1[k2];
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for (k2=0;k2
shun_iwasawa a35b8f
        kiss_fftnd(st->cfg_nd, tmp2+k2*dimOther, tmp1);  // tmp1 now holds dimOther complex points
shun_iwasawa a35b8f
        for (k1=0;k1
shun_iwasawa a35b8f
            freqdata[ k1*(nrbins) + k2] = tmp1[k1];
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
void kiss_fftndri(kiss_fftndr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int k1,k2;
shun_iwasawa a35b8f
    int dimReal = st->dimReal;
shun_iwasawa a35b8f
    int dimOther = st->dimOther;
shun_iwasawa a35b8f
    int nrbins = dimReal/2+1;
shun_iwasawa a35b8f
    kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf; 
shun_iwasawa a35b8f
    kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for (k2=0;k2
shun_iwasawa a35b8f
        for (k1=0;k1
shun_iwasawa a35b8f
            tmp1[k1] = freqdata[ k1*(nrbins) + k2 ];
shun_iwasawa a35b8f
        kiss_fftnd(st->cfg_nd, tmp1, tmp2+k2*dimOther);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for (k1=0;k1
shun_iwasawa a35b8f
        for (k2=0;k2
shun_iwasawa a35b8f
            tmp1[k2] = tmp2[ k2*dimOther+k1 ];
shun_iwasawa a35b8f
        kiss_fftri( st->cfg_r,tmp1,timedata + k1*dimReal);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
}