Simplified version of FFT code - drives on-board LED as a "Colour Organ".

Dependencies:   FastAnalogIn NVIC_set_all_priorities mbed-dsp mbed

Fork of KL25Z_FFT_Demo_tony by Tony Abbey

Revision:
2:aa24865dfef5
Parent:
1:7c7539fba82b
--- a/main.cpp	Thu Jul 10 06:54:57 2014 +0000
+++ b/main.cpp	Fri Jul 11 17:03:53 2014 +0000
@@ -14,7 +14,7 @@
 
 FastAnalogIn   Audio(PTC2);
 
-//#define RGBW_ext // Disable this line when you want to use the KL25Z on-board RGB LED.
+// #define RGBW_ext // Disable this line when you want to use the KL25Z on-board RGB LED.
 
 
 #ifndef RGBW_ext
@@ -23,8 +23,10 @@
     PwmOut rled(LED_RED);
     PwmOut bled(LED_BLUE);
 #else
-// HSI to RGBW conversion with direct output to external PWM channels - RGBW LED
-// hsi2rgbw_pwm led(PTD4, PTA12, PTA4, PTA5); //Red, Green, Blue, White
+    PwmOut gled(D6);
+    PwmOut rled(D5);
+    PwmOut bled(D7);
+    
 #endif
 
 // Dummy ISR for disabling NMI on PTA4 - !! DO NOT REMOVE THIS !!
@@ -45,13 +47,9 @@
 //   Max allowed FFT_SIZE is 64
 ////////////////////////////////////////////////////////////////////////////////
 
-int SLOWDOWN = 0;                       // 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.
-int SAMPLE_RATE_HZ = 20000;             // Sample rate of the audio in hertz.
-float SPECTRUM_MIN_DB = 20.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 SAMPLE_RATE_HZ = 8000;             // Sample rate of the audio in hertz - note was 20000
+float SPECTRUM_MIN_DB = 33.0;           // Audio intensity (in decibels) that maps to low LED brightness.
+float SPECTRUM_MAX_DB = 60.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 = 64;                // Size of the FFT.
@@ -72,6 +70,8 @@
 char commandBuffer[MAX_CHARS];
 float frequencyWindow[PIXEL_COUNT+1];
 float hues[PIXEL_COUNT];
+float oldhues[PIXEL_COUNT];
+float huescount = 1;
 bool commandRecv = 0;
 ////////////////////////////////////////////////////////////////////////////////
 // UTILITY FUNCTIONS
@@ -134,39 +134,9 @@
 {
     // Update each LED based on the intensity of the audio
     // in the associated frequency window.
-    static int SLrpt = 0, SLpixcnt = 0;
-    int SLpixend = 0;
-    float intensity, otherMean;
-    if(SLOWDOWN != 0)
-    {
-        if(SLOWDOWN >= 1000)
-        {
-            if(SLOWDOWN <= (1000 + PIXEL_COUNT-1))
-            {
-                SLpixcnt = SLOWDOWN - 1000;
-                SLrpt = 0;
-                SLpixend = SLpixcnt + 1;
-            }
-            else
-                SLOWDOWN = 0;
-        }
-        else
-        {
-            SLrpt++;
-            if (SLrpt >= SLOWDOWN)
-            {
-                SLrpt = 0;
-                SLpixcnt = SLpixcnt < PIXEL_COUNT-1 ? ++SLpixcnt : 0;
-            }
-            SLpixend = SLpixcnt + 1;
-        }
-    }
-    else
-    {
-        SLpixcnt = 0;
-        SLrpt = 0;
-        SLpixend = PIXEL_COUNT;
-    }
+    static int SLpixcnt = 0;
+    float intensity, otherMean;  
+    int SLpixend = PIXEL_COUNT;
     for (int i = SLpixcnt; i < SLpixend; ++i) {
         windowMean(magnitudes,
                    frequencyToBin(frequencyWindow[i]),
@@ -179,8 +149,11 @@
         intensity -= SPECTRUM_MIN_DB;
         intensity = intensity < 0.0 ? 0.0 : intensity;
         intensity /= (SPECTRUM_MAX_DB-SPECTRUM_MIN_DB);
-        intensity = intensity > 1.0 ? 1.0 : intensity;
-        hues[i]=intensity;
+        //intensity = intensity > 1.0 ? 1.0 : intensity;
+        hues[i]=(intensity+oldhues[i]) / huescount; // averaging function
+        oldhues[i]=hues[i];
+        huescount += 1;
+        if (huescount > 9) huescount = 1;
     }
     rled=1.0-hues[0] ;  // onboard LED is common anode so inversion needed
     gled=1.0-hues[1];
@@ -195,7 +168,8 @@
 void samplingCallback()
 {
     // Read from the ADC and store the sample data
-    samples[sampleCounter] = (1023 * Audio) - 511.0f;
+    samples[sampleCounter] = (1024 * Audio) - 511.0f;
+    // samples[sampleCounter] =  Audio ; // just to see what this call actually produces
     // 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;
@@ -237,12 +211,12 @@
 void parseCommand(char* command)
 { 
     if (strcmp(command, "GET MAGNITUDES") == 0) {
-        for (int i = 0; i < FFT_SIZE; ++i) {
-            printf("%f\r\n", magnitudes[i]);
+        for (int i = 1; i < FFT_SIZE; ++i) {
+            printf("%4.2f,", magnitudes[i]);
         }
     } else if (strcmp(command, "GET SAMPLES") == 0) {
         for (int i = 0; i < FFT_SIZE*2; i+=2) {
-            printf("%f\r\n", samples[i]);
+            printf("%f,", samples[i]);
         }
     } else if (strcmp(command, "GET FFT_SIZE") == 0) {
         printf("%d\r\n", FFT_SIZE);
@@ -262,10 +236,6 @@
         printf("%f\r\n", SPECTRUM_MAX_DB);
     } else if (strstr(command, "SET SPECTRUM_MAX_DB") != NULL) {
         SPECTRUM_MAX_DB = (typeof(SPECTRUM_MAX_DB)) atof(command+(sizeof("SET SPECTRUM_MAX_DB")-1));
-    } else if (strcmp(command, "GET SLOWDOWN") == 0) {
-        printf("%d\r\n", SLOWDOWN);
-    } else if (strstr(command, "SET SLOWDOWN") != NULL) {
-        SLOWDOWN = (typeof(SLOWDOWN)) atoi(command+(sizeof("SET SLOWDOWN")-1));
     } else if (strcmp(command, "GET HUES") == 0) {
         for (int i = 0; i < PIXEL_COUNT; ++i) {
             printf("%f\r\n", hues[i]);