mbed port of FFT routines from STM32 DSP library and Ivan Mellen's implementation. Tested on LPC2368 mbed but should work on 1768 too (original code was written for Cortex-M3)
main.cpp
00001 #include "mbed.h" 00002 #include <limits.h> 00003 00004 DigitalOut myled(LED1); 00005 LocalFileSystem local("local"); 00006 00007 // code from FFTCM3.s by Ivan Mellen 00008 // http://www.luminarymicro.com/component/option,com_joomlaboard/Itemid,92/func,view/id,1636/catid,6/ 00009 extern "C" void fftR4(short *y, short *x, int N); 00010 extern "C" void ifftR4(short *y, short *x, int N); 00011 00012 // code from STM32 DSP Library 00013 /* 64 points*/ 00014 extern "C" void cr4_fft_64_stm32(void *pssOUT, void *pssIN, uint16_t Nbin); 00015 /* 256 points */ 00016 extern "C" void cr4_fft_256_stm32(void *pssOUT, void *pssIN, uint16_t Nbin); 00017 /* 1024 points */ 00018 extern "C" void cr4_fft_1024_stm32(void *pssOUT, void *pssIN, uint16_t Nbin); 00019 00020 void test_stm32() 00021 { 00022 #define N 64 /*Number of points*/ 00023 uint32_t x[N], y[N]; /* input and output arrays */ 00024 int16_t real[N], imag[N]; /* real and imaginary arrays */ 00025 memset(real, 0, sizeof(real)); 00026 memset(imag, 0, sizeof(imag)); 00027 real[1]=SHRT_MAX; 00028 /* Fill the input array */ 00029 for (int i=0; i<N; i++) 00030 { 00031 x[i] = (((uint16_t)(real[i])) | ((uint32_t)(imag[i]<<16))); 00032 } 00033 cr4_fft_64_stm32(y, x, N); /*computes the FFT of the x[N] samples*/ 00034 FILE* log = fopen("/local/stm32.txt","w"); 00035 for (int i=0; i<N; i++) 00036 { 00037 fprintf(log, "%d: %d, %d -> %d, %d\n", i, real[i], imag[i], int16_t(y[i] & 0xFFFF), int16_t(y[i] >> 16)); 00038 } 00039 fclose(log); 00040 } 00041 00042 void test_mellen() 00043 { 00044 short x[512]; // input data 16 bit, 4 byte aligned x0r,x0i,x1r,x1i,.... 00045 short y[512]; // output data 16 bit,4 byte aligned y0r,y0i,y1r,y1i,.... 00046 short z[512]; // same format... 00047 00048 for (int i=0;i<512;i++) x[i]=0; 00049 for (int i=0;i<512;i=i+8) 00050 { x[i+0]=16384; x[i+2]=16384; x[i+4]=-16384; x[i+6]=-16384;} 00051 // x = [ 16384,16384,-16384,-16384,16384,...] 1/4 Fsampling 00052 00053 //call functions 00054 fftR4(y, x, 256); // y is in frequency domain y[128]= 00055 printf("fftR4 ok\n"); 00056 ifftR4(z, y, 256); // z should be x/N + noise introduced by 16 bit truncating 00057 printf("ifftR4 ok\n"); 00058 FILE* log = fopen("/local/mellen.txt","w"); 00059 for (int i=0; i<256; i++) 00060 { 00061 fprintf(log, "%d: %d -> %d -> %d\n", i, x[i], y[i], z[i]); 00062 } 00063 fclose(log); 00064 } 00065 00066 int main() 00067 { 00068 printf("Testing Mellen\n"); 00069 test_mellen(); 00070 printf("Testing STM32\n"); 00071 test_stm32(); 00072 printf("Done\n"); 00073 }
Generated on Tue Jul 12 2022 21:10:14 by 1.7.2