fftwrap.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright (C) 2005 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. /*#define USE_SMALLFT*/
  31. #define USE_KISS_FFT
  32. #include "misc.h"
  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] = 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] = (in[i] + (1<<(shift-1))) >> 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. speex_warning("FFT should not be done in-place");
  87. float scale = 1./((struct drft_lookup *)table)->n;
  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. int i;
  103. speex_warning("FFT should not be done in-place");
  104. } else {
  105. int i;
  106. for (i=0;i<((struct drft_lookup *)table)->n;i++)
  107. out[i] = in[i];
  108. }
  109. spx_drft_backward((struct drft_lookup *)table, out);
  110. }
  111. #elif defined(USE_KISS_FFT)
  112. #include "kiss_fftr.h"
  113. #include "kiss_fft.h"
  114. struct kiss_config {
  115. kiss_fftr_cfg forward;
  116. kiss_fftr_cfg backward;
  117. kiss_fft_cpx *freq_data;
  118. int N;
  119. };
  120. void *spx_fft_init(int size)
  121. {
  122. struct kiss_config *table;
  123. table = speex_alloc(sizeof(struct kiss_config));
  124. table->freq_data = speex_alloc(sizeof(kiss_fft_cpx)*((size>>1)+1));
  125. table->forward = kiss_fftr_alloc(size,0,NULL,NULL);
  126. table->backward = kiss_fftr_alloc(size,1,NULL,NULL);
  127. table->N = size;
  128. return table;
  129. }
  130. void spx_fft_destroy(void *table)
  131. {
  132. struct kiss_config *t = (struct kiss_config *)table;
  133. kiss_fftr_free(t->forward);
  134. kiss_fftr_free(t->backward);
  135. speex_free(t->freq_data);
  136. speex_free(table);
  137. }
  138. #ifdef FIXED_POINT
  139. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  140. {
  141. int i;
  142. int shift;
  143. struct kiss_config *t = (struct kiss_config *)table;
  144. shift = maximize_range(in, in, 32000, t->N);
  145. kiss_fftr(t->forward, in, t->freq_data);
  146. out[0] = t->freq_data[0].r;
  147. for (i=1;i<t->N>>1;i++)
  148. {
  149. out[(i<<1)-1] = t->freq_data[i].r;
  150. out[(i<<1)] = t->freq_data[i].i;
  151. }
  152. out[(i<<1)-1] = t->freq_data[i].r;
  153. renorm_range(in, in, shift, t->N);
  154. renorm_range(out, out, shift, t->N);
  155. }
  156. #else
  157. void spx_fft(void *table, spx_word16_t *in, spx_word16_t *out)
  158. {
  159. int i;
  160. float scale;
  161. struct kiss_config *t = (struct kiss_config *)table;
  162. scale = 1./t->N;
  163. kiss_fftr(t->forward, in, t->freq_data);
  164. out[0] = scale*t->freq_data[0].r;
  165. for (i=1;i<t->N>>1;i++)
  166. {
  167. out[(i<<1)-1] = scale*t->freq_data[i].r;
  168. out[(i<<1)] = scale*t->freq_data[i].i;
  169. }
  170. out[(i<<1)-1] = scale*t->freq_data[i].r;
  171. }
  172. #endif
  173. void spx_ifft(void *table, spx_word16_t *in, spx_word16_t *out)
  174. {
  175. int i;
  176. struct kiss_config *t = (struct kiss_config *)table;
  177. t->freq_data[0].r = in[0];
  178. t->freq_data[0].i = 0;
  179. for (i=1;i<t->N>>1;i++)
  180. {
  181. t->freq_data[i].r = in[(i<<1)-1];
  182. t->freq_data[i].i = in[(i<<1)];
  183. }
  184. t->freq_data[i].r = in[(i<<1)-1];
  185. t->freq_data[i].i = 0;
  186. kiss_fftri(t->backward, t->freq_data, out);
  187. }
  188. #else
  189. #error No other FFT implemented
  190. #endif
  191. int fixed_point = 1;
  192. #ifdef FIXED_POINT
  193. #include "smallft.h"
  194. void spx_fft_float(void *table, float *in, float *out)
  195. {
  196. int i;
  197. #ifdef USE_SMALLFT
  198. int N = ((struct drft_lookup *)table)->n;
  199. #elif defined(USE_KISS_FFT)
  200. int N = ((struct kiss_config *)table)->N;
  201. #else
  202. #endif
  203. spx_word16_t _in[N];
  204. spx_word16_t _out[N];
  205. for (i=0;i<N;i++)
  206. _in[i] = (int)floor(.5+in[i]);
  207. spx_fft(table, _in, _out);
  208. for (i=0;i<N;i++)
  209. out[i] = _out[i];
  210. if (!fixed_point)
  211. {
  212. float scale;
  213. struct drft_lookup t;
  214. spx_drft_init(&t, ((struct kiss_config *)table)->N);
  215. scale = 1./((struct kiss_config *)table)->N;
  216. for (i=0;i<((struct kiss_config *)table)->N;i++)
  217. out[i] = scale*in[i];
  218. spx_drft_forward(&t, out);
  219. spx_drft_clear(&t);
  220. }
  221. }
  222. void spx_ifft_float(void *table, float *in, float *out)
  223. {
  224. int i;
  225. #ifdef USE_SMALLFT
  226. int N = ((struct drft_lookup *)table)->n;
  227. #elif defined(USE_KISS_FFT)
  228. int N = ((struct kiss_config *)table)->N;
  229. #else
  230. #endif
  231. spx_word16_t _in[N];
  232. spx_word16_t _out[N];
  233. for (i=0;i<N;i++)
  234. _in[i] = (int)floor(.5+in[i]);
  235. spx_ifft(table, _in, _out);
  236. for (i=0;i<N;i++)
  237. out[i] = _out[i];
  238. if (!fixed_point)
  239. {
  240. int i;
  241. struct drft_lookup t;
  242. spx_drft_init(&t, ((struct kiss_config *)table)->N);
  243. for (i=0;i<((struct kiss_config *)table)->N;i++)
  244. out[i] = in[i];
  245. spx_drft_backward(&t, out);
  246. spx_drft_clear(&t);
  247. }
  248. }
  249. #else
  250. void spx_fft_float(void *table, float *in, float *out)
  251. {
  252. spx_fft(table, in, out);
  253. }
  254. void spx_ifft_float(void *table, float *in, float *out)
  255. {
  256. spx_ifft(table, in, out);
  257. }
  258. #endif
粤ICP备19079148号