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.
23 #include "kiss_fftr.h"
36 void config(int argc,char** argv)
39 int c = getopt (argc, argv, "n:r:as");
43 case 'n': nfft=(int)atoi(optarg);break;
44 case 'r': navg=(int)atoi(optarg);break;
45 case 'a': remove_dc=1;break;
46 case 's': stereo=1;break;
48 fprintf (stderr, "usage options:\n"
49 "\t-n d: fft dimension(s) [1024]\n"
50 "\t-r d: number of rows to average [20]\n"
51 "\t-a : remove average from each fft buffer\n"
52 "\t-s : input is stereo, channels will be combined before fft\n"
53 "16 bit machine format real input is assumed\n"
56 fprintf (stderr, "bad %c\n", c);
61 if ( optind < argc ) {
62 if (strcmp("-",argv[optind]) !=0)
63 fin = fopen(argv[optind],"rb");
67 if ( optind < argc ) {
68 if ( strcmp("-",argv[optind]) !=0 )
69 fout = fopen(argv[optind],"wb");
78 #define CHECKNULL(p) if ( (p)==NULL ) do { fprintf(stderr,"CHECKNULL failed @ %s(%d): %s\n",__FILE__,__LINE__,#p );exit(1);} while(0)
88 void val2rgb(float x,rgb_t *p)
90 const double pi = 3.14159265358979;
91 p->g = (int)(255*sin(x*pi));
92 p->r = (int)(255*abs(sin(x*pi*3/2)));
93 p->b = (int)(255*abs(sin(x*pi*5/2)));
94 //fprintf(stderr,"%.2f : %d,%d,%d\n",x,(int)p->r,(int)p->g,(int)p->b);
98 void cpx2pixels(rgb_t * res,const float * fbuf,size_t n)
101 float minval,maxval,valrange;
102 minval=maxval=fbuf[0];
104 for (i = 0; i < n; ++i) {
105 if (fbuf[i] > maxval) maxval = fbuf[i];
106 if (fbuf[i] < minval) minval = fbuf[i];
109 fprintf(stderr,"min ==%f,max=%f\n",minval,maxval);
110 valrange = maxval-minval;
112 fprintf(stderr,"min == max == %f\n",minval);
116 for (i = 0; i < n; ++i)
117 val2rgb( (fbuf[i] - minval)/valrange , res+i );
121 void transform_signal(void)
124 kiss_fftr_cfg cfg=NULL;
125 kiss_fft_scalar *tbuf;
134 CHECKNULL( cfg=kiss_fftr_alloc(nfft,0,0,0) );
135 CHECKNULL( inbuf=(short*)malloc(sizeof(short)*2*nfft ) );
136 CHECKNULL( tbuf=(kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*nfft ) );
137 CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) );
138 CHECKNULL( mag2buf=(float*)malloc(sizeof(float)*nfreqs ) );
140 memset(mag2buf,0,sizeof(mag2buf)*nfreqs);
144 n = fread(inbuf,sizeof(short)*2,nfft,fin);
148 tbuf[i] = inbuf[2*i] + inbuf[2*i+1];
150 n = fread(inbuf,sizeof(short),nfft,fin);
159 for (i=0;i<nfft;++i) avg += tbuf[i];
161 for (i=0;i<nfft;++i) tbuf[i] -= (kiss_fft_scalar)avg;
165 kiss_fftr(cfg,tbuf,fbuf);
167 for (i=0;i<nfreqs;++i)
168 mag2buf[i] += fbuf[i].r * fbuf[i].r + fbuf[i].i * fbuf[i].i;
170 if (++avgctr == navg) {
173 vals = (float*)realloc(vals,sizeof(float)*nrows*nfreqs);
175 for (i=0;i<nfreqs;++i)
176 vals[(nrows - 1) * nfreqs + i] = 10 * log10 ( mag2buf[i] / navg + eps );
177 memset(mag2buf,0,sizeof(mag2buf[0])*nfreqs);
191 png_bytepp row_pointers=NULL;
192 rgb_t * row_data=NULL;
194 int nfreqs = nfft/2+1;
196 png_structp png_ptr=NULL;
197 png_infop info_ptr=NULL;
199 CHECKNULL( png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,0,0,0) );
200 CHECKNULL( info_ptr = png_create_info_struct(png_ptr) );
203 png_init_io(png_ptr, fout );
204 png_set_IHDR(png_ptr, info_ptr ,nfreqs,nrows,8,PNG_COLOR_TYPE_RGB,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT );
207 row_data = (rgb_t*)malloc(sizeof(rgb_t) * nrows * nfreqs) ;
208 cpx2pixels(row_data, vals, nfreqs*nrows );
210 row_pointers = realloc(row_pointers, nrows*sizeof(png_bytep));
211 for (i=0;i<nrows;++i) {
212 row_pointers[i] = (png_bytep)(row_data + i*nfreqs);
214 png_set_rows(png_ptr, info_ptr, row_pointers);
217 fprintf(stderr,"creating %dx%d png\n",nfreqs,nrows);
218 fprintf(stderr,"bitdepth %d \n",png_get_bit_depth(png_ptr,info_ptr ) );
220 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY , NULL);
224 int main(int argc,char ** argv)
232 if (fout!=stdout) fclose(fout);
233 if (fin!=stdin) fclose(fin);