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
kiss_fftnd_alloc(const int * dims,int ndims,int inverse_fft,void * mem,size_t * lenmem)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;i<ndims;++i) {
37 size_t sublen=0;
38 kiss_fft_alloc (dims[i], inverse_fft, NULL, &sublen);
39 memneeded += sublen; /* st->states[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;i<ndims;++i) {
70 size_t len;
71 st->dims[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 */
kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx * fin,kiss_fft_cpx * fout)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 ; i<stride ; ++i )
182 kiss_fft_stride( st->states[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