FFT_BUENA
Dependencies: FastAnalogIn HSI2RGBW_PWM NVIC_set_all_priorities mbed-dsp mbed
Fork of KL25Z_FFT_Demo by
Diff: main.cpp
- Revision:
- 4:a6130c61c228
- Parent:
- 3:f826669fc0a8
- Child:
- 5:6caecff3094d
--- a/main.cpp Tue Nov 25 03:49:15 2014 +0000 +++ b/main.cpp Wed Nov 26 22:51:15 2014 +0000 @@ -1,7 +1,3 @@ -// Audio Spectrum Display -// Copyright 2013 Tony DiCola (tony@tonydicola.com) -// Code ported from the guide at http://learn.adafruit.com/fft-fun-with-fourier-transforms?view=all - #include "mbed.h" #include "NVIC_set_all_priorities.h" #include <ctype.h> @@ -12,9 +8,19 @@ Serial pc(USBTX, USBRX); -//FastAnalogIn Audio(PTC2); +float pulso=83; +float pulsocalculado=0; -//#define RGBW_ext // Disable this line when you want to use the KL25Z on-board RGB LED. +float qrs2[512]= {0}; +float fs=102.4; +float ts=1/fs; +int tiempo=5; +float pi=3.1416; +float l=30/pulso; +float a=1.6; +float b=(2*l)/0.11; +int n=100; +float qrs1=(a/(2*b))*(2-b); #ifndef RGBW_ext // HSI to RGB conversion with direct output to PWM channels - on-board RGB LED @@ -26,7 +32,8 @@ // Dummy ISR for disabling NMI on PTA4 - !! DO NOT REMOVE THIS !! // More info at https://mbed.org/questions/1387/How-can-I-access-the-FTFA_FOPT-register-/ -extern "C" void NMI_Handler() { +extern "C" void NMI_Handler() +{ DigitalIn test(PTA4); } @@ -43,17 +50,17 @@ //////////////////////////////////////////////////////////////////////////////// int SLOWDOWN = 4; // Create an optical delay in spectrumLoop - useful when only one RGB led is used. - // Only active when nonzero. - // A value >= 1000 and <= 1000 + PIXEL_COUNT fixes the output to a single frequency - // window = a single color. +// Only active when nonzero. +// A value >= 1000 and <= 1000 + PIXEL_COUNT fixes the output to a single frequency +// window = a single color. int SAMPLE_RATE_HZ = 40000; // Sample rate of the audio in hertz. float SPECTRUM_MIN_DB = 30.0; // Audio intensity (in decibels) that maps to low LED brightness. float SPECTRUM_MAX_DB = 80.0; // Audio intensity (in decibels) that maps to high LED brightness. int LEDS_ENABLED = 1; // Control if the LED's should display the spectrum or not. 1 is true, 0 is false. - // Useful for turning the LED display on and off with commands from the serial port. -const int FFT_SIZE = 4096; // Size of the FFT. +// Useful for turning the LED display on and off with commands from the serial port. +const int FFT_SIZE = 512; // Size of the FFT. const int PIXEL_COUNT = 32; // Number of pixels. You should be able to increase this without - // any other changes to the program. +// any other changes to the program. const int MAX_CHARS = 65; // Max size of the input command buffer //////////////////////////////////////////////////////////////////////////////// @@ -73,7 +80,8 @@ // UTILITY FUNCTIONS //////////////////////////////////////////////////////////////////////////////// -void rxisr() { +void rxisr() +{ char c = pc.getc(); // Add any characters that aren't the end of a command (semicolon) to the input buffer. if (c != ';') { @@ -136,32 +144,23 @@ static int SLrpt = 0, SLpixcnt = 0; int SLpixend = 0; float intensity, otherMean; - if(SLOWDOWN != 0) - { - if(SLOWDOWN >= 1000) - { - if(SLOWDOWN <= (1000 + PIXEL_COUNT-1)) - { + if(SLOWDOWN != 0) { + if(SLOWDOWN >= 1000) { + if(SLOWDOWN <= (1000 + PIXEL_COUNT-1)) { SLpixcnt = SLOWDOWN - 1000; SLrpt = 0; SLpixend = SLpixcnt + 1; - } - else + } else SLOWDOWN = 0; - } - else - { + } else { SLrpt++; - if (SLrpt >= SLOWDOWN) - { + if (SLrpt >= SLOWDOWN) { SLrpt = 0; SLpixcnt = SLpixcnt < PIXEL_COUNT-1 ? ++SLpixcnt : 0; } SLpixend = SLpixcnt + 1; } - } - else - { + } else { SLpixcnt = 0; SLrpt = 0; SLpixend = PIXEL_COUNT; @@ -191,7 +190,7 @@ void samplingCallback() { // Read from the ADC and store the sample data - samples[sampleCounter] = (sampleCounter+1)/2; + samples[sampleCounter] = qrs1+qrs2[(sampleCounter+1)/2]; // Complex FFT functions require a coefficient for the imaginary part of the input. // Since we only have real data, set this coefficient to zero. samples[sampleCounter+1] = 0.0; @@ -303,9 +302,29 @@ // Set up serial port. pc.baud (9600); pc.attach(&rxisr); + + float harm[512]; + int inmax=0,max=0; + + + for(int i=1; i<=n; i=i+1) { + for(int j=0; j<512; j=j+1) { + harm[j]=(((2*b*a)/(i*i*pi*pi))*(1-cos((i*pi)/b)))*cos((i*pi*(j+1)*ts)/l); + } + for(int k=0; k<512; k=k+1) { + qrs2[k]=qrs2[k]+harm[k]; + } + } + + + + + + + #ifndef RGBW_ext led.invertpwm(1); //On-board KL25Z RGB LED uses common anode. -#endif +#endif // Clear the input command buffer memset(commandBuffer, 0, sizeof(commandBuffer)); @@ -316,61 +335,62 @@ samplingBegin(); // Init arm_ccft_32 - switch (FFT_SIZE) - { - case 16: - S = & arm_cfft_sR_f32_len16; - break; - case 32: - S = & arm_cfft_sR_f32_len32; - break; - case 64: - S = & arm_cfft_sR_f32_len64; - break; - case 128: - S = & arm_cfft_sR_f32_len128; - break; - case 256: - S = & arm_cfft_sR_f32_len256; - break; - case 512: - S = & arm_cfft_sR_f32_len512; - break; - case 1024: - S = & arm_cfft_sR_f32_len1024; - break; - case 2048: - S = & arm_cfft_sR_f32_len2048; - break; - case 4096: - S = & arm_cfft_sR_f32_len4096; - break; + switch (FFT_SIZE) { + case 16: + S = & arm_cfft_sR_f32_len16; + break; + case 32: + S = & arm_cfft_sR_f32_len32; + break; + case 64: + S = & arm_cfft_sR_f32_len64; + break; + case 128: + S = & arm_cfft_sR_f32_len128; + break; + case 256: + S = & arm_cfft_sR_f32_len256; + break; + case 512: + S = & arm_cfft_sR_f32_len512; + break; + case 1024: + S = & arm_cfft_sR_f32_len1024; + break; + case 2048: + S = & arm_cfft_sR_f32_len2048; + break; + case 4096: + S = & arm_cfft_sR_f32_len4096; + break; } while(1) { // Calculate FFT if a full sample is available. if (samplingIsDone()) { - - pc.printf("%f %f %f %f %f %f %f %f\t%f %f %f %f %f %f %f %f\n", - samples[0], samples[2], samples[4], samples[6], - samples[8], samples[10], samples[12], samples[14], - samples[16], samples[18], samples[20], samples[22], - samples[24], samples[26], samples[28], samples[30]); - - + + // Run FFT on sample data. // Run FFT on sample data. arm_cfft_f32(S, samples, 0, 1); // Calculate magnitude of complex numbers output by the FFT. arm_cmplx_mag_f32(samples, magnitudes, FFT_SIZE); + + + /*for(int i=0; i<512; i=i+1) { + pc.printf("%d\t%f\n",i,magnitudes[i]); + }*/ + for(int i=1; i<FFT_SIZE/2; i=i+1) { + if(magnitudes[i]>max) { + max=magnitudes[i]; + inmax=i; + } + } + + pulsocalculado=fs/(FFT_SIZE-1)*inmax*60; + pc.printf("%f\n",pulsocalculado); - pc.printf("%f %f %f %f %f %f %f %f\t%f %f %f %f %f %f %f %f\n\n\n", - magnitudes[0], magnitudes[1], magnitudes[2], magnitudes[3], - magnitudes[4], magnitudes[5], magnitudes[6], magnitudes[7], - magnitudes[8], magnitudes[9], magnitudes[10], magnitudes[11], - magnitudes[12], magnitudes[13], magnitudes[14], magnitudes[15]); - - + inmax=0; if (LEDS_ENABLED == 1) { spectrumLoop();