fftwrap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Copyright (C) 2005-2006 Jean-Marc Valin
  2. File: fftwrap.c
  3. Wrapper for various FFTs
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "arch.h"
  31. #include "os_support.h"
  32. #define MAX_FFT_SIZE 2048
  33. #ifdef FIXED_POINT
  34. static int maximize_range(spx_word16_t *in, spx_word16_t *out, spx_word16_t bound, int len)
  35. {
  36. int i, shift;
  37. spx_word16_t max_val = 0;
  38. for (i=0;i<len;i++)
  39. {
  40. if (in[i]>max_val)
  41. max_val = in[i];
  42. if (-in[i]>max_val)
  43. max_val = -in[i];
  44. }
  45. shift=0;
  46. while (max_val <= (bound>>1) && max_val != 0)
  47. {
  48. max_val <<= 1;
  49. shift++;
  50. }
  51. for (i=0;i<len;i++)
  52. {
  53. out[i] = SHL16(in[i], shift);
  54. }
  55. return shift;
  56. }
  57. static void renorm_range(spx_word16_t *in, spx_word16_t *out, int shift, int len)
  58. {
  59. int i;
  60. for (i=0;i<len;i++)
  61. {
  62. out[i] = PSHR16(in[i], shift);
  63. }
  64. }
  65. #endif
  66. #ifdef USE_SMALLFT
  67. #include "smallft.h"
  68. #include <math.h>
  69. void *spx_fft_init(int size)
  70. {
  71. struct drft_lookup *table;
  72. table = speex_alloc(sizeof(struct drft_lookup));
  73. spx_drft_init((struct drft_lookup *)table, size);
  74. return (void*)table;
  75. }
  76. void spx_fft_destroy(void *table)
  77. {
  78. spx_drft_clear(table);
  79. speex_free(table);
  80. }
  81. void spx_fft(void *table, float *in, float *out)
  82. {
  83. if (in==out)
  84. {
  85. int i;
  86. float scale = 1./((struct drft_lookup *)table)->n;
  87. speex_warning("FFT should not be done in-place");
  88. for (i=0;i<((struct drft_lookup *)table)->n;i++)
  89. out[i] = scale*in[i];
  90. } else {
  91. int i;
  92. float scale = 1./((struct drft_lookup *)table)->n;
  93. for (i=0;i<((struct drft_lookup *)table)->n;i++)
  94. out[i] = scale*in[i];
  95. }
  96. spx_drft_forward((struct drft_lookup *)table, out);
  97. }
  98. void spx_ifft(void *table, float *in, float *out)
  99. {
  100. if (in==out)
  101. {
  102. speex_warning("FFT should not be done in-place");
  103. } else {
  104. int i;
  105. for (i=0;i<((struct drft_lookup *)table)->n;i++)
  106. out[i] = in[i];
  107. }
  108. spx_drft_backward((struct drft_lookup *)table, out);
  109. }
  110. #elif defined(USE_INTEL_MKL)
  111. #include <mkl.h>
  112. struct mkl_config {
  113. DFTI_DESCRIPTOR_HANDLE desc;
  114. int N;
  115. };
  116. void *spx_fft_init(int size)
  117. {
  118. struct mkl_config *table = (struct mkl_config *) speex_alloc(sizeof(struct mkl_config));
  119. table->N = size;
  120. DftiCreateDescriptor(&table->desc, DFTI_SINGLE, DFTI_REAL, 1, size);
  121. DftiSetValue(table->desc, DFTI_PACKED_FORMAT, DFTI_PACK_FORMAT);
  122. DftiSetValue(table->desc, DFTI_PLACEMENT, DFTI_NOT_INPLACE);
  123. DftiSetValue(table->desc, DFTI_FORWARD_SCALE, 1.0f / size);
  124. DftiCommitDescriptor(table->desc);
  125. return table;
  126. }
  127. void spx_fft_destroy(void *table)
  128. {
  129. struct mkl_config *t = (struct mkl_config *) table;
  130. DftiFreeDescriptor(t->desc);
  131. speex_free(table);
  132. }
  133. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  134. {
  135. struct mkl_config *t = (struct mkl_config *) table;
  136. DftiComputeForward(t->desc, in, out);
  137. }
  138. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  139. {
  140. struct mkl_config *t = (struct mkl_config *) table;
  141. DftiComputeBackward(t->desc, in, out);
  142. }
  143. #elif defined(USE_INTEL_IPP)
  144. #include <ipps.h>
  145. struct ipp_fft_config
  146. {
  147. IppsDFTSpec_R_32f *dftSpec;
  148. Ipp8u *buffer;
  149. };
  150. void *spx_fft_init(int size)
  151. {
  152. int bufferSize = 0;
  153. int hint;
  154. struct ipp_fft_config *table;
  155. table = (struct ipp_fft_config *)speex_alloc(sizeof(struct ipp_fft_config));
  156. /* there appears to be no performance difference between ippAlgHintFast and
  157. ippAlgHintAccurate when using the with the floating point version
  158. of the fft. */
  159. hint = ippAlgHintAccurate;
  160. ippsDFTInitAlloc_R_32f(&table->dftSpec, size, IPP_FFT_DIV_FWD_BY_N, hint);
  161. ippsDFTGetBufSize_R_32f(table->dftSpec, &bufferSize);
  162. table->buffer = ippsMalloc_8u(bufferSize);
  163. return table;
  164. }
  165. void spx_fft_destroy(void *table)
  166. {
  167. struct ipp_fft_config *t = (struct ipp_fft_config *)table;
  168. ippsFree(t->buffer);
  169. ippsDFTFree_R_32f(t->dftSpec);
  170. speex_free(t);
  171. }
  172. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  173. {
  174. struct ipp_fft_config *t = (struct ipp_fft_config *)table;
  175. ippsDFTFwd_RToPack_32f(in, out, t->dftSpec, t->buffer);
  176. }
  177. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  178. {
  179. struct ipp_fft_config *t = (struct ipp_fft_config *)table;
  180. ippsDFTInv_PackToR_32f(in, out, t->dftSpec, t->buffer);
  181. }
  182. #elif defined(USE_GPL_FFTW3)
  183. #include <fftw3.h>
  184. struct fftw_config {
  185. float *in;
  186. float *out;
  187. fftwf_plan fft;
  188. fftwf_plan ifft;
  189. int N;
  190. };
  191. void *spx_fft_init(int size)
  192. {
  193. struct fftw_config *table = (struct fftw_config *) speex_alloc(sizeof(struct fftw_config));
  194. table->in = fftwf_malloc(sizeof(float) * (size+2));
  195. table->out = fftwf_malloc(sizeof(float) * (size+2));
  196. table->fft = fftwf_plan_dft_r2c_1d(size, table->in, (fftwf_complex *) table->out, FFTW_PATIENT);
  197. table->ifft = fftwf_plan_dft_c2r_1d(size, (fftwf_complex *) table->in, table->out, FFTW_PATIENT);
  198. table->N = size;
  199. return table;
  200. }
  201. void spx_fft_destroy(void *table)
  202. {
  203. struct fftw_config *t = (struct fftw_config *) table;
  204. fftwf_destroy_plan(t->fft);
  205. fftwf_destroy_plan(t->ifft);
  206. fftwf_free(t->in);
  207. fftwf_free(t->out);
  208. speex_free(table);
  209. }
  210. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  211. {
  212. int i;
  213. struct fftw_config *t = (struct fftw_config *) table;
  214. const int N = t->N;
  215. float *iptr = t->in;
  216. float *optr = t->out;
  217. const float m = 1.0 / N;
  218. for(i=0;i<N;++i)
  219. iptr[i]=in[i] * m;
  220. fftwf_execute(t->fft);
  221. out[0] = optr[0];
  222. for(i=1;i<N;++i)
  223. out[i] = optr[i+1];
  224. }
  225. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  226. {
  227. int i;
  228. struct fftw_config *t = (struct fftw_config *) table;
  229. const int N = t->N;
  230. float *iptr = t->in;
  231. float *optr = t->out;
  232. iptr[0] = in[0];
  233. iptr[1] = 0.0f;
  234. for(i=1;i<N;++i)
  235. iptr[i+1] = in[i];
  236. iptr[N+1] = 0.0f;
  237. fftwf_execute(t->ifft);
  238. for(i=0;i<N;++i)
  239. out[i] = optr[i];
  240. }
  241. #elif defined(USE_KISS_FFT)
  242. #include "kiss_fftr.h"
  243. #include "kiss_fft.h"
  244. struct kiss_config {
  245. kiss_fftr_cfg forward;
  246. kiss_fftr_cfg backward;
  247. int N;
  248. };
  249. void *spx_fft_init(int size)
  250. {
  251. struct kiss_config *table;
  252. table = (struct kiss_config*)speex_alloc(sizeof(struct kiss_config));
  253. table->forward = kiss_fftr_alloc(size,0,NULL,NULL);
  254. table->backward = kiss_fftr_alloc(size,1,NULL,NULL);
  255. table->N = size;
  256. return table;
  257. }
  258. void spx_fft_destroy(void *table)
  259. {
  260. struct kiss_config *t = (struct kiss_config *)table;
  261. kiss_fftr_free(t->forward);
  262. kiss_fftr_free(t->backward);
  263. speex_free(table);
  264. }
  265. #ifdef FIXED_POINT
  266. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  267. {
  268. int shift;
  269. struct kiss_config *t = (struct kiss_config *)table;
  270. shift = maximize_range(in, in, 32000, t->N);
  271. kiss_fftr2(t->forward, in, out);
  272. renorm_range(in, in, shift, t->N);
  273. renorm_range(out, out, shift, t->N);
  274. }
  275. #else
  276. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  277. {
  278. int i;
  279. float scale;
  280. struct kiss_config *t = (struct kiss_config *)table;
  281. scale = 1./t->N;
  282. kiss_fftr2(t->forward, in, out);
  283. for (i=0;i<t->N;i++)
  284. out[i] *= scale;
  285. }
  286. #endif
  287. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  288. {
  289. struct kiss_config *t = (struct kiss_config *)table;
  290. kiss_fftri2(t->backward, in, out);
  291. }
  292. #else
  293. #error No other FFT implemented
  294. #endif
  295. #ifdef FIXED_POINT
  296. /*#include "smallft.h"*/
  297. void spx_fft_float(void *table, float *in, float *out)
  298. {
  299. int i;
  300. #ifdef USE_SMALLFT
  301. int N = ((struct drft_lookup *)table)->n;
  302. #elif defined(USE_KISS_FFT)
  303. int N = ((struct kiss_config *)table)->N;
  304. #else
  305. #endif
  306. #ifdef VAR_ARRAYS
  307. spx_word16_t _in[N];
  308. spx_word16_t _out[N];
  309. #else
  310. spx_word16_t _in[MAX_FFT_SIZE];
  311. spx_word16_t _out[MAX_FFT_SIZE];
  312. #endif
  313. for (i=0;i<N;i++)
  314. _in[i] = (int)floor(.5+in[i]);
  315. spx_fft(table, _in, _out);
  316. for (i=0;i<N;i++)
  317. out[i] = _out[i];
  318. #if 0
  319. if (!fixed_point)
  320. {
  321. float scale;
  322. struct drft_lookup t;
  323. spx_drft_init(&t, ((struct kiss_config *)table)->N);
  324. scale = 1./((struct kiss_config *)table)->N;
  325. for (i=0;i<((struct kiss_config *)table)->N;i++)
  326. out[i] = scale*in[i];
  327. spx_drft_forward(&t, out);
  328. spx_drft_clear(&t);
  329. }
  330. #endif
  331. }
  332. void spx_ifft_float(void *table, float *in, float *out)
  333. {
  334. int i;
  335. #ifdef USE_SMALLFT
  336. int N = ((struct drft_lookup *)table)->n;
  337. #elif defined(USE_KISS_FFT)
  338. int N = ((struct kiss_config *)table)->N;
  339. #else
  340. #endif
  341. #ifdef VAR_ARRAYS
  342. spx_word16_t _in[N];
  343. spx_word16_t _out[N];
  344. #else
  345. spx_word16_t _in[MAX_FFT_SIZE];
  346. spx_word16_t _out[MAX_FFT_SIZE];
  347. #endif
  348. for (i=0;i<N;i++)
  349. _in[i] = (int)floor(.5+in[i]);
  350. spx_ifft(table, _in, _out);
  351. for (i=0;i<N;i++)
  352. out[i] = _out[i];
  353. #if 0
  354. if (!fixed_point)
  355. {
  356. int i;
  357. struct drft_lookup t;
  358. spx_drft_init(&t, ((struct kiss_config *)table)->N);
  359. for (i=0;i<((struct kiss_config *)table)->N;i++)
  360. out[i] = in[i];
  361. spx_drft_backward(&t, out);
  362. spx_drft_clear(&t);
  363. }
  364. #endif
  365. }
  366. #else
  367. void spx_fft_float(void *table, float *in, float *out)
  368. {
  369. spx_fft(table, in, out);
  370. }
  371. void spx_ifft_float(void *table, float *in, float *out)
  372. {
  373. spx_ifft(table, in, out);
  374. }
  375. #endif