Contiki-NG
Loading...
Searching...
No Matches
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
39#include "contiki.h"
40#include "lib/ifft.h"
41
42/*---------------------------------------------------------------------------*/
43/* constant table of sin values in 8/7 bits resolution */
44/* NOTE: symmetry can be used to reduce this to 1/2 or 1/4 the size */
45#define SIN_TAB_LEN 120
46#define RESOLUTION 7
47
48static const int8_t SIN_TAB[] = {
49 0,6,13,20,26,33,39,45,52,58,63,69,75,80,
50 85,90,95,99,103,107,110,114,116,119,121,
51 123,125,126,127,127,127,127,127,126,125,
52 123,121,119,116,114,110,107,103,99,95,90,
53 85,80,75,69,63,58,52,45,39,33,26,20,13,6,
54 0,-6,-13,-20,-26,-33,-39,-45,-52,-58,-63,
55 -69,-75,-80,-85,-90,-95,-99,-103,-107,-110,
56 -114,-116,-119,-121,-123,-125,-126,-127,-127,
57 -127,-127,-127,-126,-125,-123,-121,-119,-116,
58 -114,-110,-107,-103,-99,-95,-90,-85,-80,-75,
59 -69,-63,-58,-52,-45,-39,-33,-26,-20,-13,-6
60};
61
62
63static uint16_t bitrev(uint16_t j, uint16_t nu)
64{
65 uint16_t k;
66 k = 0;
67 for (; nu > 0; nu--) {
68 k = (k << 1) + (j & 1);
69 j = j >> 1;
70 }
71 return k;
72}
73
74
75/* Non interpolating sine... which takes an angle of 0 - 999 */
76static int16_t sinI(uint16_t angleMilli)
77{
78 uint16_t pos;
79 pos = (uint16_t) ((SIN_TAB_LEN * (uint32_t) angleMilli) / 1000);
80 return SIN_TAB[pos % SIN_TAB_LEN];
81}
82
83static int16_t cosI(uint16_t angleMilli)
84{
85 return sinI(angleMilli + 250);
86}
87
88static uint16_t ilog2(uint16_t val)
89{
90 uint16_t log;
91 log = 0;
92 val = val >> 1; /* The 20 = 1 => log = 0 => val = 0 */
93 while (val > 0) {
94 val = val >> 1;
95 log++;
96 }
97 return log;
98}
99
100
101/* ifft(xre[], n) - integer (fixpoint) version of Fast Fourier Transform
102 An integer version of FFT that takes in-samples in an int16_t array
103 and does an fft on n samples in the array.
104 The result of the FFT is stored in the same array as the samples
105 was stored. Them imaginary part of the result is stored in xim which
106 needs to be of the same size as xre (e.g. n ints).
107
108 Note: This fft is designed to be used with 8 bit values (e.g. not
109 16 bit values). The reason for the int16_t array is for keeping some
110 'room' for the calculations. It is also designed for doing fairly small
111 FFT:s since to large sample arrays might cause it to overflow during
112 calculations.
113*/
114void
115ifft(int16_t xre[], int16_t xim[], uint16_t n)
116{
117 uint16_t nu;
118 uint16_t n2;
119 uint16_t nu1;
120 int p, k, l, i;
121 int32_t c, s, tr, ti;
122
123 nu = ilog2(n);
124 nu1 = nu - 1;
125 n2 = n / 2;
126
127 for (i = 0; i < n; i++)
128 xim[i] = 0;
129
130 for (l = 1; l <= nu; l++) {
131 for (k = 0; k < n; k += n2) {
132 for (i = 1; i <= n2; i++) {
133 p = bitrev(k >> nu1, nu);
134 c = cosI((1000 * p) / n);
135 s = sinI((1000 * p) / n);
136
137 tr = ((xre[k + n2] * c + xim[k + n2] * s) >> RESOLUTION);
138 ti = ((xim[k + n2] * c - xre[k + n2] * s) >> RESOLUTION);
139
140 xre[k + n2] = xre[k] - tr;
141 xim[k + n2] = xim[k] - ti;
142 xre[k] += tr;
143 xim[k] += ti;
144 k++;
145 }
146 }
147 nu1--;
148 n2 = n2 / 2;
149 }
150
151 for (k = 0; k < n; k++) {
152 p = bitrev(k, nu);
153 if (p > k) {
154 n2 = xre[k];
155 xre[k] = xre[p];
156 xre[p] = n2;
157
158 n2 = xim[k];
159 xim[k] = xim[p];
160 xim[p] = n2;
161 }
162 }
163
164 /* This is a fast but not 100% correct magnitude calculation */
165 /* Should be sqrt(xre[i]^2 + xim[i]^2) and normalized with div. by n */
166 for (i = 0, n2 = n / 2; i < n2; i++) {
167 xre[i] = (ABS(xre[i]) + ABS(xim[i]));
168 }
169}