annotate src/ext/kissfft/tools/fftutil.c @ 196:da283326bcd3 tip master

Update plugin versions in RDF
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 28 Feb 2020 09:43:02 +0000
parents 5ed6e970541b
children
rev   line source
c@174 1 /*
c@174 2 Copyright (c) 2003-2004, Mark Borgerding
c@174 3
c@174 4 All rights reserved.
c@174 5
c@174 6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
c@174 7
c@174 8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
c@174 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.
c@174 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.
c@174 11
c@174 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.
c@174 13 */
c@174 14
c@174 15 #include <stdlib.h>
c@174 16 #include <math.h>
c@174 17 #include <stdio.h>
c@174 18 #include <string.h>
c@174 19 #include <unistd.h>
c@174 20
c@174 21 #include "kiss_fft.h"
c@174 22 #include "kiss_fftndr.h"
c@174 23
c@174 24 static
c@174 25 void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse)
c@174 26 {
c@174 27 kiss_fft_cfg st;
c@174 28 kiss_fft_cpx * buf;
c@174 29 kiss_fft_cpx * bufout;
c@174 30
c@174 31 buf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
c@174 32 bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
c@174 33 st = kiss_fft_alloc( nfft ,isinverse ,0,0);
c@174 34
c@174 35 while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) {
c@174 36 kiss_fft( st , buf ,bufout);
c@174 37 fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout );
c@174 38 }
c@174 39 free(st);
c@174 40 free(buf);
c@174 41 free(bufout);
c@174 42 }
c@174 43
c@174 44 static
c@174 45 void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
c@174 46 {
c@174 47 kiss_fftnd_cfg st;
c@174 48 kiss_fft_cpx *buf;
c@174 49 int dimprod=1,i;
c@174 50 for (i=0;i<ndims;++i)
c@174 51 dimprod *= dims[i];
c@174 52
c@174 53 buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * dimprod);
c@174 54 st = kiss_fftnd_alloc (dims, ndims, isinverse, 0, 0);
c@174 55
c@174 56 while (fread (buf, sizeof (kiss_fft_cpx) * dimprod, 1, fin) > 0) {
c@174 57 kiss_fftnd (st, buf, buf);
c@174 58 fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout);
c@174 59 }
c@174 60 free (st);
c@174 61 free (buf);
c@174 62 }
c@174 63
c@174 64
c@174 65
c@174 66 static
c@174 67 void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
c@174 68 {
c@174 69 int dimprod=1,i;
c@174 70 kiss_fftndr_cfg st;
c@174 71 void *ibuf;
c@174 72 void *obuf;
c@174 73 int insize,outsize; // size in bytes
c@174 74
c@174 75 for (i=0;i<ndims;++i)
c@174 76 dimprod *= dims[i];
c@174 77 insize = outsize = dimprod;
c@174 78 int rdim = dims[ndims-1];
c@174 79
c@174 80 if (isinverse)
c@174 81 insize = insize*2*(rdim/2+1)/rdim;
c@174 82 else
c@174 83 outsize = outsize*2*(rdim/2+1)/rdim;
c@174 84
c@174 85 ibuf = malloc(insize*sizeof(kiss_fft_scalar));
c@174 86 obuf = malloc(outsize*sizeof(kiss_fft_scalar));
c@174 87
c@174 88 st = kiss_fftndr_alloc(dims, ndims, isinverse, 0, 0);
c@174 89
c@174 90 while ( fread (ibuf, sizeof(kiss_fft_scalar), insize, fin) > 0) {
c@174 91 if (isinverse) {
c@174 92 kiss_fftndri(st,
c@174 93 (kiss_fft_cpx*)ibuf,
c@174 94 (kiss_fft_scalar*)obuf);
c@174 95 }else{
c@174 96 kiss_fftndr(st,
c@174 97 (kiss_fft_scalar*)ibuf,
c@174 98 (kiss_fft_cpx*)obuf);
c@174 99 }
c@174 100 fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout);
c@174 101 }
c@174 102 free(st);
c@174 103 free(ibuf);
c@174 104 free(obuf);
c@174 105 }
c@174 106
c@174 107 static
c@174 108 void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse)
c@174 109 {
c@174 110 kiss_fftr_cfg st;
c@174 111 kiss_fft_scalar * rbuf;
c@174 112 kiss_fft_cpx * cbuf;
c@174 113
c@174 114 rbuf = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar) * nfft );
c@174 115 cbuf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * (nfft/2+1) );
c@174 116 st = kiss_fftr_alloc( nfft ,isinverse ,0,0);
c@174 117
c@174 118 if (isinverse==0) {
c@174 119 while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) {
c@174 120 kiss_fftr( st , rbuf ,cbuf);
c@174 121 fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout );
c@174 122 }
c@174 123 }else{
c@174 124 while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) {
c@174 125 kiss_fftri( st , cbuf ,rbuf);
c@174 126 fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout );
c@174 127 }
c@174 128 }
c@174 129 free(st);
c@174 130 free(rbuf);
c@174 131 free(cbuf);
c@174 132 }
c@174 133
c@174 134 static
c@174 135 int get_dims(char * arg,int * dims)
c@174 136 {
c@174 137 char *p0;
c@174 138 int ndims=0;
c@174 139
c@174 140 do{
c@174 141 p0 = strchr(arg,',');
c@174 142 if (p0)
c@174 143 *p0++ = '\0';
c@174 144 dims[ndims++] = atoi(arg);
c@174 145 // fprintf(stderr,"dims[%d] = %d\n",ndims-1,dims[ndims-1]);
c@174 146 arg = p0;
c@174 147 }while (p0);
c@174 148 return ndims;
c@174 149 }
c@174 150
c@174 151 int main(int argc,char ** argv)
c@174 152 {
c@174 153 int isinverse=0;
c@174 154 int isreal=0;
c@174 155 FILE *fin=stdin;
c@174 156 FILE *fout=stdout;
c@174 157 int ndims=1;
c@174 158 int dims[32];
c@174 159 dims[0] = 1024; /*default fft size*/
c@174 160
c@174 161 while (1) {
c@174 162 int c=getopt(argc,argv,"n:iR");
c@174 163 if (c==-1) break;
c@174 164 switch (c) {
c@174 165 case 'n':
c@174 166 ndims = get_dims(optarg,dims);
c@174 167 break;
c@174 168 case 'i':isinverse=1;break;
c@174 169 case 'R':isreal=1;break;
c@174 170 case '?':
c@174 171 fprintf(stderr,"usage options:\n"
c@174 172 "\t-n d1[,d2,d3...]: fft dimension(s)\n"
c@174 173 "\t-i : inverse\n"
c@174 174 "\t-R : real input samples, not complex\n");
c@174 175 exit (1);
c@174 176 default:fprintf(stderr,"bad %c\n",c);break;
c@174 177 }
c@174 178 }
c@174 179
c@174 180 if ( optind < argc ) {
c@174 181 if (strcmp("-",argv[optind]) !=0)
c@174 182 fin = fopen(argv[optind],"rb");
c@174 183 ++optind;
c@174 184 }
c@174 185
c@174 186 if ( optind < argc ) {
c@174 187 if ( strcmp("-",argv[optind]) !=0 )
c@174 188 fout = fopen(argv[optind],"wb");
c@174 189 ++optind;
c@174 190 }
c@174 191
c@174 192 if (ndims==1) {
c@174 193 if (isreal)
c@174 194 fft_file_real(fin,fout,dims[0],isinverse);
c@174 195 else
c@174 196 fft_file(fin,fout,dims[0],isinverse);
c@174 197 }else{
c@174 198 if (isreal)
c@174 199 fft_filend_real(fin,fout,dims,ndims,isinverse);
c@174 200 else
c@174 201 fft_filend(fin,fout,dims,ndims,isinverse);
c@174 202 }
c@174 203
c@174 204 if (fout!=stdout) fclose(fout);
c@174 205 if (fin!=stdin) fclose(fin);
c@174 206
c@174 207 return 0;
c@174 208 }