Guillaume Cathelain / Mbed OS Env_simDAC

Dependencies:   mbed-dsp

main.cpp

Committer:
guicat
Date:
2019-01-24
Revision:
1:4a666bd3fef6
Parent:
0:05e2c9ca68e2
Child:
2:7945f79d7c8e

File content as of revision 1:4a666bd3fef6:

/*
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"
/* Include mbed-dsp libraries */
#include "arm_common_tables.h"
#include "arm_const_structs.h"
#include "math_helper.h"

/* FFT settings */
#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 */
float Input[SAMPLES];
float Output[FFT_SIZE];
/* MBED class APIs */
//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);
    }
}

int n=0;
void dac_generate(){
    myDAC.write(sine[n]);
    n++;
    if (n>=BUFFER_SIZE){
        n=0;
    }
}
        

int main() {
    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
    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);
    
    /* 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);
}