![](/media/cache/profiles/240ac9371ec2671ae99847c3ae2e6384.50x50_q85.jpg)
FFT for LPC1114FN28 with UTI_FFT_Real_mod
Dependencies: UIT_FFT_Real_mod mbed
Fork of Demo_FFT_IFFT by
Diff: main.cpp
- Revision:
- 2:18b19edf441d
- Parent:
- 1:f070e455cea0
--- a/main.cpp Fri Dec 19 12:34:56 2014 +0000 +++ b/main.cpp Thu Oct 15 15:39:00 2015 +0000 @@ -1,58 +1,153 @@ //-------------------------------------------------------------- -// Demo program of FftReal class -// Copyright (c) 2014 MIKAMI, Naoki, 2014/12/19 +/** + * FFT experiment for LPC1114FN28 + * + * programed by Takehisa Oneta(ohneta) + * Aug. 2015 + */ +//-------------------------------------------------------------- + +#include "fftReal.hpp" +#include "mbed.h" + +#define SAMPLING_NUM 64 +//#define SAMPLING_NUM 32 //-------------------------------------------------------------- -#include "dftComplex.hpp" -#include "fftReal.hpp" +Serial pc(USBTX, USBRX); +AnalogIn ainR(dp9); +AnalogIn ainL(dp10); +Ticker soundInterrupt; -#include "mbed.h" +const char *gBarGraf[] = { + " ", + "* ", + "** ", + "*** ", + "**** ", + "***** ", + "****** ", + "******* ", + "******** ", + "********* ", + "********** ", + "*********** ", + "************ ", + "************* ", + "************** ", + "*************** ", + "**************** ", + "***************** ", + "****************** ", + "******************* ", + "******************** ", + "********************* ", + "********************** ", + "*********************** ", + "************************ ", + "************************* ", + "************************** ", + "*************************** ", + "**************************** ", + "***************************** ", + "****************************** ", + "*******************************", +}; -using namespace Mikami; +//-------------------------------------------------------------- + + +uint32_t gSoundIntrFlag = 0; +uint32_t gSoundIntrCount = 0; + +float gBufferR[SAMPLING_NUM]; +float gBufferL[SAMPLING_NUM]; + +//---------------------------------------------------- +/** + * Ticker handler + */ +void soundInterruptHandle() +{ + if (gSoundIntrFlag == 0) { + gSoundIntrFlag = 1; + } +} + +//---------------------------------------------------- int main() { - const int N = 256;//16; // number of date for FFT + pc.baud(115200); + + Complex y1[SAMPLING_NUM / 2 + 1]; + Complex y2[SAMPLING_NUM / 2 + 1]; - float x1[N], x2[N]; - Complex y1[N], y2[N/2+1]; + pc.printf("### START \n"); - // Generate random data - srand(1234); - for (int n=0; n<N; n++) - x1[n] = 2.0f*rand()/(float)RAND_MAX - 1.0f; - printf("\r\n#### Original data for DFT ####\r\n"); - for (int n=0; n<N; n++) - printf("f[%2d]: %8.4f\r\n", n, x1[n]); + gSoundIntrFlag = 0; + gSoundIntrCount = 0; + uint32_t intr_us = 25; // + soundInterrupt.attach_us(&soundInterruptHandle, intr_us); + +float dd = 20.0f; - // DFT, for comarison - DftComplex(x1, y1, N); - printf("\r\n#### Result of direct DFT ####\r\n"); - printf(" real imaginary\r\n"); - for (int n=0; n<N; n++) - printf("F[%2d]: %8.4f, %8.4f\r\n", n, y1[n].real(), y1[n].imag()); - - Timer tm; // for measurement of execution time + FftReal fft(SAMPLING_NUM); + while (1) { + + if (gSoundIntrFlag == 1) { + float r = ainR; + float l = ainL; + + //pc.printf("*** [%02d] = %8.4f : %8.4f \n", gSoundIntrCount, r, l); - // FFT - FftReal fft(N); - tm.reset(); - tm.start(); - fft.Execute(x1, y2); - tm.stop(); - printf("\r\nExecution time (FFT): %d [us]\r\n", tm.read_us()); - printf("\r\n#### Result of DFT using FFT ####\r\n"); - for (int n=0; n<=N/2; n++) - printf("F[%2d]: %8.4f, %8.4f\r\n", n, y2[n].real(), y2[n].imag()); - - // IFFT - tm.reset(); - tm.start(); - fft.ExecuteIfft(y2, x2); - tm.stop(); - printf("\r\nExecution time (IFFT): %d [us]\r\n", tm.read_us()); - printf("\r\n#### Result of IFFT ####\r\n"); - printf(" original IFFT of FFT\r\n"); - for (int n=0; n<N; n++) - printf("f[%2d]: %8.4f, %8.4f\r\n", n, x1[n], x2[n]); + gBufferR[gSoundIntrCount] = r * dd - 1.0f; + if (gBufferR[gSoundIntrCount] < -1.0f) { + gBufferR[gSoundIntrCount] = -1.0f; + } else if (gBufferR[gSoundIntrCount] > 1.0f) { + gBufferR[gSoundIntrCount] = 1.0f; + } + gBufferL[gSoundIntrCount] = l * dd - 1.0f; + if (gBufferL[gSoundIntrCount] < -1.0f) { + gBufferL[gSoundIntrCount] = -1.0f; + } else if (gBufferL[gSoundIntrCount] > 1.0f) { + gBufferL[gSoundIntrCount] = 1.0f; + } + //pc.printf("*** [%02d] = %2.4f : %2.4f : %2.4f : %2.4f \n", gSoundIntrCount, r, l, gBufferR[gSoundIntrCount], gBufferL[gSoundIntrCount]); + //pc.printf("\n"); + + gSoundIntrCount++; + if (gSoundIntrCount >= SAMPLING_NUM) { + gSoundIntrCount = 0; + + fft.Execute(gBufferR, y1); + fft.Execute(gBufferL, y2); + + //pc.printf("%c[2J%c[;H", 27, 27); + pc.printf("%c[%c;%cH", 27, 0, 0); + //pc.printf("%c[;H", 27); + + + pc.printf(" [Right] [Left]\r\n"); + for (int n = 0; n <= (SAMPLING_NUM / 2); n++) { + uint32_t dtR = (uint32_t)abs(y1[n]); + if (dtR >= (SAMPLING_NUM / 2)) { + dtR = 15; + } + + uint32_t dtL = (uint32_t)abs(y2[n]); + if (dtL >= (SAMPLING_NUM / 2)) { + dtL = 15; + } + + pc.printf("%2d: %s : %s\r\n", n, gBarGraf[dtR], gBarGraf[dtL]); + } + + } + gSoundIntrFlag = 0; + } + } + } + +//----------------------------------------------------