Ura

Dependencies:   CMSIS_DSP_401 mbed

Revision:
1:9b1df0b2507d
Parent:
0:b712470aea1d
Child:
2:707eb5bc5dfc
--- a/main.cpp	Tue Sep 30 16:13:41 2014 +0000
+++ b/main.cpp	Fri Oct 03 14:35:00 2014 +0000
@@ -1,81 +1,122 @@
 #include "mbed.h"
+#include <ctype.h>
 #include "arm_math.h"
-#include "math_helper.h"
-#include <stdio.h>
-
-
-#define BLOCK_SIZE 32
-#define NUM_BLOCKS 10
-#define TEST_LENGTH_SAMPLES (BLOCK_SIZE * NUM_BLOCKS)
-#define TEST_LENGTH_SAMPLES 312
-#define SNR_THRESHOLD_F32 140.0f
-#define NUM_TAPS 29
-
-Serial pc(SERIAL_TX, SERIAL_RX);
-
-/* -------------------------------------------------------------------
-* The input signal and reference output (computed with MATLAB)
-* are defined externally in arm_fir_lpf_data.c.
-* ------------------------------------------------------------------- */
-
-extern float32_t testInput_f32_1kHz_15kHz[TEST_LENGTH_SAMPLES];
-extern float32_t refOutput[TEST_LENGTH_SAMPLES];
-
-/* -------------------------------------------------------------------
-* Declare State buffer of size (numTaps + blockSize - 1)
-* ------------------------------------------------------------------- */
-
-static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1];
-
-/* ----------------------------------------------------------------------
-* FIR Coefficients buffer generated using fir1() MATLAB function.
-* fir1(28, 6/24)
-* ------------------------------------------------------------------- */
+#include "arm_const_structs.h"
+ 
+void arm_cfft_f32( 
+   const arm_cfft_instance_f32 * S, 
+   float32_t * p1,
+   uint8_t ifftFlag,
+   uint8_t bitReverseFlag);
+   
 
-const float32_t firCoeffs32[NUM_TAPS] = {
--0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f,
-+0.0085302217f, -0.0000000000f, -0.0173976984f, -0.0341458607f, -0.0333591565f,
-+0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f,
-+0.2229246956f, +0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f,
--0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f, +0.0080754303f,
-+0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f
-};
-/* ----------------------------------------------------------------------
-* FIR LPF Example
-* ------------------------------------------------------------------- */
-int main(void) {
-/* Call FIR init function to initialize the instance structure. */
-arm_fir_instance_f32 S;
-arm_fir_init_f32(&S, NUM_TAPS, (float32_t *)&firCoeffs32[0], &firStateF32[0], BLOCK_SIZE);
-/* ----------------------------------------------------------------------
-* Call the FIR process function for every blockSize samples
-* ------------------------------------------------------------------- */
-for (uint32_t i=0; i < NUM_BLOCKS; i++) {
-float32_t* signal = testInput_f32_1kHz_15kHz + (i * BLOCK_SIZE);
-arm_fir_f32(&S, signal, signal, BLOCK_SIZE);
+ 
+Serial pc(USBTX, USBRX);
+ 
+AnalogIn   left(A2);
+AnalogIn   right(A3);
+ 
+int SAMPLE_RATE_HZ = 40000;             // Sample rate of the audio in hertz.
+const int FFT_SIZE = 16;                // Size of the FFT.
+ 
+const static arm_cfft_instance_f32 *S;
+Ticker samplingTimer;
+float samples[FFT_SIZE*2];
+float magnitudes[FFT_SIZE];
+int sampleCounter = 0;
+ 
+void samplingCallback()
+{
+    /*
+    // Read from the ADC and store the sample data
+    samples[sampleCounter] = (1023 * left) - 511.0;
+    // 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;
+    // Update sample buffer position and stop after the buffer is filled
+    sampleCounter += 2;
+    */
+    if (sampleCounter >= FFT_SIZE*2) {
+        samplingTimer.detach();
+    }
+}
+ 
+void samplingBegin()
+{
+    // Reset sample buffer position and start callback at necessary rate.
+    sampleCounter = 0;
+    samplingTimer.attach_us(&samplingCallback, 1000000/SAMPLE_RATE_HZ);
+}
+ 
+bool samplingIsDone()
+{
+    return sampleCounter >= FFT_SIZE*2;
 }
-/* ----------------------------------------------------------------------
-* Compare the generated output against the reference output computed
-* in MATLAB.
-* ------------------------------------------------------------------- */
-//float32_t snr = arm_snr_f32((float*)refOutput, (float*)testInput_f32_1kHz_15kHz, (unsigned)TEST_LENGTH_SAMPLES);
-
-float32_t snr;
-unsigned int b;
-arm_max_f32( testInput_f32_1kHz_15kHz,TEST_LENGTH_SAMPLES, &snr, &b); 
-
-  
-pc.printf("snr: %f\n\r", snr);
-while(1)
+ 
+int main()
 {
-    if (snr < SNR_THRESHOLD_F32) {
-        pc.printf("Failed\n\r");
-        pc.printf("snr: %f\n\r", snr);
-        } else {
-            pc.printf("Success\n\r");
-                    pc.printf("snr: %f\n\r", snr);
-            }
-}
-
-
+    // Set up serial port.
+    pc.baud (38400);
+ 
+    // 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;
+    }
+    float maxValue = 0.0f;
+    unsigned int testIndex = 0;
+ 
+    // Begin sampling audio
+    samplingBegin();
+ 
+    while(1)
+    {
+        // Calculate FFT if a full sample is available.
+        if (samplingIsDone())
+        {
+            // Run FFT on sample data.
+            //arm_cfft_radix2_f32(arm_cfft_radix2_instance_f32*S, samples);
+            arm_cfft_f32(S, samples, 0, 1);
+            for(int i = 0;i < FFT_SIZE*2;++i)
+                printf("% 8.2f ",samples[i]);
+            printf("\r\n");
+            // Calculate magnitude of complex numbers output by the FFT.
+            arm_cmplx_mag_f32(samples, magnitudes, FFT_SIZE);
+            for(int i = 0;i < FFT_SIZE;++i)
+                printf("% 8.2f ",magnitudes[i]);
+            printf("\r\n");
+            arm_max_f32(magnitudes, FFT_SIZE, &maxValue, &testIndex);
+            printf("MAX value at magnitudes[%d] : %+ 8.2f\r\n", testIndex, maxValue);
+            // Wait for user confirmation to restart audio sampling.
+            //pc.getc();
+            wait(1);
+            samplingBegin();
+        }
+    }
 }
\ No newline at end of file