fftwrap.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #include "config.h"
  28. #include "arch.h"
  29. #include "os_support.h"
  30. #define MAX_FFT_SIZE 2048
  31. #ifdef FIXED_POINT
  32. static int maximize_range(spx_word16_t *in, spx_word16_t *out, spx_word16_t bound, int len)
  33. {
  34. int i, shift;
  35. spx_word16_t max_val = 0;
  36. for (i=0;i<len;i++)
  37. {
  38. if (in[i]>max_val)
  39. max_val = in[i];
  40. if (-in[i]>max_val)
  41. max_val = -in[i];
  42. }
  43. shift=0;
  44. while (max_val <= (bound>>1) && max_val != 0)
  45. {
  46. max_val <<= 1;
  47. shift++;
  48. }
  49. for (i=0;i<len;i++)
  50. {
  51. out[i] = SHL16(in[i], shift);
  52. }
  53. return shift;
  54. }
  55. static void renorm_range(spx_word16_t *in, spx_word16_t *out, int shift, int len)
  56. {
  57. int i;
  58. for (i=0;i<len;i++)
  59. {
  60. out[i] = PSHR16(in[i], shift);
  61. }
  62. }
  63. #endif
  64. #ifdef USE_SMALLFT
  65. #include "smallft.h"
  66. #include <math.h>
  67. void *spx_fft_init(int size)
  68. {
  69. struct drft_lookup *table;
  70. table = speex_alloc(sizeof(struct drft_lookup));
  71. spx_drft_init((struct drft_lookup *)table, size);
  72. return (void*)table;
  73. }
  74. void spx_fft_destroy(void *table)
  75. {
  76. spx_drft_clear(table);
  77. speex_free(table);
  78. }
  79. void spx_fft(void *table, float *in, float *out)
  80. {
  81. if (in==out)
  82. {
  83. int i;
  84. float scale = 1./((struct drft_lookup *)table)->n;
  85. speex_warning("FFT should not be done in-place");
  86. for (i=0;i<((struct drft_lookup *)table)->n;i++)
  87. out[i] = scale*in[i];
  88. } else {
  89. int i;
  90. float scale = 1./((struct drft_lookup *)table)->n;
  91. for (i=0;i<((struct drft_lookup *)table)->n;i++)
  92. out[i] = scale*in[i];
  93. }
  94. spx_drft_forward((struct drft_lookup *)table, out);
  95. }
  96. void spx_ifft(void *table, float *in, float *out)
  97. {
  98. if (in==out)
  99. {
  100. speex_warning("FFT should not be done in-place");
  101. } else {
  102. int i;
  103. for (i=0;i<((struct drft_lookup *)table)->n;i++)
  104. out[i] = in[i];
  105. }
  106. spx_drft_backward((struct drft_lookup *)table, out);
  107. }
  108. #elif defined(USE_INTEL_MKL)
  109. #include <mkl.h>
  110. struct mkl_config {
  111. DFTI_DESCRIPTOR_HANDLE desc;
  112. int N;
  113. };
  114. void *spx_fft_init(int size)
  115. {
  116. struct mkl_config *table = (struct mkl_config *) speex_alloc(sizeof(struct mkl_config));
  117. table->N = size;
  118. DftiCreateDescriptor(&table->desc, DFTI_SINGLE, DFTI_REAL, 1, size);
  119. DftiSetValue(table->desc, DFTI_PACKED_FORMAT, DFTI_PACK_FORMAT);
  120. DftiSetValue(table->desc, DFTI_PLACEMENT, DFTI_NOT_INPLACE);
  121. DftiSetValue(table->desc, DFTI_FORWARD_SCALE, 1.0f / size);
  122. DftiCommitDescriptor(table->desc);
  123. return table;
  124. }
  125. void spx_fft_destroy(void *table)
  126. {
  127. struct mkl_config *t = (struct mkl_config *) table;
  128. DftiFreeDescriptor(t->desc);
  129. speex_free(table);
  130. }
  131. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  132. {
  133. struct mkl_config *t = (struct mkl_config *) table;
  134. DftiComputeForward(t->desc, in, out);
  135. }
  136. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  137. {
  138. struct mkl_config *t = (struct mkl_config *) table;
  139. DftiComputeBackward(t->desc, in, out);
  140. }
  141. #elif defined(USE_GPL_FFTW3)
  142. #include <fftw3.h>
  143. struct fftw_config {
  144. float *in;
  145. float *out;
  146. fftwf_plan fft;
  147. fftwf_plan ifft;
  148. int N;
  149. };
  150. void *spx_fft_init(int size)
  151. {
  152. struct fftw_config *table = (struct fftw_config *) speex_alloc(sizeof(struct fftw_config));
  153. table->in = fftwf_malloc(sizeof(float) * (size+2));
  154. table->out = fftwf_malloc(sizeof(float) * (size+2));
  155. table->fft = fftwf_plan_dft_r2c_1d(size, table->in, (fftwf_complex *) table->out, FFTW_PATIENT);
  156. table->ifft = fftwf_plan_dft_c2r_1d(size, (fftwf_complex *) table->in, table->out, FFTW_PATIENT);
  157. table->N = size;
  158. return table;
  159. }
  160. void spx_fft_destroy(void *table)
  161. {
  162. struct fftw_config *t = (struct fftw_config *) table;
  163. fftwf_destroy_plan(t->fft);
  164. fftwf_destroy_plan(t->ifft);
  165. fftwf_free(t->in);
  166. fftwf_free(t->out);
  167. speex_free(table);
  168. }
  169. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  170. {
  171. int i;
  172. struct fftw_config *t = (struct fftw_config *) table;
  173. const int N = t->N;
  174. float *iptr = t->in;
  175. float *optr = t->out;
  176. const float m = 1.0 / N;
  177. for(i=0;i<N;++i)
  178. iptr[i]=in[i] * m;
  179. fftwf_execute(t->fft);
  180. out[0] = optr[0];
  181. for(i=1;i<N;++i)
  182. out[i] = optr[i+1];
  183. }
  184. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  185. {
  186. int i;
  187. struct fftw_config *t = (struct fftw_config *) table;
  188. const int N = t->N;
  189. float *iptr = t->in;
  190. float *optr = t->out;
  191. iptr[0] = in[0];
  192. iptr[1] = 0.0f;
  193. for(i=1;i<N;++i)
  194. iptr[i+1] = in[i];
  195. iptr[N+1] = 0.0f;
  196. fftwf_execute(t->ifft);
  197. for(i=0;i<N;++i)
  198. out[i] = optr[i];
  199. }
  200. #elif defined(USE_KISS_FFT)
  201. #include "kiss_fftr.h"
  202. #include "kiss_fft.h"
  203. struct kiss_config {
  204. kiss_fftr_cfg forward;
  205. kiss_fftr_cfg backward;
  206. int N;
  207. };
  208. void *spx_fft_init(int size)
  209. {
  210. struct kiss_config *table;
  211. table = (struct kiss_config*)speex_alloc(sizeof(struct kiss_config));
  212. table->forward = kiss_fftr_alloc(size,0,NULL,NULL);
  213. table->backward = kiss_fftr_alloc(size,1,NULL,NULL);
  214. table->N = size;
  215. return table;
  216. }
  217. void spx_fft_destroy(void *table)
  218. {
  219. struct kiss_config *t = (struct kiss_config *)table;
  220. kiss_fftr_free(t->forward);
  221. kiss_fftr_free(t->backward);
  222. speex_free(table);
  223. }
  224. #ifdef FIXED_POINT
  225. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  226. {
  227. int shift;
  228. struct kiss_config *t = (struct kiss_config *)table;
  229. shift = maximize_range(in, in, 32000, t->N);
  230. kiss_fftr2(t->forward, in, out);
  231. renorm_range(in, in, shift, t->N);
  232. renorm_range(out, out, shift, t->N);
  233. }
  234. #else
  235. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  236. {
  237. int i;
  238. float scale;
  239. struct kiss_config *t = (struct kiss_config *)table;
  240. scale = 1./t->N;
  241. kiss_fftr2(t->forward, in, out);
  242. for (i=0;i<t->N;i++)
  243. out[i] *= scale;
  244. }
  245. #endif
  246. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  247. {
  248. struct kiss_config *t = (struct kiss_config *)table;
  249. kiss_fftri2(t->backward, in, out);
  250. }
  251. #else
  252. #error No other FFT implemented
  253. #endif
  254. #ifdef FIXED_POINT
  255. /*#include "smallft.h"*/
  256. void spx_fft_float(void *table, float *in, float *out)
  257. {
  258. int i;
  259. #ifdef USE_SMALLFT
  260. int N = ((struct drft_lookup *)table)->n;
  261. #elif defined(USE_KISS_FFT)
  262. int N = ((struct kiss_config *)table)->N;
  263. #else
  264. #endif
  265. #ifdef VAR_ARRAYS
  266. spx_word16_t _in[N];
  267. spx_word16_t _out[N];
  268. #else
  269. spx_word16_t _in[MAX_FFT_SIZE];
  270. spx_word16_t _out[MAX_FFT_SIZE];
  271. #endif
  272. for (i=0;i<N;i++)
  273. _in[i] = (int)floor(.5+in[i]);
  274. spx_fft(table, _in, _out);
  275. for (i=0;i<N;i++)
  276. out[i] = _out[i];
  277. #if 0
  278. if (!fixed_point)
  279. {
  280. float scale;
  281. struct drft_lookup t;
  282. spx_drft_init(&t, ((struct kiss_config *)table)->N);
  283. scale = 1./((struct kiss_config *)table)->N;
  284. for (i=0;i<((struct kiss_config *)table)->N;i++)
  285. out[i] = scale*in[i];
  286. spx_drft_forward(&t, out);
  287. spx_drft_clear(&t);
  288. }
  289. #endif
  290. }
  291. void spx_ifft_float(void *table, float *in, float *out)
  292. {
  293. int i;
  294. #ifdef USE_SMALLFT
  295. int N = ((struct drft_lookup *)table)->n;
  296. #elif defined(USE_KISS_FFT)
  297. int N = ((struct kiss_config *)table)->N;
  298. #else
  299. #endif
  300. #ifdef VAR_ARRAYS
  301. spx_word16_t _in[N];
  302. spx_word16_t _out[N];
  303. #else
  304. spx_word16_t _in[MAX_FFT_SIZE];
  305. spx_word16_t _out[MAX_FFT_SIZE];
  306. #endif
  307. for (i=0;i<N;i++)
  308. _in[i] = (int)floor(.5+in[i]);
  309. spx_ifft(table, _in, _out);
  310. for (i=0;i<N;i++)
  311. out[i] = _out[i];
  312. #if 0
  313. if (!fixed_point)
  314. {
  315. int i;
  316. struct drft_lookup t;
  317. spx_drft_init(&t, ((struct kiss_config *)table)->N);
  318. for (i=0;i<((struct kiss_config *)table)->N;i++)
  319. out[i] = in[i];
  320. spx_drft_backward(&t, out);
  321. spx_drft_clear(&t);
  322. }
  323. #endif
  324. }
  325. #else
  326. void spx_fft_float(void *table, float *in, float *out)
  327. {
  328. spx_fft(table, in, out);
  329. }
  330. void spx_ifft_float(void *table, float *in, float *out)
  331. {
  332. spx_ifft(table, in, out);
  333. }
  334. #endif