libdspl-2.0
Библиотека алгоритмов цифровой обработки сигналов
fft_create.c
1 /*
2 * Copyright (c) 2015-2024 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of libdspl-2.0.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * DSPL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <float.h>
25 
26 #include "dspl.h"
27 #include "dft.h"
28 
29 
30 
31 
32 
33 #ifdef DOXYGEN_ENGLISH
34 
95 #endif
96 #ifdef DOXYGEN_RUSSIAN
97 
158 #endif
159 int DSPL_API fft_create(fft_t* pfft, int n)
160 {
161 
162  int n1, n2, addr, s, k, m, nw, err;
163  double phi;
164  s = n;
165  nw = addr = 0;
166 
167  if(pfft->n == n)
168  return RES_OK;
169 
170  while(s > 1)
171  {
172  n2 = 1;
173  if(s%4096 == 0) { n2 = 4096; goto label_size; }
174  if(s%2048 == 0) { n2 = 2048; goto label_size; }
175  if(s%1024 == 0) { n2 = 1024; goto label_size; }
176  if(s%512 == 0) { n2 = 512; goto label_size; }
177  if(s%256 == 0) { n2 = 256; goto label_size; }
178  if(s%128 == 0) { n2 = 128; goto label_size; }
179  if(s% 64 == 0) { n2 = 64; goto label_size; }
180  if(s% 32 == 0) { n2 = 32; goto label_size; }
181  if(s% 16 == 0) { n2 = 16; goto label_size; }
182  if(s% 7 == 0) { n2 = 7; goto label_size; }
183  if(s% 8 == 0) { n2 = 8; goto label_size; }
184  if(s% 5 == 0) { n2 = 5; goto label_size; }
185  if(s% 4 == 0) { n2 = 4; goto label_size; }
186  if(s% 3 == 0) { n2 = 3; goto label_size; }
187  if(s% 2 == 0) { n2 = 2; goto label_size; }
188 
189 
190 label_size:
191  if(n2 == 1)
192  {
193  if(s > FFT_COMPOSITE_MAX)
194  {
195  err = ERROR_FFT_SIZE;
196  goto error_proc;
197  }
198 
199  nw += s;
200  pfft->w = pfft->w ?
201  (complex_t*) realloc(pfft->w, nw*sizeof(complex_t)):
202  (complex_t*) malloc( nw*sizeof(complex_t));
203  for(k = 0; k < s; k++)
204  {
205  phi = - M_2PI * (double)k / (double)s;
206  RE(pfft->w[addr]) = cos(phi);
207  IM(pfft->w[addr]) = sin(phi);
208  addr++;
209  }
210  s = 1;
211  }
212  else
213  {
214  n1 = s / n2;
215  nw += s;
216  pfft->w = pfft->w ?
217  (complex_t*) realloc(pfft->w, nw*sizeof(complex_t)):
218  (complex_t*) malloc( nw*sizeof(complex_t));
219 
220  for(k = 0; k < n1; k++)
221  {
222  for(m = 0; m < n2; m++)
223  {
224  phi = - M_2PI * (double)(k*m) / (double)s;
225  RE(pfft->w[addr]) = cos(phi);
226  IM(pfft->w[addr]) = sin(phi);
227  addr++;
228  }
229  }
230  }
231  s /= n2;
232  }
233 
234  pfft->t0 = pfft->t0 ? (complex_t*) realloc(pfft->t0, n*sizeof(complex_t)):
235  (complex_t*) malloc( n*sizeof(complex_t));
236 
237  pfft->t1 = pfft->t1 ? (complex_t*) realloc(pfft->t1, n*sizeof(complex_t)):
238  (complex_t*) malloc( n*sizeof(complex_t));
239  pfft->n = n;
240 
241  /* w32 fill */
242  addr = 0;
243  for(k = 0; k < 4; k++)
244  {
245  for(m = 0; m < 8; m++)
246  {
247  phi = - M_2PI * (double)(k*m) / 32.0;
248  RE(pfft->w32[addr]) = cos(phi);
249  IM(pfft->w32[addr]) = sin(phi);
250  addr++;
251  }
252  }
253 
254 
255  /* w64 fill */
256  addr = 0;
257  for(k = 0; k < 8; k++)
258  {
259  for(m = 0; m < 8; m++)
260  {
261  phi = - M_2PI * (double)(k*m) / 64.0;
262  RE(pfft->w64[addr]) = cos(phi);
263  IM(pfft->w64[addr]) = sin(phi);
264  addr++;
265  }
266  }
267 
268  /* w128 fill */
269  addr = 0;
270  for(k = 0; k < 8; k++)
271  {
272  for(m = 0; m < 16; m++)
273  {
274  phi = - M_2PI * (double)(k*m) / 128.0;
275  RE(pfft->w128[addr]) = cos(phi);
276  IM(pfft->w128[addr]) = sin(phi);
277  addr++;
278  }
279  }
280 
281  /* w256 fill */
282  addr = 0;
283  for(k = 0; k < 16; k++)
284  {
285  for(m = 0; m < 16; m++)
286  {
287  phi = - M_2PI * (double)(k*m) / 256.0;
288  RE(pfft->w256[addr]) = cos(phi);
289  IM(pfft->w256[addr]) = sin(phi);
290  addr++;
291  }
292  }
293 
294  /* w512 fill */
295  addr = 0;
296  for(k = 0; k < 16; k++)
297  {
298  for(m = 0; m < 32; m++)
299  {
300  phi = - M_2PI * (double)(k*m) / 512.0;
301  RE(pfft->w512[addr]) = cos(phi);
302  IM(pfft->w512[addr]) = sin(phi);
303  addr++;
304  }
305  }
306 
307  /* w1024 fill */
308  if(pfft->w1024 == NULL)
309  {
310  pfft->w1024 = (complex_t*) malloc(1024 * sizeof(complex_t));
311  addr = 0;
312  for(k = 0; k < 32; k++)
313  {
314  for(m = 0; m < 32; m++)
315  {
316  phi = - M_2PI * (double)(k*m) / 1024.0;
317  RE(pfft->w1024[addr]) = cos(phi);
318  IM(pfft->w1024[addr]) = sin(phi);
319  addr++;
320  }
321  }
322  }
323 
324  /* w2048 fill */
325  if(pfft->w2048 == NULL)
326  {
327  pfft->w2048= (complex_t*) malloc(2048 * sizeof(complex_t));
328  addr = 0;
329  for(k = 0; k < 32; k++)
330  {
331  for(m = 0; m < 64; m++)
332  {
333  phi = - M_2PI * (double)(k*m) / 2048.0;
334  RE(pfft->w2048[addr]) = cos(phi);
335  IM(pfft->w2048[addr]) = sin(phi);
336  addr++;
337  }
338  }
339  }
340 
341  /* w4096 fill */
342  if(pfft->w4096 == NULL)
343  {
344  pfft->w4096= (complex_t*) malloc(4096 * sizeof(complex_t));
345  addr = 0;
346  for(k = 0; k < 16; k++)
347  {
348  for(m = 0; m < 256; m++)
349  {
350  phi = - M_2PI * (double)(k*m) / 4096.0;
351  RE(pfft->w4096[addr]) = cos(phi);
352  IM(pfft->w4096[addr]) = sin(phi);
353  addr++;
354  }
355  }
356  }
357 
358  return RES_OK;
359 error_proc:
360  if(pfft->t0) free(pfft->t0);
361  if(pfft->t1) free(pfft->t1);
362  if(pfft->w) free(pfft->w);
363  pfft->n = 0;
364  return err;
365 }
complex_t * w1024
Definition: dspl.h:289
complex_t * t1
Definition: dspl.h:281
complex_t * t0
Definition: dspl.h:280
#define RE(x)
Макрос определяющий реальную часть комплексного числа.
Definition: dspl.h:420
int n
Definition: dspl.h:292
complex_t w512[512]
Definition: dspl.h:288
int fft_create(fft_t *pfft, int n)
Заполнение структуры fft_t для алгоритма БПФ
Definition: fft_create.c:159
Структура данных объекта быстрого преобразования Фурье
Definition: dspl.h:277
complex_t * w4096
Definition: dspl.h:291
complex_t w64[64]
Definition: dspl.h:285
double complex_t[2]
Описание комплексного типа данных.
Definition: dspl.h:86
complex_t w32[32]
Definition: dspl.h:284
#define RES_OK
Функция завершилась корректно. Ошибки отсутствуют.
Definition: dspl.h:558
complex_t * w
Definition: dspl.h:279
#define ERROR_FFT_SIZE
Неверно задан размер БПФ. Размер БПФ может быть составным вида , где , а – произвольный простой множ...
Definition: dspl.h:571
complex_t * w2048
Definition: dspl.h:290
complex_t w128[128]
Definition: dspl.h:286
complex_t w256[256]
Definition: dspl.h:287
#define IM(x)
Макрос определяющий мнимую часть комплексного числа.
Definition: dspl.h:478