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 <stdlib.h></stdlib.h>
shun_iwasawa a35b8f
#include <math.h></math.h>
shun_iwasawa a35b8f
#include <stdio.h></stdio.h>
shun_iwasawa a35b8f
#include <string.h></string.h>
shun_iwasawa a35b8f
#include <unistd.h></unistd.h>
shun_iwasawa a35b8f
shun_iwasawa a35b8f
#include "kiss_fft.h"
shun_iwasawa a35b8f
#include "kiss_fftndr.h"
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static
shun_iwasawa a35b8f
void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kiss_fft_cfg st;
shun_iwasawa a35b8f
    kiss_fft_cpx * buf;
shun_iwasawa a35b8f
    kiss_fft_cpx * bufout;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    buf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
shun_iwasawa a35b8f
    bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
shun_iwasawa a35b8f
    st = kiss_fft_alloc( nfft ,isinverse ,0,0);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) {
shun_iwasawa a35b8f
        kiss_fft( st , buf ,bufout);
shun_iwasawa a35b8f
        fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout );
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    free(st);
shun_iwasawa a35b8f
    free(buf);
shun_iwasawa a35b8f
    free(bufout);
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static
shun_iwasawa a35b8f
void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kiss_fftnd_cfg st;
shun_iwasawa a35b8f
    kiss_fft_cpx *buf;
shun_iwasawa a35b8f
    int dimprod=1,i;
shun_iwasawa a35b8f
    for (i=0;i
shun_iwasawa a35b8f
        dimprod *= dims[i];
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * dimprod);
shun_iwasawa a35b8f
    st = kiss_fftnd_alloc (dims, ndims, isinverse, 0, 0);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    while (fread (buf, sizeof (kiss_fft_cpx) * dimprod, 1, fin) > 0) {
shun_iwasawa a35b8f
        kiss_fftnd (st, buf, buf);
shun_iwasawa a35b8f
        fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    free (st);
shun_iwasawa a35b8f
    free (buf);
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static
shun_iwasawa a35b8f
void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int dimprod=1,i;
shun_iwasawa a35b8f
    kiss_fftndr_cfg st;
shun_iwasawa a35b8f
    void *ibuf;
shun_iwasawa a35b8f
    void *obuf;
shun_iwasawa a35b8f
    int insize,outsize; // size in bytes
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for (i=0;i
shun_iwasawa a35b8f
        dimprod *= dims[i];
shun_iwasawa a35b8f
    insize = outsize = dimprod;
shun_iwasawa a35b8f
    int rdim = dims[ndims-1];
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    if (isinverse)
shun_iwasawa a35b8f
        insize = insize*2*(rdim/2+1)/rdim;
shun_iwasawa a35b8f
    else
shun_iwasawa a35b8f
        outsize = outsize*2*(rdim/2+1)/rdim;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    ibuf = malloc(insize*sizeof(kiss_fft_scalar));
shun_iwasawa a35b8f
    obuf = malloc(outsize*sizeof(kiss_fft_scalar));
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    st = kiss_fftndr_alloc(dims, ndims, isinverse, 0, 0);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    while ( fread (ibuf, sizeof(kiss_fft_scalar), insize,  fin) > 0) {
shun_iwasawa a35b8f
        if (isinverse) {
shun_iwasawa a35b8f
            kiss_fftndri(st,
shun_iwasawa a35b8f
                    (kiss_fft_cpx*)ibuf,
shun_iwasawa a35b8f
                    (kiss_fft_scalar*)obuf);
shun_iwasawa a35b8f
        }else{
shun_iwasawa a35b8f
            kiss_fftndr(st,
shun_iwasawa a35b8f
                    (kiss_fft_scalar*)ibuf,
shun_iwasawa a35b8f
                    (kiss_fft_cpx*)obuf);
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
        fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    free(st);
shun_iwasawa a35b8f
    free(ibuf);
shun_iwasawa a35b8f
    free(obuf);
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static
shun_iwasawa a35b8f
void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kiss_fftr_cfg st;
shun_iwasawa a35b8f
    kiss_fft_scalar * rbuf;
shun_iwasawa a35b8f
    kiss_fft_cpx * cbuf;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    rbuf = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar) * nfft );
shun_iwasawa a35b8f
    cbuf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * (nfft/2+1) );
shun_iwasawa a35b8f
    st = kiss_fftr_alloc( nfft ,isinverse ,0,0);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    if (isinverse==0) {
shun_iwasawa a35b8f
        while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) {
shun_iwasawa a35b8f
            kiss_fftr( st , rbuf ,cbuf);
shun_iwasawa a35b8f
            fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout );
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
    }else{
shun_iwasawa a35b8f
        while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) {
shun_iwasawa a35b8f
            kiss_fftri( st , cbuf ,rbuf);
shun_iwasawa a35b8f
            fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout );
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    free(st);
shun_iwasawa a35b8f
    free(rbuf);
shun_iwasawa a35b8f
    free(cbuf);
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static
shun_iwasawa a35b8f
int get_dims(char * arg,int * dims)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    char *p0;
shun_iwasawa a35b8f
    int ndims=0;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    do{
shun_iwasawa a35b8f
        p0 = strchr(arg,',');
shun_iwasawa a35b8f
        if (p0)
shun_iwasawa a35b8f
            *p0++ = '\0';
shun_iwasawa a35b8f
        dims[ndims++] = atoi(arg);
shun_iwasawa a35b8f
//         fprintf(stderr,"dims[%d] = %d\n",ndims-1,dims[ndims-1]); 
shun_iwasawa a35b8f
        arg = p0;
shun_iwasawa a35b8f
    }while (p0);
shun_iwasawa a35b8f
    return ndims;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
int main(int argc,char ** argv)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int isinverse=0;
shun_iwasawa a35b8f
    int isreal=0;
shun_iwasawa a35b8f
    FILE *fin=stdin;
shun_iwasawa a35b8f
    FILE *fout=stdout;
shun_iwasawa a35b8f
    int ndims=1;
shun_iwasawa a35b8f
    int dims[32];
shun_iwasawa a35b8f
    dims[0] = 1024; /*default fft size*/
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    while (1) {
shun_iwasawa a35b8f
        int c=getopt(argc,argv,"n:iR");
shun_iwasawa a35b8f
        if (c==-1) break;
shun_iwasawa a35b8f
        switch (c) {
shun_iwasawa a35b8f
            case 'n':
shun_iwasawa a35b8f
                ndims = get_dims(optarg,dims);
shun_iwasawa a35b8f
                break;
shun_iwasawa a35b8f
            case 'i':isinverse=1;break;
shun_iwasawa a35b8f
            case 'R':isreal=1;break;
shun_iwasawa a35b8f
            case '?':
shun_iwasawa a35b8f
                     fprintf(stderr,"usage options:\n"
shun_iwasawa a35b8f
                            "\t-n d1[,d2,d3...]: fft dimension(s)\n"
shun_iwasawa a35b8f
                            "\t-i : inverse\n"
shun_iwasawa a35b8f
                            "\t-R : real input samples, not complex\n");
shun_iwasawa a35b8f
                     exit (1);
shun_iwasawa a35b8f
            default:fprintf(stderr,"bad %c\n",c);break;
shun_iwasawa a35b8f
        }
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    if ( optind < argc ) {
shun_iwasawa a35b8f
        if (strcmp("-",argv[optind]) !=0)
shun_iwasawa a35b8f
            fin = fopen(argv[optind],"rb");
shun_iwasawa a35b8f
        ++optind;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    if ( optind < argc ) {
shun_iwasawa a35b8f
        if ( strcmp("-",argv[optind]) !=0 ) 
shun_iwasawa a35b8f
            fout = fopen(argv[optind],"wb");
shun_iwasawa a35b8f
        ++optind;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    if (ndims==1) {
shun_iwasawa a35b8f
        if (isreal)
shun_iwasawa a35b8f
            fft_file_real(fin,fout,dims[0],isinverse);
shun_iwasawa a35b8f
        else
shun_iwasawa a35b8f
            fft_file(fin,fout,dims[0],isinverse);
shun_iwasawa a35b8f
    }else{
shun_iwasawa a35b8f
        if (isreal)
shun_iwasawa a35b8f
            fft_filend_real(fin,fout,dims,ndims,isinverse);
shun_iwasawa a35b8f
        else
shun_iwasawa a35b8f
            fft_filend(fin,fout,dims,ndims,isinverse);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    if (fout!=stdout) fclose(fout);
shun_iwasawa a35b8f
    if (fin!=stdin) fclose(fin);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    return 0;
shun_iwasawa a35b8f
}