=Norig) twidx-=Norig;
228 | C_MUL(t,scratch[q] , twiddles[twidx] );
229 | C_ADDTO( Fout[ k ] ,t);
230 | }
231 | k += m;
232 | }
233 | }
234 | KISS_FFT_TMP_FREE(scratch);
235 | }
236 |
237 | static
238 | void kf_work(
239 | kiss_fft_cpx * Fout,
240 | const kiss_fft_cpx * f,
241 | const size_t fstride,
242 | int in_stride,
243 | int * factors,
244 | const kiss_fft_cfg st
245 | )
246 | {
247 | kiss_fft_cpx * Fout_beg=Fout;
248 | const int p=*factors++; /* the radix */
249 | const int m=*factors++; /* stage's fft length/p */
250 | const kiss_fft_cpx * Fout_end = Fout + p*m;
251 |
252 | #ifdef _OPENMP
253 | // use openmp extensions at the
254 | // top-level (not recursive)
255 | if (fstride==1 && p<=5)
256 | {
257 | int k;
258 |
259 | // execute the p different work units in different threads
260 | # pragma omp parallel for
261 | for (k=0;k floor_sqrt)
324 | p = n; /* no more factors, skip to end */
325 | }
326 | n /= p;
327 | *facbuf++ = p;
328 | *facbuf++ = n;
329 | } while (n > 1);
330 | }
331 |
332 | /*
333 | *
334 | * User-callable function to allocate all necessary storage space for the fft.
335 | *
336 | * The return value is a contiguous block of memory, allocated with malloc. As such,
337 | * It can be freed with free(), rather than a kiss_fft-specific function.
338 | * */
339 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
340 | {
341 | kiss_fft_cfg st=NULL;
342 | size_t memneeded = sizeof(struct kiss_fft_state)
343 | + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/
344 |
345 | if ( lenmem==NULL ) {
346 | st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
347 | }else{
348 | if (mem != NULL && *lenmem >= memneeded)
349 | st = (kiss_fft_cfg)mem;
350 | *lenmem = memneeded;
351 | }
352 | if (st) {
353 | int i;
354 | st->nfft=nfft;
355 | st->inverse = inverse_fft;
356 |
357 | for (i=0;iinverse)
361 | phase *= -1;
362 | kf_cexp(st->twiddles+i, phase );
363 | }
364 |
365 | kf_factor(nfft,st->factors);
366 | }
367 | return st;
368 | }
369 |
370 |
371 | void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride)
372 | {
373 | if (fin == fout) {
374 | //NOTE: this is not really an in-place FFT algorithm.
375 | //It just performs an out-of-place FFT into a temp buffer
376 | kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft);
377 | kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
378 | memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
379 | KISS_FFT_TMP_FREE(tmpbuf);
380 | }else{
381 | kf_work( fout, fin, 1,in_stride, st->factors,st );
382 | }
383 | }
384 |
385 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
386 | {
387 | kiss_fft_stride(cfg,fin,fout,1);
388 | }
389 |
390 |
391 | void kiss_fft_cleanup(void)
392 | {
393 | // nothing needed any more
394 | }
395 |
396 | int kiss_fft_next_fast_size(int n)
397 | {
398 | while(1) {
399 | int m=n;
400 | while ( (m%2) == 0 ) m/=2;
401 | while ( (m%3) == 0 ) m/=3;
402 | while ( (m%5) == 0 ) m/=5;
403 | if (m<=1)
404 | break; /* n is completely factorable by twos, threes, and fives */
405 | n++;
406 | }
407 | return n;
408 | }
409 |
--------------------------------------------------------------------------------
/Util/kiss_fft/kiss_fft.h:
--------------------------------------------------------------------------------
1 | #ifndef KISS_FFT_H
2 | #define KISS_FFT_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #ifdef __cplusplus
10 | extern "C" {
11 | #endif
12 |
13 | /*
14 | ATTENTION!
15 | If you would like a :
16 | -- a utility that will handle the caching of fft objects
17 | -- real-only (no imaginary time component ) FFT
18 | -- a multi-dimensional FFT
19 | -- a command-line utility to perform ffts
20 | -- a command-line utility to perform fast-convolution filtering
21 |
22 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c
23 | in the tools/ directory.
24 | */
25 |
26 | #ifdef USE_SIMD
27 | # include
28 | # define kiss_fft_scalar __m128
29 | #define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16)
30 | #define KISS_FFT_FREE _mm_free
31 | #else
32 | #define KISS_FFT_MALLOC malloc
33 | #define KISS_FFT_FREE free
34 | #endif
35 |
36 |
37 | #ifdef FIXED_POINT
38 | #include
39 | # if (FIXED_POINT == 32)
40 | # define kiss_fft_scalar int32_t
41 | # else
42 | # define kiss_fft_scalar int16_t
43 | # endif
44 | #else
45 | # ifndef kiss_fft_scalar
46 | /* default is float */
47 | # define kiss_fft_scalar float
48 | # endif
49 | #endif
50 |
51 | typedef struct {
52 | kiss_fft_scalar r;
53 | kiss_fft_scalar i;
54 | }kiss_fft_cpx;
55 |
56 | typedef struct kiss_fft_state* kiss_fft_cfg;
57 |
58 | /*
59 | * kiss_fft_alloc
60 | *
61 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
62 | *
63 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL);
64 | *
65 | * The return value from fft_alloc is a cfg buffer used internally
66 | * by the fft routine or NULL.
67 | *
68 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc.
69 | * The returned value should be free()d when done to avoid memory leaks.
70 | *
71 | * The state can be placed in a user supplied buffer 'mem':
72 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
73 | * then the function places the cfg in mem and the size used in *lenmem
74 | * and returns mem.
75 | *
76 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
77 | * then the function returns NULL and places the minimum cfg
78 | * buffer size in *lenmem.
79 | * */
80 |
81 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem);
82 |
83 | /*
84 | * kiss_fft(cfg,in_out_buf)
85 | *
86 | * Perform an FFT on a complex input buffer.
87 | * for a forward FFT,
88 | * fin should be f[0] , f[1] , ... ,f[nfft-1]
89 | * fout will be F[0] , F[1] , ... ,F[nfft-1]
90 | * Note that each element is complex and can be accessed like
91 | f[k].r and f[k].i
92 | * */
93 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
94 |
95 | /*
96 | A more generic version of the above function. It reads its input from every Nth sample.
97 | * */
98 | void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride);
99 |
100 | /* If kiss_fft_alloc allocated a buffer, it is one contiguous
101 | buffer and can be simply free()d when no longer needed*/
102 | #define kiss_fft_free free
103 |
104 | /*
105 | Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up
106 | your compiler output to call this before you exit.
107 | */
108 | void kiss_fft_cleanup(void);
109 |
110 |
111 | /*
112 | * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5)
113 | */
114 | int kiss_fft_next_fast_size(int n);
115 |
116 | /* for real ffts, we need an even size */
117 | #define kiss_fftr_next_fast_size_real(n) \
118 | (kiss_fft_next_fast_size( ((n)+1)>>1)<<1)
119 |
120 | #ifdef __cplusplus
121 | }
122 | #endif
123 |
124 | #endif
125 |
--------------------------------------------------------------------------------
/Util/kiss_fft/kissfft.hh:
--------------------------------------------------------------------------------
1 | #ifndef KISSFFT_CLASS_HH
2 | #include
3 | #include
4 |
5 | namespace kissfft_utils {
6 |
7 | template
8 | struct traits
9 | {
10 | typedef T_scalar scalar_type;
11 | typedef std::complex cpx_type;
12 | void fill_twiddles( std::complex * dst ,int nfft,bool inverse)
13 | {
14 | T_scalar phinc = (inverse?2:-2)* acos( (T_scalar) -1) / nfft;
15 | for (int i=0;i(0,i*phinc) );
17 | }
18 |
19 | void prepare(
20 | std::vector< std::complex > & dst,
21 | int nfft,bool inverse,
22 | std::vector & stageRadix,
23 | std::vector & stageRemainder )
24 | {
25 | _twiddles.resize(nfft);
26 | fill_twiddles( &_twiddles[0],nfft,inverse);
27 | dst = _twiddles;
28 |
29 | //factorize
30 | //start factoring out 4's, then 2's, then 3,5,7,9,...
31 | int n= nfft;
32 | int p=4;
33 | do {
34 | while (n % p) {
35 | switch (p) {
36 | case 4: p = 2; break;
37 | case 2: p = 3; break;
38 | default: p += 2; break;
39 | }
40 | if (p*p>n)
41 | p=n;// no more factors
42 | }
43 | n /= p;
44 | stageRadix.push_back(p);
45 | stageRemainder.push_back(n);
46 | }while(n>1);
47 | }
48 | std::vector _twiddles;
49 |
50 |
51 | const cpx_type twiddle(int i) { return _twiddles[i]; }
52 | };
53 |
54 | }
55 |
56 | template
58 | >
59 | class kissfft
60 | {
61 | public:
62 | typedef T_traits traits_type;
63 | typedef typename traits_type::scalar_type scalar_type;
64 | typedef typename traits_type::cpx_type cpx_type;
65 |
66 | kissfft(int nfft,bool inverse,const traits_type & traits=traits_type() )
67 | :_nfft(nfft),_inverse(inverse),_traits(traits)
68 | {
69 | _traits.prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder);
70 | }
71 |
72 | void transform(const cpx_type * src , cpx_type * dst)
73 | {
74 | kf_work(0, dst, src, 1,1);
75 | }
76 |
77 | private:
78 | void kf_work( int stage,cpx_type * Fout, const cpx_type * f, size_t fstride,size_t in_stride)
79 | {
80 | int p = _stageRadix[stage];
81 | int m = _stageRemainder[stage];
82 | cpx_type * Fout_beg = Fout;
83 | cpx_type * Fout_end = Fout + p*m;
84 |
85 | if (m==1) {
86 | do{
87 | *Fout = *f;
88 | f += fstride*in_stride;
89 | }while(++Fout != Fout_end );
90 | }else{
91 | do{
92 | // recursive call:
93 | // DFT of size m*p performed by doing
94 | // p instances of smaller DFTs of size m,
95 | // each one takes a decimated version of the input
96 | kf_work(stage+1, Fout , f, fstride*p,in_stride);
97 | f += fstride*in_stride;
98 | }while( (Fout += m) != Fout_end );
99 | }
100 |
101 | Fout=Fout_beg;
102 |
103 | // recombine the p smaller DFTs
104 | switch (p) {
105 | case 2: kf_bfly2(Fout,fstride,m); break;
106 | case 3: kf_bfly3(Fout,fstride,m); break;
107 | case 4: kf_bfly4(Fout,fstride,m); break;
108 | case 5: kf_bfly5(Fout,fstride,m); break;
109 | default: kf_bfly_generic(Fout,fstride,m,p); break;
110 | }
111 | }
112 |
113 | // these were #define macros in the original kiss_fft
114 | void C_ADD( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a+b;}
115 | void C_MUL( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a*b;}
116 | void C_SUB( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a-b;}
117 | void C_ADDTO( cpx_type & c,const cpx_type & a) { c+=a;}
118 | void C_FIXDIV( cpx_type & ,int ) {} // NO-OP for float types
119 | scalar_type S_MUL( const scalar_type & a,const scalar_type & b) { return a*b;}
120 | scalar_type HALF_OF( const scalar_type & a) { return a*.5;}
121 | void C_MULBYSCALAR(cpx_type & c,const scalar_type & a) {c*=a;}
122 |
123 | void kf_bfly2( cpx_type * Fout, const size_t fstride, int m)
124 | {
125 | for (int k=0;kreal() - HALF_OF(scratch[3].real() ) , Fout->imag() - HALF_OF(scratch[3].imag() ) );
177 |
178 | C_MULBYSCALAR( scratch[0] , epi3.imag() );
179 |
180 | C_ADDTO(*Fout,scratch[3]);
181 |
182 | Fout[m2] = cpx_type( Fout[m].real() + scratch[0].imag() , Fout[m].imag() - scratch[0].real() );
183 |
184 | C_ADDTO( Fout[m] , cpx_type( -scratch[0].imag(),scratch[0].real() ) );
185 | ++Fout;
186 | }while(--k);
187 | }
188 |
189 | void kf_bfly5( cpx_type * Fout, const size_t fstride, const size_t m)
190 | {
191 | cpx_type *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
192 | size_t u;
193 | cpx_type scratch[13];
194 | cpx_type * twiddles = &_twiddles[0];
195 | cpx_type *tw;
196 | cpx_type ya,yb;
197 | ya = twiddles[fstride*m];
198 | yb = twiddles[fstride*2*m];
199 |
200 | Fout0=Fout;
201 | Fout1=Fout0+m;
202 | Fout2=Fout0+2*m;
203 | Fout3=Fout0+3*m;
204 | Fout4=Fout0+4*m;
205 |
206 | tw=twiddles;
207 | for ( u=0; u=Norig) twidx-=Norig;
284 | C_MUL(t,scratchbuf[q] , twiddles[twidx] );
285 | C_ADDTO( Fout[ k ] ,t);
286 | }
287 | k += m;
288 | }
289 | }
290 | }
291 |
292 | int _nfft;
293 | bool _inverse;
294 | std::vector _twiddles;
295 | std::vector _stageRadix;
296 | std::vector _stageRemainder;
297 | traits_type _traits;
298 | };
299 | #endif
300 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/Makefile:
--------------------------------------------------------------------------------
1 | WARNINGS=-W -Wall -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return \
2 | -Wcast-align -Wcast-qual -Wnested-externs -Wshadow -Wbad-function-cast \
3 | -Wwrite-strings
4 |
5 | ifeq "$(DATATYPE)" ""
6 | DATATYPE=float
7 | endif
8 |
9 | ifeq "$(DATATYPE)" "int32_t"
10 | TYPEFLAGS=-DFIXED_POINT=32
11 | endif
12 |
13 | ifeq "$(DATATYPE)" "int16_t"
14 | TYPEFLAGS=-DFIXED_POINT=16
15 | endif
16 |
17 | ifeq "$(DATATYPE)" "simd"
18 | TYPEFLAGS=-DUSE_SIMD=1 -msse
19 | endif
20 |
21 | ifeq "$(TYPEFLAGS)" ""
22 | TYPEFLAGS=-Dkiss_fft_scalar=$(DATATYPE)
23 | endif
24 |
25 | ifneq ("$(KISS_FFT_USE_ALLOCA)","")
26 | CFLAGS+= -DKISS_FFT_USE_ALLOCA=1
27 | endif
28 | CFLAGS+= $(CFLAGADD)
29 |
30 |
31 | FFTUTIL=fft_$(DATATYPE)
32 | FASTFILT=fastconv_$(DATATYPE)
33 | FASTFILTREAL=fastconvr_$(DATATYPE)
34 | PSDPNG=psdpng_$(DATATYPE)
35 | DUMPHDR=dumphdr_$(DATATYPE)
36 |
37 | all: $(FFTUTIL) $(FASTFILT) $(FASTFILTREAL)
38 | # $(PSDPNG)
39 | # $(DUMPHDR)
40 |
41 | #CFLAGS=-Wall -O3 -pedantic -march=pentiumpro -ffast-math -fomit-frame-pointer $(WARNINGS)
42 | # If the above flags do not work, try the following
43 | CFLAGS=-Wall -O3 $(WARNINGS)
44 | # tip: try -openmp or -fopenmp to use multiple cores
45 |
46 | $(FASTFILTREAL): ../kiss_fft.c kiss_fastfir.c kiss_fftr.c
47 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) -DREAL_FASTFIR $+ -DFAST_FILT_UTIL -lm
48 |
49 | $(FASTFILT): ../kiss_fft.c kiss_fastfir.c
50 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -DFAST_FILT_UTIL -lm
51 |
52 | $(FFTUTIL): ../kiss_fft.c fftutil.c kiss_fftnd.c kiss_fftr.c kiss_fftndr.c
53 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -lm
54 |
55 | $(PSDPNG): ../kiss_fft.c psdpng.c kiss_fftr.c
56 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -lpng -lm
57 |
58 | $(DUMPHDR): ../kiss_fft.c dumphdr.c
59 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -lm
60 |
61 | clean:
62 | rm -f *~ fft fft_* fastconv fastconv_* fastconvr fastconvr_* psdpng psdpng_*
63 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/fftutil.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2003-2004, Mark Borgerding
3 |
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 |
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.
11 |
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.
13 | */
14 |
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 |
21 | #include "kiss_fft.h"
22 | #include "kiss_fftndr.h"
23 |
24 | static
25 | void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse)
26 | {
27 | kiss_fft_cfg st;
28 | kiss_fft_cpx * buf;
29 | kiss_fft_cpx * bufout;
30 |
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);
34 |
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 );
38 | }
39 | free(st);
40 | free(buf);
41 | free(bufout);
42 | }
43 |
44 | static
45 | void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
46 | {
47 | kiss_fftnd_cfg st;
48 | kiss_fft_cpx *buf;
49 | int dimprod=1,i;
50 | for (i=0;i 0) {
57 | kiss_fftnd (st, buf, buf);
58 | fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout);
59 | }
60 | free (st);
61 | free (buf);
62 | }
63 |
64 |
65 |
66 | static
67 | void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
68 | {
69 | int dimprod=1,i;
70 | kiss_fftndr_cfg st;
71 | void *ibuf;
72 | void *obuf;
73 | int insize,outsize; // size in bytes
74 |
75 | for (i=0;i 0) {
91 | if (isinverse) {
92 | kiss_fftndri(st,
93 | (kiss_fft_cpx*)ibuf,
94 | (kiss_fft_scalar*)obuf);
95 | }else{
96 | kiss_fftndr(st,
97 | (kiss_fft_scalar*)ibuf,
98 | (kiss_fft_cpx*)obuf);
99 | }
100 | fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout);
101 | }
102 | free(st);
103 | free(ibuf);
104 | free(obuf);
105 | }
106 |
107 | static
108 | void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse)
109 | {
110 | kiss_fftr_cfg st;
111 | kiss_fft_scalar * rbuf;
112 | kiss_fft_cpx * cbuf;
113 |
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);
117 |
118 | if (isinverse==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 );
122 | }
123 | }else{
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 );
127 | }
128 | }
129 | free(st);
130 | free(rbuf);
131 | free(cbuf);
132 | }
133 |
134 | static
135 | int get_dims(char * arg,int * dims)
136 | {
137 | char *p0;
138 | int ndims=0;
139 |
140 | do{
141 | p0 = strchr(arg,',');
142 | if (p0)
143 | *p0++ = '\0';
144 | dims[ndims++] = atoi(arg);
145 | // fprintf(stderr,"dims[%d] = %d\n",ndims-1,dims[ndims-1]);
146 | arg = p0;
147 | }while (p0);
148 | return ndims;
149 | }
150 |
151 | int main(int argc,char ** argv)
152 | {
153 | int isinverse=0;
154 | int isreal=0;
155 | FILE *fin=stdin;
156 | FILE *fout=stdout;
157 | int ndims=1;
158 | int dims[32];
159 | dims[0] = 1024; /*default fft size*/
160 |
161 | while (1) {
162 | int c=getopt(argc,argv,"n:iR");
163 | if (c==-1) break;
164 | switch (c) {
165 | case 'n':
166 | ndims = get_dims(optarg,dims);
167 | break;
168 | case 'i':isinverse=1;break;
169 | case 'R':isreal=1;break;
170 | case '?':
171 | fprintf(stderr,"usage options:\n"
172 | "\t-n d1[,d2,d3...]: fft dimension(s)\n"
173 | "\t-i : inverse\n"
174 | "\t-R : real input samples, not complex\n");
175 | exit (1);
176 | default:fprintf(stderr,"bad %c\n",c);break;
177 | }
178 | }
179 |
180 | if ( optind < argc ) {
181 | if (strcmp("-",argv[optind]) !=0)
182 | fin = fopen(argv[optind],"rb");
183 | ++optind;
184 | }
185 |
186 | if ( optind < argc ) {
187 | if ( strcmp("-",argv[optind]) !=0 )
188 | fout = fopen(argv[optind],"wb");
189 | ++optind;
190 | }
191 |
192 | if (ndims==1) {
193 | if (isreal)
194 | fft_file_real(fin,fout,dims[0],isinverse);
195 | else
196 | fft_file(fin,fout,dims[0],isinverse);
197 | }else{
198 | if (isreal)
199 | fft_filend_real(fin,fout,dims,ndims,isinverse);
200 | else
201 | fft_filend(fin,fout,dims,ndims,isinverse);
202 | }
203 |
204 | if (fout!=stdout) fclose(fout);
205 | if (fin!=stdin) fclose(fin);
206 |
207 | return 0;
208 | }
209 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kfc.c:
--------------------------------------------------------------------------------
1 | #include "kfc.h"
2 |
3 | /*
4 | Copyright (c) 2003-2004, Mark Borgerding
5 |
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9 |
10 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11 | * 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.
12 | * 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.
13 |
14 | 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.
15 | */
16 |
17 |
18 | typedef struct cached_fft *kfc_cfg;
19 |
20 | struct cached_fft
21 | {
22 | int nfft;
23 | int inverse;
24 | kiss_fft_cfg cfg;
25 | kfc_cfg next;
26 | };
27 |
28 | static kfc_cfg cache_root=NULL;
29 | static int ncached=0;
30 |
31 | static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
32 | {
33 | size_t len;
34 | kfc_cfg cur=cache_root;
35 | kfc_cfg prev=NULL;
36 | while ( cur ) {
37 | if ( cur->nfft == nfft && inverse == cur->inverse )
38 | break;/*found the right node*/
39 | prev = cur;
40 | cur = prev->next;
41 | }
42 | if (cur== NULL) {
43 | /* no cached node found, need to create a new one*/
44 | kiss_fft_alloc(nfft,inverse,0,&len);
45 | #ifdef USE_SIMD
46 | int padding = (16-sizeof(struct cached_fft)) & 15;
47 | // make sure the cfg aligns on a 16 byte boundary
48 | len += padding;
49 | #endif
50 | cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len ));
51 | if (cur == NULL)
52 | return NULL;
53 | cur->cfg = (kiss_fft_cfg)(cur+1);
54 | #ifdef USE_SIMD
55 | cur->cfg = (kiss_fft_cfg) ((char*)(cur+1)+padding);
56 | #endif
57 | kiss_fft_alloc(nfft,inverse,cur->cfg,&len);
58 | cur->nfft=nfft;
59 | cur->inverse=inverse;
60 | cur->next = NULL;
61 | if ( prev )
62 | prev->next = cur;
63 | else
64 | cache_root = cur;
65 | ++ncached;
66 | }
67 | return cur->cfg;
68 | }
69 |
70 | void kfc_cleanup(void)
71 | {
72 | kfc_cfg cur=cache_root;
73 | kfc_cfg next=NULL;
74 | while (cur){
75 | next = cur->next;
76 | free(cur);
77 | cur=next;
78 | }
79 | ncached=0;
80 | cache_root = NULL;
81 | }
82 | void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
83 | {
84 | kiss_fft( find_cached_fft(nfft,0),fin,fout );
85 | }
86 |
87 | void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
88 | {
89 | kiss_fft( find_cached_fft(nfft,1),fin,fout );
90 | }
91 |
92 | #ifdef KFC_TEST
93 | static void check(int nc)
94 | {
95 | if (ncached != nc) {
96 | fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached);
97 | exit(1);
98 | }
99 | }
100 |
101 | int main(void)
102 | {
103 | kiss_fft_cpx buf1[1024],buf2[1024];
104 | memset(buf1,0,sizeof(buf1));
105 | check(0);
106 | kfc_fft(512,buf1,buf2);
107 | check(1);
108 | kfc_fft(512,buf1,buf2);
109 | check(1);
110 | kfc_ifft(512,buf1,buf2);
111 | check(2);
112 | kfc_cleanup();
113 | check(0);
114 | return 0;
115 | }
116 | #endif
117 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kfc.h:
--------------------------------------------------------------------------------
1 | #ifndef KFC_H
2 | #define KFC_H
3 | #include "kiss_fft.h"
4 |
5 | #ifdef __cplusplus
6 | extern "C" {
7 | #endif
8 |
9 | /*
10 | KFC -- Kiss FFT Cache
11 |
12 | Not needing to deal with kiss_fft_alloc and a config
13 | object may be handy for a lot of programs.
14 |
15 | KFC uses the underlying KISS FFT functions, but caches the config object.
16 | The first time kfc_fft or kfc_ifft for a given FFT size, the cfg
17 | object is created for it. All subsequent calls use the cached
18 | configuration object.
19 |
20 | NOTE:
21 | You should probably not use this if your program will be using a lot
22 | of various sizes of FFTs. There is a linear search through the
23 | cached objects. If you are only using one or two FFT sizes, this
24 | will be negligible. Otherwise, you may want to use another method
25 | of managing the cfg objects.
26 |
27 | There is no automated cleanup of the cached objects. This could lead
28 | to large memory usage in a program that uses a lot of *DIFFERENT*
29 | sized FFTs. If you want to force all cached cfg objects to be freed,
30 | call kfc_cleanup.
31 |
32 | */
33 |
34 | /*forward complex FFT */
35 | void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout);
36 | /*reverse complex FFT */
37 | void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout);
38 |
39 | /*free all cached objects*/
40 | void kfc_cleanup(void);
41 |
42 | #ifdef __cplusplus
43 | }
44 | #endif
45 |
46 | #endif
47 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kiss_fastfir.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2003-2004, Mark Borgerding
3 |
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 |
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.
11 |
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.
13 | */
14 |
15 | #include "_kiss_fft_guts.h"
16 |
17 |
18 | /*
19 | Some definitions that allow real or complex filtering
20 | */
21 | #ifdef REAL_FASTFIR
22 | #define MIN_FFT_LEN 2048
23 | #include "kiss_fftr.h"
24 | typedef kiss_fft_scalar kffsamp_t;
25 | typedef kiss_fftr_cfg kfcfg_t;
26 | #define FFT_ALLOC kiss_fftr_alloc
27 | #define FFTFWD kiss_fftr
28 | #define FFTINV kiss_fftri
29 | #else
30 | #define MIN_FFT_LEN 1024
31 | typedef kiss_fft_cpx kffsamp_t;
32 | typedef kiss_fft_cfg kfcfg_t;
33 | #define FFT_ALLOC kiss_fft_alloc
34 | #define FFTFWD kiss_fft
35 | #define FFTINV kiss_fft
36 | #endif
37 |
38 | typedef struct kiss_fastfir_state *kiss_fastfir_cfg;
39 |
40 |
41 |
42 | kiss_fastfir_cfg kiss_fastfir_alloc(const kffsamp_t * imp_resp,size_t n_imp_resp,
43 | size_t * nfft,void * mem,size_t*lenmem);
44 |
45 | /* see do_file_filter for usage */
46 | size_t kiss_fastfir( kiss_fastfir_cfg cfg, kffsamp_t * inbuf, kffsamp_t * outbuf, size_t n, size_t *offset);
47 |
48 |
49 |
50 | static int verbose=0;
51 |
52 |
53 | struct kiss_fastfir_state{
54 | size_t nfft;
55 | size_t ngood;
56 | kfcfg_t fftcfg;
57 | kfcfg_t ifftcfg;
58 | kiss_fft_cpx * fir_freq_resp;
59 | kiss_fft_cpx * freqbuf;
60 | size_t n_freq_bins;
61 | kffsamp_t * tmpbuf;
62 | };
63 |
64 |
65 | kiss_fastfir_cfg kiss_fastfir_alloc(
66 | const kffsamp_t * imp_resp,size_t n_imp_resp,
67 | size_t *pnfft, /* if <= 0, an appropriate size will be chosen */
68 | void * mem,size_t*lenmem)
69 | {
70 | kiss_fastfir_cfg st = NULL;
71 | size_t len_fftcfg,len_ifftcfg;
72 | size_t memneeded = sizeof(struct kiss_fastfir_state);
73 | char * ptr;
74 | size_t i;
75 | size_t nfft=0;
76 | float scale;
77 | int n_freq_bins;
78 | if (pnfft)
79 | nfft=*pnfft;
80 |
81 | if (nfft<=0) {
82 | /* determine fft size as next power of two at least 2x
83 | the impulse response length*/
84 | i=n_imp_resp-1;
85 | nfft=2;
86 | do{
87 | nfft<<=1;
88 | }while (i>>=1);
89 | #ifdef MIN_FFT_LEN
90 | if ( nfft < MIN_FFT_LEN )
91 | nfft=MIN_FFT_LEN;
92 | #endif
93 | }
94 | if (pnfft)
95 | *pnfft = nfft;
96 |
97 | #ifdef REAL_FASTFIR
98 | n_freq_bins = nfft/2 + 1;
99 | #else
100 | n_freq_bins = nfft;
101 | #endif
102 | /*fftcfg*/
103 | FFT_ALLOC (nfft, 0, NULL, &len_fftcfg);
104 | memneeded += len_fftcfg;
105 | /*ifftcfg*/
106 | FFT_ALLOC (nfft, 1, NULL, &len_ifftcfg);
107 | memneeded += len_ifftcfg;
108 | /* tmpbuf */
109 | memneeded += sizeof(kffsamp_t) * nfft;
110 | /* fir_freq_resp */
111 | memneeded += sizeof(kiss_fft_cpx) * n_freq_bins;
112 | /* freqbuf */
113 | memneeded += sizeof(kiss_fft_cpx) * n_freq_bins;
114 |
115 | if (lenmem == NULL) {
116 | st = (kiss_fastfir_cfg) malloc (memneeded);
117 | } else {
118 | if (*lenmem >= memneeded)
119 | st = (kiss_fastfir_cfg) mem;
120 | *lenmem = memneeded;
121 | }
122 | if (!st)
123 | return NULL;
124 |
125 | st->nfft = nfft;
126 | st->ngood = nfft - n_imp_resp + 1;
127 | st->n_freq_bins = n_freq_bins;
128 | ptr=(char*)(st+1);
129 |
130 | st->fftcfg = (kfcfg_t)ptr;
131 | ptr += len_fftcfg;
132 |
133 | st->ifftcfg = (kfcfg_t)ptr;
134 | ptr += len_ifftcfg;
135 |
136 | st->tmpbuf = (kffsamp_t*)ptr;
137 | ptr += sizeof(kffsamp_t) * nfft;
138 |
139 | st->freqbuf = (kiss_fft_cpx*)ptr;
140 | ptr += sizeof(kiss_fft_cpx) * n_freq_bins;
141 |
142 | st->fir_freq_resp = (kiss_fft_cpx*)ptr;
143 | ptr += sizeof(kiss_fft_cpx) * n_freq_bins;
144 |
145 | FFT_ALLOC (nfft,0,st->fftcfg , &len_fftcfg);
146 | FFT_ALLOC (nfft,1,st->ifftcfg , &len_ifftcfg);
147 |
148 | memset(st->tmpbuf,0,sizeof(kffsamp_t)*nfft);
149 | /*zero pad in the middle to left-rotate the impulse response
150 | This puts the scrap samples at the end of the inverse fft'd buffer */
151 | st->tmpbuf[0] = imp_resp[ n_imp_resp - 1 ];
152 | for (i=0;itmpbuf[ nfft - n_imp_resp + 1 + i ] = imp_resp[ i ];
154 | }
155 |
156 | FFTFWD(st->fftcfg,st->tmpbuf,st->fir_freq_resp);
157 |
158 | /* TODO: this won't work for fixed point */
159 | scale = 1.0 / st->nfft;
160 |
161 | for ( i=0; i < st->n_freq_bins; ++i ) {
162 | #ifdef USE_SIMD
163 | st->fir_freq_resp[i].r *= _mm_set1_ps(scale);
164 | st->fir_freq_resp[i].i *= _mm_set1_ps(scale);
165 | #else
166 | st->fir_freq_resp[i].r *= scale;
167 | st->fir_freq_resp[i].i *= scale;
168 | #endif
169 | }
170 | return st;
171 | }
172 |
173 | static void fastconv1buf(const kiss_fastfir_cfg st,const kffsamp_t * in,kffsamp_t * out)
174 | {
175 | size_t i;
176 | /* multiply the frequency response of the input signal by
177 | that of the fir filter*/
178 | FFTFWD( st->fftcfg, in , st->freqbuf );
179 | for ( i=0; in_freq_bins; ++i ) {
180 | kiss_fft_cpx tmpsamp;
181 | C_MUL(tmpsamp,st->freqbuf[i],st->fir_freq_resp[i]);
182 | st->freqbuf[i] = tmpsamp;
183 | }
184 |
185 | /* perform the inverse fft*/
186 | FFTINV(st->ifftcfg,st->freqbuf,out);
187 | }
188 |
189 | /* n : the size of inbuf and outbuf in samples
190 | return value: the number of samples completely processed
191 | n-retval samples should be copied to the front of the next input buffer */
192 | static size_t kff_nocopy(
193 | kiss_fastfir_cfg st,
194 | const kffsamp_t * inbuf,
195 | kffsamp_t * outbuf,
196 | size_t n)
197 | {
198 | size_t norig=n;
199 | while (n >= st->nfft ) {
200 | fastconv1buf(st,inbuf,outbuf);
201 | inbuf += st->ngood;
202 | outbuf += st->ngood;
203 | n -= st->ngood;
204 | }
205 | return norig - n;
206 | }
207 |
208 | static
209 | size_t kff_flush(kiss_fastfir_cfg st,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n)
210 | {
211 | size_t zpad=0,ntmp;
212 |
213 | ntmp = kff_nocopy(st,inbuf,outbuf,n);
214 | n -= ntmp;
215 | inbuf += ntmp;
216 | outbuf += ntmp;
217 |
218 | zpad = st->nfft - n;
219 | memset(st->tmpbuf,0,sizeof(kffsamp_t)*st->nfft );
220 | memcpy(st->tmpbuf,inbuf,sizeof(kffsamp_t)*n );
221 |
222 | fastconv1buf(st,st->tmpbuf,st->tmpbuf);
223 |
224 | memcpy(outbuf,st->tmpbuf,sizeof(kffsamp_t)*( st->ngood - zpad ));
225 | return ntmp + st->ngood - zpad;
226 | }
227 |
228 | size_t kiss_fastfir(
229 | kiss_fastfir_cfg vst,
230 | kffsamp_t * inbuf,
231 | kffsamp_t * outbuf,
232 | size_t n_new,
233 | size_t *offset)
234 | {
235 | size_t ntot = n_new + *offset;
236 | if (n_new==0) {
237 | return kff_flush(vst,inbuf,outbuf,ntot);
238 | }else{
239 | size_t nwritten = kff_nocopy(vst,inbuf,outbuf,ntot);
240 | *offset = ntot - nwritten;
241 | /*save the unused or underused samples at the front of the input buffer */
242 | memcpy( inbuf , inbuf+nwritten , *offset * sizeof(kffsamp_t) );
243 | return nwritten;
244 | }
245 | }
246 |
247 | #ifdef FAST_FILT_UTIL
248 | #include
249 | #include
250 | #include
251 | #include
252 |
253 | static
254 | void direct_file_filter(
255 | FILE * fin,
256 | FILE * fout,
257 | const kffsamp_t * imp_resp,
258 | size_t n_imp_resp)
259 | {
260 | size_t nlag = n_imp_resp - 1;
261 |
262 | const kffsamp_t *tmph;
263 | kffsamp_t *buf, *circbuf;
264 | kffsamp_t outval;
265 | size_t nread;
266 | size_t nbuf;
267 | size_t oldestlag = 0;
268 | size_t k, tap;
269 | #ifndef REAL_FASTFIR
270 | kffsamp_t tmp;
271 | #endif
272 |
273 | nbuf = 4096;
274 | buf = (kffsamp_t *) malloc ( sizeof (kffsamp_t) * nbuf);
275 | circbuf = (kffsamp_t *) malloc (sizeof (kffsamp_t) * nlag);
276 | if (!circbuf || !buf) {
277 | perror("circbuf allocation");
278 | exit(1);
279 | }
280 |
281 | if ( fread (circbuf, sizeof (kffsamp_t), nlag, fin) != nlag ) {
282 | perror ("insufficient data to overcome transient");
283 | exit (1);
284 | }
285 |
286 | do {
287 | nread = fread (buf, sizeof (kffsamp_t), nbuf, fin);
288 | if (nread <= 0)
289 | break;
290 |
291 | for (k = 0; k < nread; ++k) {
292 | tmph = imp_resp+nlag;
293 | #ifdef REAL_FASTFIR
294 | # ifdef USE_SIMD
295 | outval = _mm_set1_ps(0);
296 | #else
297 | outval = 0;
298 | #endif
299 | for (tap = oldestlag; tap < nlag; ++tap)
300 | outval += circbuf[tap] * *tmph--;
301 | for (tap = 0; tap < oldestlag; ++tap)
302 | outval += circbuf[tap] * *tmph--;
303 | outval += buf[k] * *tmph;
304 | #else
305 | # ifdef USE_SIMD
306 | outval.r = outval.i = _mm_set1_ps(0);
307 | #else
308 | outval.r = outval.i = 0;
309 | #endif
310 | for (tap = oldestlag; tap < nlag; ++tap){
311 | C_MUL(tmp,circbuf[tap],*tmph);
312 | --tmph;
313 | C_ADDTO(outval,tmp);
314 | }
315 |
316 | for (tap = 0; tap < oldestlag; ++tap) {
317 | C_MUL(tmp,circbuf[tap],*tmph);
318 | --tmph;
319 | C_ADDTO(outval,tmp);
320 | }
321 | C_MUL(tmp,buf[k],*tmph);
322 | C_ADDTO(outval,tmp);
323 | #endif
324 |
325 | circbuf[oldestlag++] = buf[k];
326 | buf[k] = outval;
327 |
328 | if (oldestlag == nlag)
329 | oldestlag = 0;
330 | }
331 |
332 | if (fwrite (buf, sizeof (buf[0]), nread, fout) != nread) {
333 | perror ("short write");
334 | exit (1);
335 | }
336 | } while (nread);
337 | free (buf);
338 | free (circbuf);
339 | }
340 |
341 | static
342 | void do_file_filter(
343 | FILE * fin,
344 | FILE * fout,
345 | const kffsamp_t * imp_resp,
346 | size_t n_imp_resp,
347 | size_t nfft )
348 | {
349 | int fdout;
350 | size_t n_samps_buf;
351 |
352 | kiss_fastfir_cfg cfg;
353 | kffsamp_t *inbuf,*outbuf;
354 | int nread,nwrite;
355 | size_t idx_inbuf;
356 |
357 | fdout = fileno(fout);
358 |
359 | cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0);
360 |
361 | /* use length to minimize buffer shift*/
362 | n_samps_buf = 8*4096/sizeof(kffsamp_t);
363 | n_samps_buf = nfft + 4*(nfft-n_imp_resp+1);
364 |
365 | if (verbose) fprintf(stderr,"bufsize=%d\n",(int)(sizeof(kffsamp_t)*n_samps_buf) );
366 |
367 |
368 | /*allocate space and initialize pointers */
369 | inbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf);
370 | outbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf);
371 |
372 | idx_inbuf=0;
373 | do{
374 | /* start reading at inbuf[idx_inbuf] */
375 | nread = fread( inbuf + idx_inbuf, sizeof(kffsamp_t), n_samps_buf - idx_inbuf,fin );
376 |
377 | /* If nread==0, then this is a flush.
378 | The total number of samples in input is idx_inbuf + nread . */
379 | nwrite = kiss_fastfir(cfg, inbuf, outbuf,nread,&idx_inbuf) * sizeof(kffsamp_t);
380 | /* kiss_fastfir moved any unused samples to the front of inbuf and updated idx_inbuf */
381 |
382 | if ( write(fdout, outbuf, nwrite) != nwrite ) {
383 | perror("short write");
384 | exit(1);
385 | }
386 | }while ( nread );
387 | free(cfg);
388 | free(inbuf);
389 | free(outbuf);
390 | }
391 |
392 | int main(int argc,char**argv)
393 | {
394 | kffsamp_t * h;
395 | int use_direct=0;
396 | size_t nh,nfft=0;
397 | FILE *fin=stdin;
398 | FILE *fout=stdout;
399 | FILE *filtfile=NULL;
400 | while (1) {
401 | int c=getopt(argc,argv,"n:h:i:o:vd");
402 | if (c==-1) break;
403 | switch (c) {
404 | case 'v':
405 | verbose=1;
406 | break;
407 | case 'n':
408 | nfft=atoi(optarg);
409 | break;
410 | case 'i':
411 | fin = fopen(optarg,"rb");
412 | if (fin==NULL) {
413 | perror(optarg);
414 | exit(1);
415 | }
416 | break;
417 | case 'o':
418 | fout = fopen(optarg,"w+b");
419 | if (fout==NULL) {
420 | perror(optarg);
421 | exit(1);
422 | }
423 | break;
424 | case 'h':
425 | filtfile = fopen(optarg,"rb");
426 | if (filtfile==NULL) {
427 | perror(optarg);
428 | exit(1);
429 | }
430 | break;
431 | case 'd':
432 | use_direct=1;
433 | break;
434 | case '?':
435 | fprintf(stderr,"usage options:\n"
436 | "\t-n nfft: fft size to use\n"
437 | "\t-d : use direct FIR filtering, not fast convolution\n"
438 | "\t-i filename: input file\n"
439 | "\t-o filename: output(filtered) file\n"
440 | "\t-n nfft: fft size to use\n"
441 | "\t-h filename: impulse response\n");
442 | exit (1);
443 | default:fprintf(stderr,"bad %c\n",c);break;
444 | }
445 | }
446 | if (filtfile==NULL) {
447 | fprintf(stderr,"You must supply the FIR coeffs via -h\n");
448 | exit(1);
449 | }
450 | fseek(filtfile,0,SEEK_END);
451 | nh = ftell(filtfile) / sizeof(kffsamp_t);
452 | if (verbose) fprintf(stderr,"%d samples in FIR filter\n",(int)nh);
453 | h = (kffsamp_t*)malloc(sizeof(kffsamp_t)*nh);
454 | fseek(filtfile,0,SEEK_SET);
455 | if (fread(h,sizeof(kffsamp_t),nh,filtfile) != nh)
456 | fprintf(stderr,"short read on filter file\n");
457 |
458 | fclose(filtfile);
459 |
460 | if (use_direct)
461 | direct_file_filter( fin, fout, h,nh);
462 | else
463 | do_file_filter( fin, fout, h,nh,nfft);
464 |
465 | if (fout!=stdout) fclose(fout);
466 | if (fin!=stdin) fclose(fin);
467 |
468 | return 0;
469 | }
470 | #endif
471 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kiss_fftnd.c:
--------------------------------------------------------------------------------
1 |
2 |
3 | /*
4 | Copyright (c) 2003-2004, Mark Borgerding
5 |
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9 |
10 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11 | * 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.
12 | * 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.
13 |
14 | 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.
15 | */
16 |
17 | #include "kiss_fftnd.h"
18 | #include "_kiss_fft_guts.h"
19 |
20 | struct kiss_fftnd_state{
21 | int dimprod; /* dimsum would be mighty tasty right now */
22 | int ndims;
23 | int *dims;
24 | kiss_fft_cfg *states; /* cfg states for each dimension */
25 | kiss_fft_cpx * tmpbuf; /*buffer capable of hold the entire input */
26 | };
27 |
28 | kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem)
29 | {
30 | kiss_fftnd_cfg st = NULL;
31 | int i;
32 | int dimprod=1;
33 | size_t memneeded = sizeof(struct kiss_fftnd_state);
34 | char * ptr;
35 |
36 | for (i=0;istates[i] */
40 | dimprod *= dims[i];
41 | }
42 | memneeded += sizeof(int) * ndims;/* st->dims */
43 | memneeded += sizeof(void*) * ndims;/* st->states */
44 | memneeded += sizeof(kiss_fft_cpx) * dimprod; /* st->tmpbuf */
45 |
46 | if (lenmem == NULL) {/* allocate for the caller*/
47 | st = (kiss_fftnd_cfg) malloc (memneeded);
48 | } else { /* initialize supplied buffer if big enough */
49 | if (*lenmem >= memneeded)
50 | st = (kiss_fftnd_cfg) mem;
51 | *lenmem = memneeded; /*tell caller how big struct is (or would be) */
52 | }
53 | if (!st)
54 | return NULL; /*malloc failed or buffer too small */
55 |
56 | st->dimprod = dimprod;
57 | st->ndims = ndims;
58 | ptr=(char*)(st+1);
59 |
60 | st->states = (kiss_fft_cfg *)ptr;
61 | ptr += sizeof(void*) * ndims;
62 |
63 | st->dims = (int*)ptr;
64 | ptr += sizeof(int) * ndims;
65 |
66 | st->tmpbuf = (kiss_fft_cpx*)ptr;
67 | ptr += sizeof(kiss_fft_cpx) * dimprod;
68 |
69 | for (i=0;idims[i] = dims[i];
72 | kiss_fft_alloc (st->dims[i], inverse_fft, NULL, &len);
73 | st->states[i] = kiss_fft_alloc (st->dims[i], inverse_fft, ptr,&len);
74 | ptr += len;
75 | }
76 | /*
77 | Hi there!
78 |
79 | If you're looking at this particular code, it probably means you've got a brain-dead bounds checker
80 | that thinks the above code overwrites the end of the array.
81 |
82 | It doesn't.
83 |
84 | -- Mark
85 |
86 | P.S.
87 | The below code might give you some warm fuzzies and help convince you.
88 | */
89 | if ( ptr - (char*)st != (int)memneeded ) {
90 | fprintf(stderr,
91 | "################################################################################\n"
92 | "Internal error! Memory allocation miscalculation\n"
93 | "################################################################################\n"
94 | );
95 | }
96 | return st;
97 | }
98 |
99 | /*
100 | This works by tackling one dimension at a time.
101 |
102 | In effect,
103 | Each stage starts out by reshaping the matrix into a DixSi 2d matrix.
104 | A Di-sized fft is taken of each column, transposing the matrix as it goes.
105 |
106 | Here's a 3-d example:
107 | Take a 2x3x4 matrix, laid out in memory as a contiguous buffer
108 | [ [ [ a b c d ] [ e f g h ] [ i j k l ] ]
109 | [ [ m n o p ] [ q r s t ] [ u v w x ] ] ]
110 |
111 | Stage 0 ( D=2): treat the buffer as a 2x12 matrix
112 | [ [a b ... k l]
113 | [m n ... w x] ]
114 |
115 | FFT each column with size 2.
116 | Transpose the matrix at the same time using kiss_fft_stride.
117 |
118 | [ [ a+m a-m ]
119 | [ b+n b-n]
120 | ...
121 | [ k+w k-w ]
122 | [ l+x l-x ] ]
123 |
124 | Note fft([x y]) == [x+y x-y]
125 |
126 | Stage 1 ( D=3) treats the buffer (the output of stage D=2) as an 3x8 matrix,
127 | [ [ a+m a-m b+n b-n c+o c-o d+p d-p ]
128 | [ e+q e-q f+r f-r g+s g-s h+t h-t ]
129 | [ i+u i-u j+v j-v k+w k-w l+x l-x ] ]
130 |
131 | And perform FFTs (size=3) on each of the columns as above, transposing
132 | the matrix as it goes. The output of stage 1 is
133 | (Legend: ap = [ a+m e+q i+u ]
134 | am = [ a-m e-q i-u ] )
135 |
136 | [ [ sum(ap) fft(ap)[0] fft(ap)[1] ]
137 | [ sum(am) fft(am)[0] fft(am)[1] ]
138 | [ sum(bp) fft(bp)[0] fft(bp)[1] ]
139 | [ sum(bm) fft(bm)[0] fft(bm)[1] ]
140 | [ sum(cp) fft(cp)[0] fft(cp)[1] ]
141 | [ sum(cm) fft(cm)[0] fft(cm)[1] ]
142 | [ sum(dp) fft(dp)[0] fft(dp)[1] ]
143 | [ sum(dm) fft(dm)[0] fft(dm)[1] ] ]
144 |
145 | Stage 2 ( D=4) treats this buffer as a 4*6 matrix,
146 | [ [ sum(ap) fft(ap)[0] fft(ap)[1] sum(am) fft(am)[0] fft(am)[1] ]
147 | [ sum(bp) fft(bp)[0] fft(bp)[1] sum(bm) fft(bm)[0] fft(bm)[1] ]
148 | [ sum(cp) fft(cp)[0] fft(cp)[1] sum(cm) fft(cm)[0] fft(cm)[1] ]
149 | [ sum(dp) fft(dp)[0] fft(dp)[1] sum(dm) fft(dm)[0] fft(dm)[1] ] ]
150 |
151 | Then FFTs each column, transposing as it goes.
152 |
153 | The resulting matrix is the 3d FFT of the 2x3x4 input matrix.
154 |
155 | Note as a sanity check that the first element of the final
156 | stage's output (DC term) is
157 | sum( [ sum(ap) sum(bp) sum(cp) sum(dp) ] )
158 | , i.e. the summation of all 24 input elements.
159 |
160 | */
161 | void kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
162 | {
163 | int i,k;
164 | const kiss_fft_cpx * bufin=fin;
165 | kiss_fft_cpx * bufout;
166 |
167 | /*arrange it so the last bufout == fout*/
168 | if ( st->ndims & 1 ) {
169 | bufout = fout;
170 | if (fin==fout) {
171 | memcpy( st->tmpbuf, fin, sizeof(kiss_fft_cpx) * st->dimprod );
172 | bufin = st->tmpbuf;
173 | }
174 | }else
175 | bufout = st->tmpbuf;
176 |
177 | for ( k=0; k < st->ndims; ++k) {
178 | int curdim = st->dims[k];
179 | int stride = st->dimprod / curdim;
180 |
181 | for ( i=0 ; istates[k], bufin+i , bufout+i*curdim, stride );
183 |
184 | /*toggle back and forth between the two buffers*/
185 | if (bufout == st->tmpbuf){
186 | bufout = fout;
187 | bufin = st->tmpbuf;
188 | }else{
189 | bufout = st->tmpbuf;
190 | bufin = fout;
191 | }
192 | }
193 | }
194 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kiss_fftnd.h:
--------------------------------------------------------------------------------
1 | #ifndef KISS_FFTND_H
2 | #define KISS_FFTND_H
3 |
4 | #include "kiss_fft.h"
5 |
6 | #ifdef __cplusplus
7 | extern "C" {
8 | #endif
9 |
10 | typedef struct kiss_fftnd_state * kiss_fftnd_cfg;
11 |
12 | kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem);
13 | void kiss_fftnd(kiss_fftnd_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
14 |
15 | #ifdef __cplusplus
16 | }
17 | #endif
18 | #endif
19 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kiss_fftndr.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2003-2004, Mark Borgerding
3 |
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 |
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.
11 |
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.
13 | */
14 |
15 | #include "kiss_fftndr.h"
16 | #include "_kiss_fft_guts.h"
17 | #define MAX(x,y) ( ( (x)<(y) )?(y):(x) )
18 |
19 | struct kiss_fftndr_state
20 | {
21 | int dimReal;
22 | int dimOther;
23 | kiss_fftr_cfg cfg_r;
24 | kiss_fftnd_cfg cfg_nd;
25 | void * tmpbuf;
26 | };
27 |
28 | static int prod(const int *dims, int ndims)
29 | {
30 | int x=1;
31 | while (ndims--)
32 | x *= *dims++;
33 | return x;
34 | }
35 |
36 | kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem)
37 | {
38 | kiss_fftndr_cfg st = NULL;
39 | size_t nr=0 , nd=0,ntmp=0;
40 | int dimReal = dims[ndims-1];
41 | int dimOther = prod(dims,ndims-1);
42 | size_t memneeded;
43 |
44 | (void)kiss_fftr_alloc(dimReal,inverse_fft,NULL,&nr);
45 | (void)kiss_fftnd_alloc(dims,ndims-1,inverse_fft,NULL,&nd);
46 | ntmp =
47 | MAX( 2*dimOther , dimReal+2) * sizeof(kiss_fft_scalar) // freq buffer for one pass
48 | + dimOther*(dimReal+2) * sizeof(kiss_fft_scalar); // large enough to hold entire input in case of in-place
49 |
50 | memneeded = sizeof( struct kiss_fftndr_state ) + nr + nd + ntmp;
51 |
52 | if (lenmem==NULL) {
53 | st = (kiss_fftndr_cfg) malloc(memneeded);
54 | }else{
55 | if (*lenmem >= memneeded)
56 | st = (kiss_fftndr_cfg)mem;
57 | *lenmem = memneeded;
58 | }
59 | if (st==NULL)
60 | return NULL;
61 | memset( st , 0 , memneeded);
62 |
63 | st->dimReal = dimReal;
64 | st->dimOther = dimOther;
65 | st->cfg_r = kiss_fftr_alloc( dimReal,inverse_fft,st+1,&nr);
66 | st->cfg_nd = kiss_fftnd_alloc(dims,ndims-1,inverse_fft, ((char*) st->cfg_r)+nr,&nd);
67 | st->tmpbuf = (char*)st->cfg_nd + nd;
68 |
69 | return st;
70 | }
71 |
72 | void kiss_fftndr(kiss_fftndr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
73 | {
74 | int k1,k2;
75 | int dimReal = st->dimReal;
76 | int dimOther = st->dimOther;
77 | int nrbins = dimReal/2+1;
78 |
79 | kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf;
80 | kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther);
81 |
82 | // timedata is N0 x N1 x ... x Nk real
83 |
84 | // take a real chunk of data, fft it and place the output at correct intervals
85 | for (k1=0;k1cfg_r, timedata + k1*dimReal , tmp1 ); // tmp1 now holds nrbins complex points
87 | for (k2=0;k2cfg_nd, tmp2+k2*dimOther, tmp1); // tmp1 now holds dimOther complex points
93 | for (k1=0;k1dimReal;
102 | int dimOther = st->dimOther;
103 | int nrbins = dimReal/2+1;
104 | kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf;
105 | kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther);
106 |
107 | for (k2=0;k2cfg_nd, tmp1, tmp2+k2*dimOther);
111 | }
112 |
113 | for (k1=0;k1cfg_r,tmp1,timedata + k1*dimReal);
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kiss_fftndr.h:
--------------------------------------------------------------------------------
1 | #ifndef KISS_NDR_H
2 | #define KISS_NDR_H
3 |
4 | #include "kiss_fft.h"
5 | #include "kiss_fftr.h"
6 | #include "kiss_fftnd.h"
7 |
8 | #ifdef __cplusplus
9 | extern "C" {
10 | #endif
11 |
12 | typedef struct kiss_fftndr_state *kiss_fftndr_cfg;
13 |
14 |
15 | kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem);
16 | /*
17 | dims[0] must be even
18 |
19 | If you don't care to allocate space, use mem = lenmem = NULL
20 | */
21 |
22 |
23 | void kiss_fftndr(
24 | kiss_fftndr_cfg cfg,
25 | const kiss_fft_scalar *timedata,
26 | kiss_fft_cpx *freqdata);
27 | /*
28 | input timedata has dims[0] X dims[1] X ... X dims[ndims-1] scalar points
29 | output freqdata has dims[0] X dims[1] X ... X dims[ndims-1]/2+1 complex points
30 | */
31 |
32 | void kiss_fftndri(
33 | kiss_fftndr_cfg cfg,
34 | const kiss_fft_cpx *freqdata,
35 | kiss_fft_scalar *timedata);
36 | /*
37 | input and output dimensions are the exact opposite of kiss_fftndr
38 | */
39 |
40 |
41 | #define kiss_fftr_free free
42 |
43 | #ifdef __cplusplus
44 | }
45 | #endif
46 |
47 | #endif
48 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kiss_fftr.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2003-2004, Mark Borgerding
3 |
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 |
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.
11 |
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.
13 | */
14 |
15 | #include "kiss_fftr.h"
16 | #include "_kiss_fft_guts.h"
17 |
18 | struct kiss_fftr_state{
19 | kiss_fft_cfg substate;
20 | kiss_fft_cpx * tmpbuf;
21 | kiss_fft_cpx * super_twiddles;
22 | #ifdef USE_SIMD
23 | void * pad;
24 | #endif
25 | };
26 |
27 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
28 | {
29 | int i;
30 | kiss_fftr_cfg st = NULL;
31 | size_t subsize, memneeded;
32 |
33 | if (nfft & 1) {
34 | fprintf(stderr,"Real FFT optimization must be even.\n");
35 | return NULL;
36 | }
37 | nfft >>= 1;
38 |
39 | kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
40 | memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2);
41 |
42 | if (lenmem == NULL) {
43 | st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
44 | } else {
45 | if (*lenmem >= memneeded)
46 | st = (kiss_fftr_cfg) mem;
47 | *lenmem = memneeded;
48 | }
49 | if (!st)
50 | return NULL;
51 |
52 | st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
53 | st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
54 | st->super_twiddles = st->tmpbuf + nfft;
55 | kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
56 |
57 | for (i = 0; i < nfft/2; ++i) {
58 | double phase =
59 | -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
60 | if (inverse_fft)
61 | phase *= -1;
62 | kf_cexp (st->super_twiddles+i,phase);
63 | }
64 | return st;
65 | }
66 |
67 | void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
68 | {
69 | /* input buffer timedata is stored row-wise */
70 | int k,ncfft;
71 | kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
72 |
73 | if ( st->substate->inverse) {
74 | fprintf(stderr,"kiss fft usage error: improper alloc\n");
75 | exit(1);
76 | }
77 |
78 | ncfft = st->substate->nfft;
79 |
80 | /*perform the parallel fft of two real signals packed in real,imag*/
81 | kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
82 | /* The real part of the DC element of the frequency spectrum in st->tmpbuf
83 | * contains the sum of the even-numbered elements of the input time sequence
84 | * The imag part is the sum of the odd-numbered elements
85 | *
86 | * The sum of tdc.r and tdc.i is the sum of the input time sequence.
87 | * yielding DC of input time sequence
88 | * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
89 | * yielding Nyquist bin of input time sequence
90 | */
91 |
92 | tdc.r = st->tmpbuf[0].r;
93 | tdc.i = st->tmpbuf[0].i;
94 | C_FIXDIV(tdc,2);
95 | CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
96 | CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
97 | freqdata[0].r = tdc.r + tdc.i;
98 | freqdata[ncfft].r = tdc.r - tdc.i;
99 | #ifdef USE_SIMD
100 | freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
101 | #else
102 | freqdata[ncfft].i = freqdata[0].i = 0;
103 | #endif
104 |
105 | for ( k=1;k <= ncfft/2 ; ++k ) {
106 | fpk = st->tmpbuf[k];
107 | fpnk.r = st->tmpbuf[ncfft-k].r;
108 | fpnk.i = - st->tmpbuf[ncfft-k].i;
109 | C_FIXDIV(fpk,2);
110 | C_FIXDIV(fpnk,2);
111 |
112 | C_ADD( f1k, fpk , fpnk );
113 | C_SUB( f2k, fpk , fpnk );
114 | C_MUL( tw , f2k , st->super_twiddles[k-1]);
115 |
116 | freqdata[k].r = HALF_OF(f1k.r + tw.r);
117 | freqdata[k].i = HALF_OF(f1k.i + tw.i);
118 | freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
119 | freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
120 | }
121 | }
122 |
123 | void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
124 | {
125 | /* input buffer timedata is stored row-wise */
126 | int k, ncfft;
127 |
128 | if (st->substate->inverse == 0) {
129 | fprintf (stderr, "kiss fft usage error: improper alloc\n");
130 | exit (1);
131 | }
132 |
133 | ncfft = st->substate->nfft;
134 |
135 | st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
136 | st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
137 | C_FIXDIV(st->tmpbuf[0],2);
138 |
139 | for (k = 1; k <= ncfft / 2; ++k) {
140 | kiss_fft_cpx fk, fnkc, fek, fok, tmp;
141 | fk = freqdata[k];
142 | fnkc.r = freqdata[ncfft - k].r;
143 | fnkc.i = -freqdata[ncfft - k].i;
144 | C_FIXDIV( fk , 2 );
145 | C_FIXDIV( fnkc , 2 );
146 |
147 | C_ADD (fek, fk, fnkc);
148 | C_SUB (tmp, fk, fnkc);
149 | C_MUL (fok, tmp, st->super_twiddles[k-1]);
150 | C_ADD (st->tmpbuf[k], fek, fok);
151 | C_SUB (st->tmpbuf[ncfft - k], fek, fok);
152 | #ifdef USE_SIMD
153 | st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
154 | #else
155 | st->tmpbuf[ncfft - k].i *= -1;
156 | #endif
157 | }
158 | kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
159 | }
160 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/kiss_fftr.h:
--------------------------------------------------------------------------------
1 | #ifndef KISS_FTR_H
2 | #define KISS_FTR_H
3 |
4 | #include "kiss_fft.h"
5 | #ifdef __cplusplus
6 | extern "C" {
7 | #endif
8 |
9 |
10 | /*
11 |
12 | Real optimized version can save about 45% cpu time vs. complex fft of a real seq.
13 |
14 |
15 |
16 | */
17 |
18 | typedef struct kiss_fftr_state *kiss_fftr_cfg;
19 |
20 |
21 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem);
22 | /*
23 | nfft must be even
24 |
25 | If you don't care to allocate space, use mem = lenmem = NULL
26 | */
27 |
28 |
29 | void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata);
30 | /*
31 | input timedata has nfft scalar points
32 | output freqdata has nfft/2+1 complex points
33 | */
34 |
35 | void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata);
36 | /*
37 | input freqdata has nfft/2+1 complex points
38 | output timedata has nfft scalar points
39 | */
40 |
41 | #define kiss_fftr_free free
42 |
43 | #ifdef __cplusplus
44 | }
45 | #endif
46 | #endif
47 |
--------------------------------------------------------------------------------
/Util/kiss_fft/tools/psdpng.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2003-2004, Mark Borgerding
3 |
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 |
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.
11 |
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.
13 | */
14 |
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 |
22 | #include "kiss_fft.h"
23 | #include "kiss_fftr.h"
24 |
25 | int nfft=1024;
26 | FILE * fin=NULL;
27 | FILE * fout=NULL;
28 |
29 | int navg=20;
30 | int remove_dc=0;
31 | int nrows=0;
32 | float * vals=NULL;
33 | int stereo=0;
34 |
35 | static
36 | void config(int argc,char** argv)
37 | {
38 | while (1) {
39 | int c = getopt (argc, argv, "n:r:as");
40 | if (c == -1)
41 | break;
42 | switch (c) {
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;
47 | case '?':
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"
54 | );
55 | default:
56 | fprintf (stderr, "bad %c\n", c);
57 | exit (1);
58 | break;
59 | }
60 | }
61 | if ( optind < argc ) {
62 | if (strcmp("-",argv[optind]) !=0)
63 | fin = fopen(argv[optind],"rb");
64 | ++optind;
65 | }
66 |
67 | if ( optind < argc ) {
68 | if ( strcmp("-",argv[optind]) !=0 )
69 | fout = fopen(argv[optind],"wb");
70 | ++optind;
71 | }
72 | if (fin==NULL)
73 | fin=stdin;
74 | if (fout==NULL)
75 | fout=stdout;
76 | }
77 |
78 | #define CHECKNULL(p) if ( (p)==NULL ) do { fprintf(stderr,"CHECKNULL failed @ %s(%d): %s\n",__FILE__,__LINE__,#p );exit(1);} while(0)
79 |
80 | typedef struct
81 | {
82 | png_byte r;
83 | png_byte g;
84 | png_byte b;
85 | } rgb_t;
86 |
87 | static
88 | void val2rgb(float x,rgb_t *p)
89 | {
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);
95 | }
96 |
97 | static
98 | void cpx2pixels(rgb_t * res,const float * fbuf,size_t n)
99 | {
100 | size_t i;
101 | float minval,maxval,valrange;
102 | minval=maxval=fbuf[0];
103 |
104 | for (i = 0; i < n; ++i) {
105 | if (fbuf[i] > maxval) maxval = fbuf[i];
106 | if (fbuf[i] < minval) minval = fbuf[i];
107 | }
108 |
109 | fprintf(stderr,"min ==%f,max=%f\n",minval,maxval);
110 | valrange = maxval-minval;
111 | if (valrange == 0) {
112 | fprintf(stderr,"min == max == %f\n",minval);
113 | exit (1);
114 | }
115 |
116 | for (i = 0; i < n; ++i)
117 | val2rgb( (fbuf[i] - minval)/valrange , res+i );
118 | }
119 |
120 | static
121 | void transform_signal(void)
122 | {
123 | short *inbuf;
124 | kiss_fftr_cfg cfg=NULL;
125 | kiss_fft_scalar *tbuf;
126 | kiss_fft_cpx *fbuf;
127 | float *mag2buf;
128 | int i;
129 | int n;
130 | int avgctr=0;
131 |
132 | int nfreqs=nfft/2+1;
133 |
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 ) );
139 |
140 | memset(mag2buf,0,sizeof(mag2buf)*nfreqs);
141 |
142 | while (1) {
143 | if (stereo) {
144 | n = fread(inbuf,sizeof(short)*2,nfft,fin);
145 | if (n != nfft )
146 | break;
147 | for (i=0;i points = PointTrace(image); //
21 | // //
22 | // 5.) Create a QuadTree to store points array in (for much faster //
23 | // search algorithms.) //
24 | // *Recommendation: Use an even number for QuadTree indices. I typically //
25 | // use values between 8 and 16. //
26 | // *Example: //
27 | // QuadTree tree(points, 8); //
28 | // //
29 | // 6.) Pass function SearchPolygons() to filtering surface. 'Samples' //
30 | // describes the number of points used to 'sample' each slice of the QuadTree //
31 | // to search the points array for polygons. Use a higher number for increased //
32 | // accuracy at the expense of performance. //
33 | // *Example: //
34 | // SearchPolygons(tree, index, Vec2i( 8, 2 )); //
35 | //__________________________________________________________________________________________//
36 |
37 | #include
38 | #include
39 | #include
40 | #include
41 | #include
42 | #include "SDL.h"
43 | #include "SDL_image.h"
44 | #include "../Transform.h"
45 | #include "Segment.h"
46 |
47 | using namespace std;
48 | using namespace transform;
49 |
50 | Quad::Quad(){
51 | pos = CreateVec2i(0, 0);
52 | points.clear();
53 | points.resize(0);
54 | rect = CreateRect(0, 0, 0, 0);
55 | }
56 |
57 | Quad::Quad(vector data){
58 | pos = CreateVec2i(0, 0);
59 | points = data;
60 | rect = getRect();
61 | }
62 |
63 | Quad::Quad(vector data, Vec2i position){
64 | pos = position;
65 | points = data;
66 | rect = getRect();
67 | }
68 |
69 | inline SDL_Rect Quad::getRect(){
70 | Vec2i iter;
71 | int minX = points[0].x, minY = points[0].y,
72 | maxX = minX, maxY = minY;
73 |
74 | for (int a = 0; a < points.size(); a++){
75 | iter = CreateVec2i(points[a].x, points[a].y);
76 |
77 | if (iter.x < minX)
78 | minX = iter.x;
79 |
80 | if (iter.x > maxX)
81 | maxX = iter.x;
82 |
83 | if (iter.y < minY)
84 | minY = iter.y;
85 |
86 | if (iter.y > maxY)
87 | maxY = iter.y;
88 | }
89 |
90 | return CreateRect(minX, minY, maxX, maxY);
91 | }
92 |
93 | QuadTree::QuadTree(){
94 | quads.clear();
95 | quads.resize(0);
96 | }
97 |
98 | QuadTree::QuadTree(vector data){
99 | if (data.size() < 4) data.resize(4);
100 |
101 | int step = data.size() / 4;
102 | Quad temp;
103 |
104 | for (int a = 0; a < 4; a++){
105 | //Clear temporary Quad array
106 | temp.points.clear(); temp.points.resize(0);
107 |
108 | //Add point data per step size
109 | for (int b = 0; b < step; b++){
110 | int id = b + (step*a);
111 |
112 | if (data.size() > id)
113 | temp.points.push_back(data[b + (step*a)]);
114 | }
115 |
116 | //Update temporary Quad index
117 | temp.pos = CreateVec2i(a, 0);
118 | temp.getRect();
119 | quads.push_back(temp);
120 | }
121 |
122 | rect = getRect();
123 |
124 | for (int b = 0; b < quads.size(); b++){
125 | quads[b].rect = quads[b].getRect();
126 | }
127 | }
128 |
129 | QuadTree::QuadTree(vector data, int indices){
130 | if (data.size() < indices) data.resize(indices);
131 |
132 | int step = data.size() / indices;
133 | Quad temp;
134 |
135 | for (int a = 0; a < indices; a++){
136 | //Clear temporary Quad array
137 | temp.points.clear(); temp.points.resize(0);
138 |
139 | //Add point data per step size
140 | for (int b = 0; b < step; b++){
141 | int id = b + (step*a);
142 |
143 | if (data.size() > id)
144 | temp.points.push_back(data[id]);
145 | }
146 |
147 | //Update temporary Quad index
148 | temp.pos = CreateVec2i(a, 0);
149 | quads.push_back(temp);
150 | }
151 |
152 | rect = getRect();
153 |
154 | for (int b = 0; b < quads.size(); b++){
155 | quads[b].rect = quads[b].getRect();
156 | }
157 | }
158 |
159 | inline vector* QuadTree::getPoints(int index){
160 | if (index < quads.size())
161 | return &quads[index].points;
162 | else
163 | return NULL;
164 | }
165 |
166 | inline int QuadTree::getSize(){
167 | int sum;
168 |
169 | for (int a = 0; a < quads.size(); a++){
170 | sum += quads[a].points.size();
171 | }
172 |
173 | return sum;
174 | }
175 |
176 | inline int QuadTree::getSize(int index){
177 | if (index < quads.size())
178 | return quads[index].points.size();
179 | else
180 | return 0;
181 | }
182 |
183 | inline int QuadTree::getX(int index, int item){
184 | if (index < quads.size()){
185 | if (item < quads[index].points.size())
186 | return quads[index].points[item].x;
187 | else{
188 | cout << "Failed to return X value for item " << item << " at index " << index << endl;
189 | return 0;
190 | }
191 | }
192 | else{
193 | cout << "Failed to return X value at index " << index << endl;
194 | return 0;
195 | }
196 | }
197 |
198 | inline int QuadTree::getY(int index, int item){
199 | if (index < quads.size()){
200 | if (item < quads[index].points.size())
201 | return quads[index].points[item].y;
202 | else{
203 | cout << "Failed to return Y value for item " << item << " at index " << index << endl;
204 | return 0;
205 | }
206 | }
207 | else{
208 | cout << "Failed to return Y value at index " << index << endl;
209 | return 0;
210 | }
211 | }
212 |
213 | inline Vec2i QuadTree::getVec2i(int index, int item){
214 | if (index < quads.size()){
215 | if (item < quads[index].points.size())
216 | return quads[index].points[item];
217 | else{
218 | cout << "Failed to return item " << item << " at index " << index << endl;
219 | return Vec2i(0, 0);
220 | }
221 | }
222 | else{
223 | cout << "Failed to return item at index " << index << endl;
224 | return Vec2i(0, 0);
225 | }
226 | }
227 |
228 | inline SDL_Rect QuadTree::getRect(){
229 | Vec2i iter;
230 | int minX = getX(0, 0), minY = getY(0, 0),
231 | maxX = minX, maxY = minY;
232 |
233 | for (int index = 0; index < quads.size(); index++){
234 | for (int a = 0; a < getSize(index); a++){
235 | iter = CreateVec2i(getX(index, a), getY(index, a));
236 |
237 | if (iter.x < minX)
238 | minX = iter.x;
239 |
240 | if (iter.x > maxX)
241 | maxX = iter.x;
242 |
243 | if (iter.y < minY)
244 | minY = iter.y;
245 |
246 | if (iter.y > maxY)
247 | maxY = iter.y;
248 | }
249 | }
250 |
251 | return CreateRect(minX, minY, maxX, maxY);
252 | }
253 |
254 | inline SDL_Rect QuadTree::getRect(int index){
255 | Vec2i iter;
256 | int minX = getX(index, 0), minY = getY(index, 0),
257 | maxX = minX, maxY = minY;
258 |
259 | for (int a = 0; a < getSize(index); a++){
260 | iter = CreateVec2i(getX(index, a), getY(index, a));
261 |
262 | if (iter.x < minX)
263 | minX = iter.x;
264 |
265 | if (iter.x > maxX)
266 | maxX = iter.x;
267 |
268 | if (iter.y < minY)
269 | minY = iter.y;
270 |
271 | if (iter.y > maxY)
272 | maxY = iter.y;
273 | }
274 |
275 | return CreateRect(minX, minY, maxX, maxY);
276 | }
277 |
278 | Segmented::Segmented(){
279 | surface = EmptySurface(64, 64);
280 | isValid = false;
281 | }
282 |
283 | Segmented::Segmented(SDL_Surface* source){
284 | surface = CopySurface(source);
285 |
286 | //Grayscale(surface, GRAY_AVERAGE);
287 | //Threshold(surface, CHANNEL_RED, 150);
288 |
289 | isValid = true;
290 | }
291 |
292 | Segmented::Segmented(SDL_Surface* source, Uint8 threshold){
293 | surface = CopySurface(source);
294 |
295 | Grayscale(surface, GRAY_AVERAGE);
296 | Threshold(surface, CHANNEL_RED, threshold);
297 |
298 | isValid = true;
299 | }
300 |
301 | vector RotateData(vector points){
302 |
303 | vector out;
304 | return out;
305 | }
306 |
307 | inline SDL_Rect FindRect(vector points){
308 | Vec2i iter;
309 | int minX = points[0].x, minY = points[0].y,
310 | maxX = minX, maxY = minY;
311 |
312 | for (int a = 0; a < points.size(); a++){
313 | iter = CreateVec2i(points[0].x, points[0].y);
314 |
315 | if (iter.x < minX)
316 | minX = iter.x;
317 |
318 | if (iter.x > maxX)
319 | maxX = iter.x;
320 |
321 | if (iter.y < minY)
322 | minY = iter.y;
323 |
324 | if (iter.y > maxY)
325 | maxY = iter.y;
326 | }
327 |
328 | return CreateRect(minX, minY, maxX, maxY);
329 | }
330 |
331 | inline int FindIndex(QuadTree tree, int y){
332 | int index = -1;
333 |
334 | for (int b = 0; b < tree.quads.size(); b++){
335 | if (y >= tree.quads[b].rect.y && y <= tree.quads[b].rect.h){
336 | index = b;
337 | break;
338 | }
339 | }
340 |
341 | return index;
342 | }
343 |
344 | inline bool PointExists(vector points, Vec2i point, bool useQuad){
345 | if (useQuad != false){
346 | int segments = 8;
347 |
348 | if (points.size() < segments) points.resize(segments);
349 | QuadTree sub(points, segments);
350 |
351 | int index = -1;
352 |
353 | for (int b = 0; b < sub.quads.size(); b++){
354 | if (point.y >= sub.quads[b].rect.y && point.y <= sub.quads[b].rect.h){
355 | index = b;
356 | break;
357 | }
358 | }
359 |
360 | if (index > -1){
361 | for (int a = 0; a < sub.quads[index].points.size(); a++){
362 | if (sub.getX(index, a) == point.x && sub.getY(index, a) == point.y){
363 | return true;
364 | break;
365 | }
366 | }
367 | return false;
368 | }
369 | else
370 | return false;
371 | }
372 | else{
373 | for (int a = 0; a < points.size(); a++){
374 | if (point.x == points[a].x && point.y == points[a].y){
375 | return true;
376 | break;
377 | }
378 | }
379 | return false;
380 | }
381 | }
382 |
383 | inline bool PointExists(QuadTree tree, Vec2i point){
384 | int index = -1;
385 |
386 | for (int b = 0; b < tree.quads.size(); b++){
387 | if (point.y >= tree.quads[b].rect.y && point.y <= tree.quads[b].rect.h){
388 | index = b;
389 | break;
390 | }
391 | }
392 |
393 | if (index > -1){
394 | for (int a = 0; a < tree.quads[index].points.size(); a++){
395 | if (tree.getX(index, a) == point.x && tree.getY(index, a) == point.y){
396 | return true;
397 | }
398 | }
399 | return false;
400 | }
401 | else
402 | return false;
403 | }
404 |
405 | inline bool PointExists(QuadTree tree, Vec2i point, int* id){
406 | int index = -1;
407 |
408 | for (int b = 0; b < tree.quads.size(); b++){
409 | if (point.y >= tree.quads[b].rect.y && point.y <= tree.quads[b].rect.h){
410 | index = b;
411 | break;
412 | }
413 | }
414 |
415 | if (index > -1){
416 | for (int a = 0; a < tree.quads[index].points.size(); a++){
417 | if (tree.getX(index, a) == point.x && tree.getY(index, a) == point.y){
418 | *id = index;
419 | return true;
420 | }
421 | }
422 | return false;
423 | }
424 | else
425 | return false;
426 | }
427 |
428 | inline bool PointInRect(SDL_Rect rect, Vec2i pt){
429 | //Notes: I might not need to add rect.x to rect.w & rect.y to rect.h
430 | //Need to test more.
431 |
432 | if (pt.x >= rect.x && pt.y >= rect.y &&
433 | pt.x <= rect.w && pt.y <= rect.h)
434 | return true;
435 | else
436 | return false;
437 | }
438 |
439 | inline bool PointInPoly(vector points, Vec2i point, SDL_Rect boundary){
440 | int count = 0;
441 |
442 | for (int x = point.x; x < boundary.x + boundary.w; x++){
443 | if (PointExists(points, CreateVec2i(point.x + x, point.y), true) != false){
444 | count++;
445 | }
446 | }
447 |
448 | if (count % 2 == 0)
449 | return false;
450 | else
451 | return true;
452 | }
453 |
454 | inline bool PointInPoly(QuadTree tree, Vec2i point){
455 | int count = 0; int id = FindIndex(tree, point.y);
456 |
457 | if (id > -1){
458 | QuadTree sub(tree.quads[id].points, 8);
459 | id = FindIndex(sub, point.y);
460 |
461 | for (int x = point.x; x < tree.rect.w; x++){
462 | if (PointExists(sub.quads[id].points, CreateVec2i(point.x + x, point.y), true) != false){
463 | count++;
464 | }
465 | }
466 |
467 | if (count % 2 == 0)
468 | return false;
469 | else
470 | return true;
471 | }
472 | else
473 | return false;
474 | }
475 |
476 | void MapPNG(string filename, vector points){
477 |
478 | }
479 |
480 | void MapPNG(string filename, QuadTree tree){
481 |
482 | }
483 |
484 | void MapPNG(string filename, QuadTree qt, int index){
485 | SDL_Rect rect = qt.getRect(index);
486 | SDL_Surface* temp = EmptySurface(rect.w - rect.x, rect.h - rect.y);
487 |
488 | for (int a = 0; a < qt.getSize(index); a++){
489 | if (hasPoint(temp, CreateVec2i(qt.getX(index,a), qt.getY(index,a))) != false)
490 | SetPixel(temp, qt.getX(index, a), qt.getY(index, a), SDL_MapRGBA(temp->format, 255, 255, 255, 255));
491 | }
492 |
493 | IMG_SavePNG(temp, filename.c_str());
494 | }
495 |
496 | vector PointTrace(SDL_Surface* surface){
497 | vector points;
498 | points.reserve(int(surface->w * surface->h));
499 |
500 | for (int y = 0; y < surface->h; y++){
501 | for (int x = 0; x < surface->w; x++){
502 | UMatrix3x3 neighbors;
503 |
504 | //Fill matrix with local color values
505 | for (int col = 0; col < 3; col++){
506 | for (int row = 0; row < 3; row++){
507 | int locX = x - 1 + row;
508 | int locY = y - 1 + col;
509 |
510 | Uint32 color; Uint8 g, b;
511 |
512 | if (locX < 0 || locX > surface->w - 1 || locY < 0 || locY > surface->h - 1)
513 | color = SDL_MapRGBA(surface->format, 1, 1, 1, 255);
514 | else
515 | color = GetPixel(surface, locX, locY);
516 |
517 | SDL_GetRGB(color, surface->format, &neighbors.val[col][row], &g, &b);
518 | }
519 | }
520 |
521 | //Check center value. If pixel isn't black,
522 | if (neighbors.val[1][1] != 0){
523 | //Check pixel values in all directions. If any neighbors are black, fill [1][1] with color
524 | if (int(neighbors.val[0][1]) == 0 || neighbors.val[1][0] == 0 || neighbors.val[1][2] == 0 || neighbors.val[2][1] == 0){
525 | points.push_back(CreateVec2i(x,y));
526 | }
527 | }
528 | }
529 | }
530 |
531 | return points;
532 | }
533 |
534 | void SearchPolygons(QuadTree tree, int index, Vec2i samples){
535 | SDL_Surface* temp = EmptySurface(tree.rect.w, tree.rect.h);
536 | SDL_Rect rect = tree.quads[index].rect;
537 |
538 | int w = rect.w - rect.x;
539 | int h = rect.h - rect.y;
540 |
541 | double dx = w / samples.x;
542 | double dy = h / samples.y;
543 |
544 | Vec2i loc;
545 |
546 | for (int y = 0; y < samples.y; y++){
547 | for (int x = 0; x < samples.x; x++){
548 | loc = CreateVec2i(rect.x + int(x * dx), rect.y + int(y * dy));
549 |
550 | if (PointInPoly(tree.quads[index].points, loc) != false){
551 | SetPixel(temp, loc.x, loc.y, SDL_MapRGBA(temp->format, 0, 0, 255, 255));
552 | }
553 | else
554 | SetPixel(temp, loc.x, loc.y, SDL_MapRGBA(temp->format, 255, 0, 0, 255));
555 | }
556 | }
557 |
558 | IMG_SavePNG(temp, "polygons.png");
559 | }
560 |
561 |
562 |
563 |
564 | //======================================================================================================================//
565 | // Current bugs, fixes, and feature ideas: //
566 | //----------------------------------------------------------------------------------------------------------------------//
567 | // Tasks: //
568 | // # To ensure highest accuracy, QuadTree input points in constructor should be divisible by total step size. //
569 | // Implement a strategy to avoid losing data. //
570 | // #
571 | //______________________________________________________________________________________________________________________//
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "SDL.h"
4 | #include "SDL_image.h"
5 | #include "SDL_ttf.h"
6 | #include "Transform.h"
7 | //#include "GUI.h"
8 |
9 | using namespace std;
10 | using namespace transform;
11 |
12 | const int SCREEN_WID = 1280;
13 | const int SCREEN_HEI = 960;
14 | const int WINDOW_WID = SCREEN_WID;
15 | const int WINDOW_HEI = SCREEN_HEI;
16 |
17 | int main(int argc, char* argv[]){
18 | if (SDL_Init(SDL_INIT_EVERYTHING) >0){
19 | cout << "Error: SDL failed to initialize." << endl;
20 | return 0;
21 | }
22 |
23 | SDL_Window* window = SDL_CreateWindow("SDL Image Manipulation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WID, WINDOW_HEI, NULL);
24 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
25 | SDL_Event event;
26 |
27 | //Load and modify image
28 | SDL_Surface* image = IMG_Load("Resources/test.jpg"); //Load source image
29 | SDL_Surface* filtered = EmptySurface(image); //Copy source image's pixel format, pixel data, and dimensions
30 | SDL_Surface* bg = EmptySurface(image, SCREEN_WID, SCREEN_HEI);
31 |
32 | //Generate background
33 | Fill(bg, CreateColor(50,50,50,255));
34 |
35 | SDL_Texture* tImage = SDL_CreateTextureFromSurface(renderer, image);
36 | SDL_Texture* tPost = SDL_CreateTextureFromSurface(renderer, filtered);
37 | SDL_Texture* back = SDL_CreateTextureFromSurface(renderer, bg);
38 |
39 | //Clean Up
40 | SDL_FreeSurface(image);
41 | SDL_FreeSurface(filtered);
42 |
43 | //Main loop
44 | bool quit = false;
45 |
46 | while (quit != true){
47 | while (SDL_PollEvent(&event) != 0){
48 | switch (event.key.keysym.sym){
49 | case SDLK_ESCAPE:
50 | quit = true;
51 | break;
52 | }
53 | }
54 |
55 | //Render media
56 | SDL_RenderClear(renderer);
57 | SDL_RenderCopy(renderer, back, NULL, NULL);
58 | SDL_RenderCopy(renderer, tImage, NULL, &CreateRect(0,SCREEN_HEI/4, SCREEN_WID/2, SCREEN_HEI/2));
59 | SDL_RenderCopy(renderer, tPost, NULL, &CreateRect(SCREEN_WID/2, SCREEN_HEI/4, SCREEN_WID/2, SCREEN_HEI/2));
60 | SDL_RenderPresent(renderer);
61 | }
62 |
63 | //Clean up
64 | SDL_Quit();
65 |
66 | return 0;
67 | }
--------------------------------------------------------------------------------