/* * Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. * This file is part of KISS FFT - https://github.com/mborgerding/kissfft * * SPDX-License-Identifier: BSD-3-Clause * See COPYING file for more information. */ #include #include #include #include #include #include #include "kiss_fft.h" #include "kiss_fftr.h" int nfft=1024; FILE * fin=NULL; FILE * fout=NULL; int navg=20; int remove_dc=0; int nrows=0; float * vals=NULL; int stereo=0; static void config(int argc,char** argv) { while (1) { int c = getopt (argc, argv, "n:r:as"); if (c == -1) break; switch (c) { case 'n': nfft=(int)atoi(optarg);break; case 'r': navg=(int)atoi(optarg);break; case 'a': remove_dc=1;break; case 's': stereo=1;break; case '?': fprintf (stderr, "usage options:\n" "\t-n d: fft dimension(s) [1024]\n" "\t-r d: number of rows to average [20]\n" "\t-a : remove average from each fft buffer\n" "\t-s : input is stereo, channels will be combined before fft\n" "16 bit machine format real input is assumed\n" ); break; default: fprintf (stderr, "bad %c\n", c); exit (1); break; } } if ( optind < argc ) { if (strcmp("-",argv[optind]) !=0) fin = fopen(argv[optind],"rb"); ++optind; } if ( optind < argc ) { if ( strcmp("-",argv[optind]) !=0 ) fout = fopen(argv[optind],"wb"); ++optind; } if (fin==NULL) fin=stdin; if (fout==NULL) fout=stdout; } #define CHECKNULL(p) if ( (p)==NULL ) do { fprintf(stderr,"CHECKNULL failed @ %s(%d): %s\n",__FILE__,__LINE__,#p );exit(1);} while(0) typedef struct { png_byte r; png_byte g; png_byte b; } rgb_t; static void val2rgb(float x,rgb_t *p) { const double pi = 3.14159265358979; p->g = (int)(255*sin(x*pi)); p->r = (int)(255*abs(sin(x*pi*3/2))); p->b = (int)(255*abs(sin(x*pi*5/2))); //fprintf(stderr,"%.2f : %d,%d,%d\n",x,(int)p->r,(int)p->g,(int)p->b); } static void cpx2pixels(rgb_t * res,const float * fbuf,size_t n) { size_t i; float minval,maxval,valrange; minval=maxval=fbuf[0]; for (i = 0; i < n; ++i) { if (fbuf[i] > maxval) maxval = fbuf[i]; if (fbuf[i] < minval) minval = fbuf[i]; } fprintf(stderr,"min ==%f,max=%f\n",minval,maxval); valrange = maxval-minval; if (valrange == 0) { fprintf(stderr,"min == max == %f\n",minval); exit (1); } for (i = 0; i < n; ++i) val2rgb( (fbuf[i] - minval)/valrange , res+i ); } static void transform_signal(void) { short *inbuf; kiss_fftr_cfg cfg=NULL; kiss_fft_scalar *tbuf; kiss_fft_cpx *fbuf; float *mag2buf; int i; int n; int avgctr=0; int nfreqs=nfft/2+1; CHECKNULL( cfg=kiss_fftr_alloc(nfft,0,0,0) ); CHECKNULL( inbuf=(short*)malloc(sizeof(short)*2*nfft ) ); CHECKNULL( tbuf=(kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*nfft ) ); CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) ); CHECKNULL( mag2buf=(float*)calloc(nfreqs,sizeof(float) ) ); while (1) { if (stereo) { n = fread(inbuf,sizeof(short)*2,nfft,fin); if (n != nfft ) break; for (i=0;i