Contiki 2.5
ifft.c
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * -----------------------------------------------------------------
30  * ifft - Integer FFT (fast fourier transform) library
31  *
32  *
33  * Author : Joakim Eriksson
34  * Created : 2008-03-27
35  * Updated : $Date: 2008/07/03 23:40:12 $
36  * $Revision: 1.3 $
37  */
38 #include "lib/ifft.h"
39 
40 /*---------------------------------------------------------------------------*/
41 /* constant table of sin values in 8/7 bits resolution */
42 /* NOTE: symmetry can be used to reduce this to 1/2 or 1/4 the size */
43 #define SIN_TAB_LEN 120
44 #define RESOLUTION 7
45 #define ABS(x) (x < 0 ? -x : x)
46 
47 static const int8_t SIN_TAB[] = {
48  0,6,13,20,26,33,39,45,52,58,63,69,75,80,
49  85,90,95,99,103,107,110,114,116,119,121,
50  123,125,126,127,127,127,127,127,126,125,
51  123,121,119,116,114,110,107,103,99,95,90,
52  85,80,75,69,63,58,52,45,39,33,26,20,13,6,
53  0,-6,-13,-20,-26,-33,-39,-45,-52,-58,-63,
54  -69,-75,-80,-85,-90,-95,-99,-103,-107,-110,
55  -114,-116,-119,-121,-123,-125,-126,-127,-127,
56  -127,-127,-127,-126,-125,-123,-121,-119,-116,
57  -114,-110,-107,-103,-99,-95,-90,-85,-80,-75,
58  -69,-63,-58,-52,-45,-39,-33,-26,-20,-13,-6
59 };
60 
61 
62 static uint16_t bitrev(uint16_t j, uint16_t nu)
63 {
64  uint16_t k;
65  k = 0;
66  for (; nu > 0; nu--) {
67  k = (k << 1) + (j & 1);
68  j = j >> 1;
69  }
70  return k;
71 }
72 
73 
74 /* Non interpolating sine... which takes an angle of 0 - 999 */
75 static int16_t sinI(uint16_t angleMilli)
76 {
77  uint16_t pos;
78  pos = (uint16_t) ((SIN_TAB_LEN * (uint32_t) angleMilli) / 1000);
79  return SIN_TAB[pos % SIN_TAB_LEN];
80 }
81 
82 static int16_t cosI(uint16_t angleMilli)
83 {
84  return sinI(angleMilli + 250);
85 }
86 
87 static uint16_t ilog2(uint16_t val)
88 {
89  uint16_t log;
90  log = 0;
91  val = val >> 1; /* The 20 = 1 => log = 0 => val = 0 */
92  while (val > 0) {
93  val = val >> 1;
94  log++;
95  }
96  return log;
97 }
98 
99 
100 /* ifft(xre[], n) - integer (fixpoint) version of Fast Fourier Transform
101  An integer version of FFT that takes in-samples in an int16_t array
102  and does an fft on n samples in the array.
103  The result of the FFT is stored in the same array as the samples
104  was stored. Them imaginary part of the result is stored in xim which
105  needs to be of the same size as xre (e.g. n ints).
106 
107  Note: This fft is designed to be used with 8 bit values (e.g. not
108  16 bit values). The reason for the int16_t array is for keeping some
109  'room' for the calculations. It is also designed for doing fairly small
110  FFT:s since to large sample arrays might cause it to overflow during
111  calculations.
112 */
113 void
114 ifft(int16_t xre[], int16_t xim[], uint16_t n)
115 {
116  uint16_t nu;
117  uint16_t n2;
118  uint16_t nu1;
119  int p, k, l, i;
120  int32_t c, s, tr, ti;
121 
122  nu = ilog2(n);
123  nu1 = nu - 1;
124  n2 = n / 2;
125 
126  for (i = 0; i < n; i++)
127  xim[i] = 0;
128 
129  for (l = 1; l <= nu; l++) {
130  for (k = 0; k < n; k += n2) {
131  for (i = 1; i <= n2; i++) {
132  p = bitrev(k >> nu1, nu);
133  c = cosI((1000 * p) / n);
134  s = sinI((1000 * p) / n);
135 
136  tr = ((xre[k + n2] * c + xim[k + n2] * s) >> RESOLUTION);
137  ti = ((xim[k + n2] * c - xre[k + n2] * s) >> RESOLUTION);
138 
139  xre[k + n2] = xre[k] - tr;
140  xim[k + n2] = xim[k] - ti;
141  xre[k] += tr;
142  xim[k] += ti;
143  k++;
144  }
145  }
146  nu1--;
147  n2 = n2 / 2;
148  }
149 
150  for (k = 0; k < n; k++) {
151  p = bitrev(k, nu);
152  if (p > k) {
153  n2 = xre[k];
154  xre[k] = xre[p];
155  xre[p] = n2;
156 
157  n2 = xim[k];
158  xim[k] = xim[p];
159  xim[p] = n2;
160  }
161  }
162 
163  /* This is a fast but not 100% correct magnitude calculation */
164  /* Should be sqrt(xre[i]^2 + xim[i]^2) and normalized with div. by n */
165  for (i = 0, n2 = n / 2; i < n2; i++) {
166  xre[i] = (ABS(xre[i]) + ABS(xim[i]));
167  }
168 }