Guillaume Cathelain / Mbed OS Env_simDAC

Dependencies:   mbed-dsp

Revision:
1:4a666bd3fef6
Parent:
0:05e2c9ca68e2
Child:
2:7945f79d7c8e
--- a/main.cpp	Fri Apr 13 08:59:47 2018 +0000
+++ b/main.cpp	Thu Jan 24 15:26:56 2019 +0000
@@ -1,3 +1,10 @@
+/*
+Original code from MARTIN SIMPSON : CMSIS_FFT_mbed_os_DAC
+https://os.mbed.com/users/martinsimpson/code/CMSIS_FFT_mbed_os_DAC/file/05e2c9ca68e2/main.cpp/
+
+Modified by Guillaume Cathelain to generate simulation from DAC
+*/
+
 #include "mbed.h"
 /* Include arm_math.h mathematic functions */
 #include "arm_math.h"
@@ -7,67 +14,81 @@
 #include "math_helper.h"
 
 /* FFT settings */
-#define SAMPLES                 512             /* 256 real party and 256 imaginary parts */
-#define FFT_SIZE                SAMPLES / 2     /* FFT size is always the same size as we have samples, so 256 in our case */
+#define SAMPLES                 2048             /* 0.5 real parts and 0.5 imaginary parts */
+#define FFT_SIZE                SAMPLES / 2     /* FFT size is always the same size as we have samples, so 1024/256 in our case */
 
 /* Global variables */
-float32_t Input[SAMPLES];
-float32_t Output[FFT_SIZE];
-bool      trig=0;
+float Input[SAMPLES];
+float Output[FFT_SIZE];
 /* MBED class APIs */
-DigitalOut myled(LED1);
+//DigitalOut myled(LED1);
 AnalogIn   myADC(A1);
 AnalogOut  myDAC(D13);
 Serial     pc(USBTX, USBRX);
 Ticker     timer;
+    
+/* Define sine generator settings*/    
+#define SAMPLE_FREQUENCY    (128.0)
+#define SINE_FREQUENCY      (1.0)
+const float SAMPLE_TIME = 1/SAMPLE_FREQUENCY;
+const int BUFFER_SIZE = int(SAMPLE_FREQUENCY/SINE_FREQUENCY);
+float sine[BUFFER_SIZE];
+// Create the sinewave buffer
+void calculate_sinewave(){
+    for (int i = 0; i < BUFFER_SIZE; i+=1) {
+        float t = i * SAMPLE_TIME;
+        float phase = 2* PI * SINE_FREQUENCY * t;
+        sine[i] = float(0.5 * (cos(phase)) + 0.5);
+    }
+}
 
-void sample(){
-    trig=1;
+int n=0;
+void dac_generate(){
+    myDAC.write(sine[n]);
+    n++;
+    if (n>=BUFFER_SIZE){
+        n=0;
     }
+}
+        
 
 int main() {
-
-    //arm_cfft_instance_f32 S;   // ARM CFFT module
+    pc.baud(9600);
+    pc.printf("Starting FFT\r\n");
+    // generate sinewave on myDAC
+    calculate_sinewave();
+    
     float maxValue;            // Max FFT value is stored here
     uint32_t maxIndex;         // Index in Output array where max value is
-    bool once=0;
-    pc.baud(115200);
-    pc.printf("Starting FFT\r\n");
-    while(1) {
-            timer.attach_us(&sample,20); //20us 50KHz sampling rate
-            for (int i = 0; i < SAMPLES; i += 2) {
-                while (trig==0){}
-                trig=0;
-                Input[i] = myADC.read() - 0.5f; //Real part NB removing DC offset
-                Input[i + 1] = 0;               //Imaginary Part set to zero
-                }
-            timer.detach();
-        // Init the Complex FFT module, intFlag = 0, doBitReverse = 1
-        //NB using predefined arm_cfft_sR_f32_lenXXX, in this case XXX is 256
+    timer.attach(&dac_generate,SAMPLE_TIME);
+    for (int i = 0; i < SAMPLES; i += 2) {
+        wait(SAMPLE_TIME);
+        Input[i] = myADC.read() - 0.5f; //Real part NB removing DC offset
+        Input[i + 1] = 0;               //Imaginary Part set to zero
+    }
+    // Init the Complex FFT module, intFlag = 0, doBitReverse = 1
+    //NB using predefined arm_cfft_sR_f32_lenXXX, in this case XXX is 256
+    if (FFT_SIZE==1024){
+        arm_cfft_f32(&arm_cfft_sR_f32_len1024, Input, 0, 1);  
+    } else if (FFT_SIZE==256){
         arm_cfft_f32(&arm_cfft_sR_f32_len256, Input, 0, 1);
+    }    
 
-        // Complex Magniture Module put results into Output(Half size of the Input)
-        arm_cmplx_mag_f32(Input, Output, FFT_SIZE);
-        
-        //Calculates maxValue and returns corresponding value
-        arm_max_f32(Output, FFT_SIZE, &maxValue, &maxIndex);
-
-        if (once==0){
-            pc.printf("Maximum is %f\r\n",maxValue);
-            once = 1;
-            }
+    // Complex Magniture Module put results into Output(Half size of the Input)
+    arm_cmplx_mag_f32(Input, Output, FFT_SIZE);
        
-        //maxValue /= 100.0f;
-        
-        myDAC=1.0f;     //SYNC Pulse to DAC Output
-        wait_us(20);    //Used on Oscilliscope set trigger level to the highest
-        myDAC=0.0f;     //point on this pulse (all FFT data will be scaled
-                        //90% of Max Value
-        
-        for (int i=0; i<FFT_SIZE/2 ; i++){
-            myDAC=(Output[i]/maxValue)*0.9f; // Scale to Max Value and scale to 90%
-            wait_us(10); //Each pulse of 10us is 50KHz/256 = 195Hz resolution
-            }
-        myDAC=0.0f;
-        }
+    //Calculates maxValue and returns corresponding value
+    arm_max_f32(Output, FFT_SIZE, &maxValue, &maxIndex);
+    
+    /* FFT graph, only positive frequency */
+//    for (int i=0; i<FFT_SIZE/2;i++){
+//        pc.printf("%f\r\n",Output[i]);
+//    }
+
+    /* FFT results */
+//    pc.printf("Maximum value is %f\r\n",maxValue);
+//    pc.printf("Index of maximum value is %d\r\n",maxIndex);
+    float freqStep = SAMPLE_FREQUENCY / FFT_SIZE;
+    float maxFreq = maxIndex * freqStep;
+    pc.printf("Frequency of maximum value is %.3f +/- %.3f \r\n",maxFreq,freqStep);
 }
\ No newline at end of file