modified API for fft

This commit is contained in:
joxe 2008-04-25 22:12:25 +00:00
parent 8b30087ce4
commit 7722a73abf
3 changed files with 23 additions and 16 deletions

View file

@ -32,8 +32,8 @@
* *
* Author : Joakim Eriksson * Author : Joakim Eriksson
* Created : 2008-03-27 * Created : 2008-03-27
* Updated : $Date: 2008/03/27 12:12:24 $ * Updated : $Date: 2008/04/25 22:12:25 $
* $Revision: 1.1 $ * $Revision: 1.2 $
*/ */
#include "lib/ifft.h" #include "lib/ifft.h"
@ -59,7 +59,8 @@ static const int8_t SIN_TAB[] = {
}; };
static uint16_t bitrev(uint16_t j, uint16_t nu) { static uint16_t bitrev(uint16_t j, uint16_t nu)
{
uint16_t k; uint16_t k;
k = 0; k = 0;
for (; nu > 0; nu--) { for (; nu > 0; nu--) {
@ -71,17 +72,20 @@ static uint16_t bitrev(uint16_t j, uint16_t nu) {
/* Non interpolating sine... which takes an angle of 0 - 999 */ /* Non interpolating sine... which takes an angle of 0 - 999 */
static int16_t sinI(uint16_t angleMilli) { static int16_t sinI(uint16_t angleMilli)
{
uint16_t pos; uint16_t pos;
pos = (uint16_t) ((SIN_TAB_LEN * (uint32_t) angleMilli) / 1000); pos = (uint16_t) ((SIN_TAB_LEN * (uint32_t) angleMilli) / 1000);
return SIN_TAB[pos % SIN_TAB_LEN]; return SIN_TAB[pos % SIN_TAB_LEN];
} }
static int16_t cosI(uint16_t angleMilli) { static int16_t cosI(uint16_t angleMilli)
{
return sinI(angleMilli + 250); return sinI(angleMilli + 250);
} }
static uint16_t log2(uint16_t val) { static uint16_t log2(uint16_t val)
{
uint16_t log; uint16_t log;
log = 0; log = 0;
val = val >> 1; /* The 20 = 1 => log = 0 => val = 0 */ val = val >> 1; /* The 20 = 1 => log = 0 => val = 0 */
@ -97,7 +101,8 @@ static uint16_t log2(uint16_t val) {
An integer version of FFT that takes in-samples in an int16_t array An integer version of FFT that takes in-samples in an int16_t array
and does an fft on n samples in the array. and does an fft on n samples in the array.
The result of the FFT is stored in the same array as the samples The result of the FFT is stored in the same array as the samples
was stored. was stored. Them imaginary part of the result is stored in xim which
needs to be of the same size as xre (e.g. n ints).
Note: This fft is designed to be used with 8 bit values (e.g. not Note: This fft is designed to be used with 8 bit values (e.g. not
16 bit values). The reason for the int16_t array is for keeping some 16 bit values). The reason for the int16_t array is for keeping some
@ -105,11 +110,12 @@ static uint16_t log2(uint16_t val) {
FFT:s since to large sample arrays might cause it to overflow during FFT:s since to large sample arrays might cause it to overflow during
calculations. calculations.
*/ */
void ifft(int16_t xre[], uint16_t n) { void
ifft(int16_t xre[], int16_t xim[], uint16_t n)
{
uint16_t nu; uint16_t nu;
uint16_t n2; uint16_t n2;
uint16_t nu1; uint16_t nu1;
int16_t xim[n];
int p, k, l, i; int p, k, l, i;
int32_t c, s, tr, ti; int32_t c, s, tr, ti;

View file

@ -33,8 +33,8 @@
* *
* Author : Joakim Eriksson * Author : Joakim Eriksson
* Created : 2008-03-27 * Created : 2008-03-27
* Updated : $Date: 2008/03/27 12:12:24 $ * Updated : $Date: 2008/04/25 22:12:25 $
* $Revision: 1.1 $ * $Revision: 1.2 $
*/ */
#ifndef IFFT_H #ifndef IFFT_H
@ -53,6 +53,6 @@
FFT:s since to large sample arrays might cause it to overflow during FFT:s since to large sample arrays might cause it to overflow during
calculations. calculations.
*/ */
void ifft(int16_t xre[], uint16_t n); void ifft(int16_t xre[], int16_t xim[], uint16_t n);
#endif /* IFFT_H */ #endif /* IFFT_H */

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: fft-test.c,v 1.1 2008/04/25 16:34:31 joxe Exp $ * $Id: fft-test.c,v 1.2 2008/04/25 22:12:25 joxe Exp $
* *
* ----------------------------------------------------------------- * -----------------------------------------------------------------
* fft-test - a test program that use the sound sensor for sampling * fft-test - a test program that use the sound sensor for sampling
@ -35,8 +35,8 @@
* *
* Author : Joakim Eriksson * Author : Joakim Eriksson
* Created : 2008-04-04 * Created : 2008-04-04
* Updated : $Date: 2008/04/25 16:34:31 $ * Updated : $Date: 2008/04/25 22:12:25 $
* $Revision: 1.1 $ * $Revision: 1.2 $
*/ */
#include "contiki-esb.h" #include "contiki-esb.h"
@ -57,6 +57,7 @@ PROCESS_THREAD(fft_process, ev, data)
static int i = 0; static int i = 0;
static int mode = 0; static int mode = 0;
static int16_t fftBuf[FFT_TEST_SIZE]; static int16_t fftBuf[FFT_TEST_SIZE];
static int16_t tmpBuf[FFT_TEST_SIZE];
static int max_value = 0; static int max_value = 0;
static int max_pos = 0; static int max_pos = 0;
@ -92,7 +93,7 @@ PROCESS_THREAD(fft_process, ev, data)
if (mode) { if (mode) {
/* Calculate fft, print result, and find index of /* Calculate fft, print result, and find index of
highest energy level */ highest energy level */
ifft(fftBuf, FFT_TEST_SIZE); ifft(fftBuf, tmpBuf, FFT_TEST_SIZE);
max_value = 0; max_value = 0;
max_pos = 0; max_pos = 0;
for (i = 0; i < FFT_TEST_SIZE/2; i++) { for (i = 0; i < FFT_TEST_SIZE/2; i++) {