Debian Codegraph @

src/kiss_fft/kiss_fftnd.c

raw symbols SCIP
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 size_t n_align;
35 char * ptr;
36
37 for (i=0;i<ndims;++i) {
38 size_t sublen=0;
39 kiss_fft_alloc (dims[i], inverse_fft, NULL, &sublen);
40 memneeded += sublen; /* st->states[i] */
41 dimprod *= dims[i];
42 }
43 memneeded += sizeof(int) * ndims;/* st->dims */
44 memneeded += sizeof(void*) * ndims;/* st->states */
45 n_align = 16 - memneeded % 16; /* 16 byte alignment */
46 memneeded += n_align;
47 memneeded += sizeof(kiss_fft_cpx) * dimprod; /* st->tmpbuf */
48
49 if (lenmem == NULL) {/* allocate for the caller*/
50 st = (kiss_fftnd_cfg) KISS_FFT_MALLOC (memneeded);
51 } else { /* initialize supplied buffer if big enough */
52 if (*lenmem >= memneeded)
53 st = (kiss_fftnd_cfg) mem;
54 *lenmem = memneeded; /*tell caller how big struct is (or would be) */
55 }
56 if (!st)
57 return NULL; /*malloc failed or buffer too small */
58
59 st->dimprod = dimprod;
60 st->ndims = ndims;
61 ptr=(char*)(st+1);
62
63 st->states = (kiss_fft_cfg *)ptr;
64 ptr += sizeof(void*) * ndims;
65
66 st->dims = (int*)ptr;
67 ptr += sizeof(int) * ndims;
68
69 ptr += n_align; /* 16 byte alignment */
70 st->tmpbuf = (kiss_fft_cpx*)ptr;
71 ptr += sizeof(kiss_fft_cpx) * dimprod;
72
73 for (i=0;i<ndims;++i) {
74 size_t len;
75 st->dims[i] = dims[i];
76 kiss_fft_alloc (st->dims[i], inverse_fft, NULL, &len);
77 st->states[i] = kiss_fft_alloc (st->dims[i], inverse_fft, ptr,&len);
78 ptr += len;
79 }
80 /*
81 Hi there!
82
83 If you're looking at this particular code, it probably means you've got a brain-dead bounds checker
84 that thinks the above code overwrites the end of the array.
85
86 It doesn't.
87
88 -- Mark
89
90 P.S.
91 The below code might give you some warm fuzzies and help convince you.
92 */
93 if ( (size_t)(ptr - (char*)st) != memneeded ) {
94 fprintf(stderr,
95 "################################################################################\n"
96 "Internal error! Memory allocation miscalculation\n"
97 "################################################################################\n"
98 );
99 }
100 return st;
101 }
102
103 /*
104 This works by tackling one dimension at a time.
105
106 In effect,
107 Each stage starts out by reshaping the matrix into a DixSi 2d matrix.
108 A Di-sized fft is taken of each column, transposing the matrix as it goes.
109
110 Here's a 3-d example:
111 Take a 2x3x4 matrix, laid out in memory as a contiguous buffer
112 [ [ [ a b c d ] [ e f g h ] [ i j k l ] ]
113 [ [ m n o p ] [ q r s t ] [ u v w x ] ] ]
114
115 Stage 0 ( D=2): treat the buffer as a 2x12 matrix
116 [ [a b ... k l]
117 [m n ... w x] ]
118
119 FFT each column with size 2.
120 Transpose the matrix at the same time using kiss_fft_stride.
121
122 [ [ a+m a-m ]
123 [ b+n b-n]
124 ...
125 [ k+w k-w ]
126 [ l+x l-x ] ]
127
128 Note fft([x y]) == [x+y x-y]
129
130 Stage 1 ( D=3) treats the buffer (the output of stage D=2) as an 3x8 matrix,
131 [ [ a+m a-m b+n b-n c+o c-o d+p d-p ]
132 [ e+q e-q f+r f-r g+s g-s h+t h-t ]
133 [ i+u i-u j+v j-v k+w k-w l+x l-x ] ]
134
135 And perform FFTs (size=3) on each of the columns as above, transposing
136 the matrix as it goes. The output of stage 1 is
137 (Legend: ap = [ a+m e+q i+u ]
138 am = [ a-m e-q i-u ] )
139
140 [ [ sum(ap) fft(ap)[0] fft(ap)[1] ]
141 [ sum(am) fft(am)[0] fft(am)[1] ]
142 [ sum(bp) fft(bp)[0] fft(bp)[1] ]
143 [ sum(bm) fft(bm)[0] fft(bm)[1] ]
144 [ sum(cp) fft(cp)[0] fft(cp)[1] ]
145 [ sum(cm) fft(cm)[0] fft(cm)[1] ]
146 [ sum(dp) fft(dp)[0] fft(dp)[1] ]
147 [ sum(dm) fft(dm)[0] fft(dm)[1] ] ]
148
149 Stage 2 ( D=4) treats this buffer as a 4*6 matrix,
150 [ [ sum(ap) fft(ap)[0] fft(ap)[1] sum(am) fft(am)[0] fft(am)[1] ]
151 [ sum(bp) fft(bp)[0] fft(bp)[1] sum(bm) fft(bm)[0] fft(bm)[1] ]
152 [ sum(cp) fft(cp)[0] fft(cp)[1] sum(cm) fft(cm)[0] fft(cm)[1] ]
153 [ sum(dp) fft(dp)[0] fft(dp)[1] sum(dm) fft(dm)[0] fft(dm)[1] ] ]
154
155 Then FFTs each column, transposing as it goes.
156
157 The resulting matrix is the 3d FFT of the 2x3x4 input matrix.
158
159 Note as a sanity check that the first element of the final
160 stage's output (DC term) is
161 sum( [ sum(ap) sum(bp) sum(cp) sum(dp) ] )
162 , i.e. the summation of all 24 input elements.
163
164 */
165 void kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
166 {
167 int i,k;
168 const kiss_fft_cpx * bufin=fin;
169 kiss_fft_cpx * bufout;
170
171 /*arrange it so the last bufout == fout*/
172 if ( st->ndims & 1 ) {
173 bufout = fout;
174 if (fin==fout) {
175 memcpy( st->tmpbuf, fin, sizeof(kiss_fft_cpx) * st->dimprod );
176 bufin = st->tmpbuf;
177 }
178 }else
179 bufout = st->tmpbuf;
180
181 for ( k=0; k < st->ndims; ++k) {
182 int curdim = st->dims[k];
183 int stride = st->dimprod / curdim;
184
185 for ( i=0 ; i<stride ; ++i )
186 kiss_fft_stride( st->states[k], bufin+i , bufout+i*curdim, stride );
187
188 /*toggle back and forth between the two buffers*/
189 if (bufout == st->tmpbuf){
190 bufout = fout;
191 bufin = st->tmpbuf;
192 }else{
193 bufout = st->tmpbuf;
194 bufin = fout;
195 }
196 }
197 }