2 Copyright (c) 2003-2004, Mark Borgerding
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
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.
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.
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.
22 #include "kiss_fftndr.h"
25 void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse)
29 kiss_fft_cpx * bufout;
31 buf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
32 bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
33 st = kiss_fft_alloc( nfft ,isinverse ,0,0);
35 while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) {
36 kiss_fft( st , buf ,bufout);
37 fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout );
45 void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
53 buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * dimprod);
54 st = kiss_fftnd_alloc (dims, ndims, isinverse, 0, 0);
56 while (fread (buf, sizeof (kiss_fft_cpx) * dimprod, 1, fin) > 0) {
57 kiss_fftnd (st, buf, buf);
58 fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout);
67 void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
73 int insize,outsize; // size in bytes
77 insize = outsize = dimprod;
78 int rdim = dims[ndims-1];
81 insize = insize*2*(rdim/2+1)/rdim;
83 outsize = outsize*2*(rdim/2+1)/rdim;
85 ibuf = malloc(insize*sizeof(kiss_fft_scalar));
86 obuf = malloc(outsize*sizeof(kiss_fft_scalar));
88 st = kiss_fftndr_alloc(dims, ndims, isinverse, 0, 0);
90 while ( fread (ibuf, sizeof(kiss_fft_scalar), insize, fin) > 0) {
94 (kiss_fft_scalar*)obuf);
97 (kiss_fft_scalar*)ibuf,
100 fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout);
108 void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse)
111 kiss_fft_scalar * rbuf;
114 rbuf = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar) * nfft );
115 cbuf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * (nfft/2+1) );
116 st = kiss_fftr_alloc( nfft ,isinverse ,0,0);
119 while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) {
120 kiss_fftr( st , rbuf ,cbuf);
121 fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout );
124 while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) {
125 kiss_fftri( st , cbuf ,rbuf);
126 fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout );
135 int get_dims(char * arg,int * dims)
141 p0 = strchr(arg,',');
144 dims[ndims++] = atoi(arg);
145 // fprintf(stderr,"dims[%d] = %d\n",ndims-1,dims[ndims-1]);
151 int main(int argc,char ** argv)
159 dims[0] = 1024; /*default fft size*/
162 int c=getopt(argc,argv,"n:iR");
166 ndims = get_dims(optarg,dims);
168 case 'i':isinverse=1;break;
169 case 'R':isreal=1;break;
171 fprintf(stderr,"usage options:\n"
172 "\t-n d1[,d2,d3...]: fft dimension(s)\n"
174 "\t-R : real input samples, not complex\n");
176 default:fprintf(stderr,"bad %c\n",c);break;
180 if ( optind < argc ) {
181 if (strcmp("-",argv[optind]) !=0)
182 fin = fopen(argv[optind],"rb");
186 if ( optind < argc ) {
187 if ( strcmp("-",argv[optind]) !=0 )
188 fout = fopen(argv[optind],"wb");
194 fft_file_real(fin,fout,dims[0],isinverse);
196 fft_file(fin,fout,dims[0],isinverse);
199 fft_filend_real(fin,fout,dims,ndims,isinverse);
201 fft_filend(fin,fout,dims,ndims,isinverse);
204 if (fout!=stdout) fclose(fout);
205 if (fin!=stdin) fclose(fin);