for Danillo

Dependencies:   MBed_Adafruit-GPS-Library SDFileSystem mbed

realtimeQAM.cpp

Committer:
ncfronk
Date:
2014-11-11
Revision:
0:37d4e78b2076
Child:
1:f5770d9636b4

File content as of revision 0:37d4e78b2076:

#include "mbed.h"
#include "math.h"

#define SAMPLE_LENGTH       512 
#define SAMPLE_RATE         16666
#define TIME_CONST          0.00006
#define NUM_TAPS            513
#define SIN_LENGTH          500
#define CARRIER_FREQ        225
#define OUTAVG_LENGTH       1000

Ticker tick1;
Ticker tick2;
AnalogIn AnIn(A0);
AnalogOut dac0(DAC0_OUT);
DigitalOut led_red(LED_RED);
Serial pc(USBTX, USBRX);

float pi = 3.14159;
int sinRes = 74; // resolution of sinWave 74 for 225 hz
int freq = 1;

float sinWave[SIN_LENGTH] = {};
int sinIndex = 0;

float samples[SAMPLE_LENGTH] = {};
int sampleIndex = 0;
float sI[SAMPLE_LENGTH] = {};
float filteredSI = 0;
float sQ[SAMPLE_LENGTH] = {};
float filteredSQ = 0;
float output[SAMPLE_LENGTH] = {};
float avgs[OUTAVG_LENGTH] = {};

bool newValue = false;
bool isSampling = true;
int avgIndex = 0;


void print_sin(float *bar){
    int i =0;
    for(i = 0; i < SAMPLE_LENGTH; i++){
        pc.printf("%f, ", bar[i]);
    }
    pc.printf("\n\n\n\n");
}

void tick_out(){
    if(isSampling){
        //read
        samples[sampleIndex] = AnIn.read();
        sampleIndex++;
        if(sampleIndex > SAMPLE_LENGTH){
            sampleIndex = 0;
        }
        //sampleIndex = (sampleIndex+1)&(SAMPLE_LENGTH-1);
        //write
        dac0 = sinWave[sinIndex];
        sinIndex++;
        if((sinIndex+1) > sinRes){
            sinIndex = 0;
        }
        
        newValue = true;
    }

}

void create_sinWave(){
    int i = 0;
    for(i = 0; i < sinRes; i++){
        sinWave[i] = 0.25 * sin(2.0*pi*i/sinRes) + 0.75;
    }
}

void set_Values(int inFreq){
    freq = inFreq;
    create_sinWave();
}


//---------------------------------------------------------DSP----------------------------------------------------------------------------

float firCoeffs[NUM_TAPS] = { -0.0192878684408550, -0.000565436854162197, -0.000574062372234321, -0.000581179079003090, -0.000589412242712984, -0.000596329290925752,
-0.000604183313496965, -0.000610787449621752, -0.000618254890021300, -0.000624441235452662, -0.000631532852699149, -0.000637317640388987, -0.000644060044878972, -0.000649408145295391, 
-0.000655730318423711, -0.000660536584899437, -0.000666350155348239, -0.000670543670441799, -0.000675811138190482, -0.000679345019980268, -0.000684011485494165, -0.000686779420740873,
-0.000690735948708760, -0.000692604062270113, -0.000695783521327534, -0.000696695831913496, -0.000699146857491809, -0.000699128468616956, -0.000700978318797988, -0.000700085972408138,
-0.000701562144359961, -0.000699960260396518, -0.000701552559161197, -0.000699587125715329, -0.000702084347004260, -0.000699951609455988, -0.000704184938277025, -0.000700822488823546, 
-0.000707053158406025, -0.000689238102101884, -0.000675432749281130, -0.000692284271988876, -0.000679003259712778, -0.000679427972422059, -0.000670867964963519, -0.000668435478754011, 
-0.000660330946718933, -0.000656187894249034, -0.000647892827216824, -0.000642486260779654, -0.000633714444974831, -0.000627154674611770, -0.000617702529114801, -0.000610043338194475, 
-0.000599831070682668, -0.000591137773968283, -0.000580109977377959, -0.000570367122683910, -0.000558402454183533, -0.000547524082087984, -0.000534508605134922, -0.000522458425410067, 
-0.000508372396429405, -0.000495193607822494, -0.000480069347179249, -0.000465821362319299, -0.000449698934449484, -0.000434475552201623, -0.000417460223522498, -0.000401443504121743, 
-0.000383669251697455, -0.000366972910244881, -0.000348312520886679, -0.000330677260377738, -0.000310488418329100, -0.000291306404219105, -0.000268590084163264, -0.000247735016213375, 
-0.000223063752779904, -0.000208020109997019, -0.000182718834161682, -0.000158038013340163, -0.000135672987348502, -0.000110658422989979, -8.63859218366082e-05, -6.07302747179699e-05, 
-3.52380267488923e-05, -8.75000381957480e-06, 1.79220776252956e-05, 4.53634810869086e-05, 7.31488512081353e-05, 0.000101522460573350, 0.000130337162460535, 0.000159652043314615, 
0.000189518312913980, 0.000219864413943603, 0.000250843940298056, 0.000282270121591107, 0.000314346397337869, 0.000346811034408436, 0.000379918194843229, 0.000413368163653855, 
0.000447462473702899, 0.000481854744689647, 0.000516877376372632, 0.000552121769556217, 0.000587977722826699, 0.000623999763683269, 0.000660700340823140, 0.000697608811340494, 
0.000735367166639595, 0.000773394513145455, 0.000812374375990954, 0.000851487293223158, 0.000891382011561606, 0.000931001117497454, 0.000970883568971010, 0.00101069578059601, 
0.00105169774350639, 0.00109588295343421, 0.00113549685886290, 0.00117866383169451, 0.00122111516589616, 0.00126425191781228, 0.00130756685560331, 0.00135135745612168, 
0.00139547947313276, 0.00143990979242900, 0.00148466890906943, 0.00152967223986802, 0.00157503442488970, 0.00162066455214233, 0.00166668113637550, 0.00171297190814002, 
0.00175961611250325, 0.00180649714239756, 0.00185367729128575, 0.00190106604757396, 0.00194872602458623, 0.00199658259647564, 0.00204468151104454, 0.00209294223259948, 
0.00214139247713316, 0.00218996963721177, 0.00223871049041842, 0.00228761026923322, 0.00233669674897580, 0.00238601381783270, 0.00243548525436868, 0.00248518758501967, 
0.00253485686222383, 0.00258470295716680, 0.00263426676950411, 0.00268414140692153, 0.00273361748949242, 0.00278404942218042, 0.00283412527109201, 0.00288512840396260, 
0.00293445800039565, 0.00298408012351935, 0.00303527354743087, 0.00308448377238342, 0.00313512165505945, 0.00318474186231553, 0.00323506931441536, 0.00328460350198885, 
0.00333460199814310, 0.00338400592135627, 0.00343378958762444, 0.00348308365845938, 0.00353265733443406, 0.00358174124596574, 0.00363100325859950, 0.00367976183349975, 
0.00372865317317631, 0.00377704959506895, 0.00382555699048792, 0.00387355440986243, 0.00392161316870967, 0.00396912079239840, 0.00401664308061543, 0.00406360815542227, 
0.00411058555257416, 0.00415703554738502, 0.00420348722135172, 0.00424939516950231, 0.00429522177839473, 0.00434042896145653, 0.00438546971668745, 0.00442986742204268, 
0.00447414198154179, 0.00451784282884279, 0.00456156022215696, 0.00460464825220905, 0.00464779963068343, 0.00468955284268770, 0.00473169248365623, 0.00477223280278857, 
0.00481424852813673, 0.00485403860423785, 0.00489420623498588, 0.00493373299658250, 0.00497262699146714, 0.00501099496582253, 0.00504887367893452, 0.00508624698184808, 
0.00512316386346786, 0.00515946810910305, 0.00519528393985568, 0.00523039315379049, 0.00526503059762880, 0.00529893784256351, 0.00533241509486141, 0.00536513893964146, 
0.00539742537221889, 0.00542889931428335, 0.00545989970145767, 0.00549004893072241, 0.00551972419584414, 0.00554855435612888, 0.00557692734453731, 0.00560444087154133, 
0.00563147234239205, 0.00565757686128727, 0.00568316642545793, 0.00570777861250159, 0.00573192298998399, 0.00575509504545748, 0.00577790201469266, 0.00579969134709878, 
0.00582115054736086, 0.00584139221452850, 0.00586128734004678, 0.00587972989282672, 0.00589808690052833, 0.00591540333198250, 0.00593205379805961, 0.00594813880209655, 
0.00596267377455868, 0.00597741589424509, 0.00599039453248084, 0.00600314980390327, 0.00601494155132523, 0.00602610113531495, 0.00603636342822522, 0.00604578641469852, 
0.00605437894886202, 0.00606211163669993, 0.00606910653387789, 0.00607525672904290, 0.00608068579068155, 0.00608523210616189, 0.00608901297897988, 0.00609187619324112, 
0.00609396152547327, 0.00609514946463964, 0.00609558653157114, 0.00609514946463964, 0.00609396152547327, 0.00609187619324112, 0.00608901297897988, 0.00608523210616189, 
0.00608068579068155, 0.00607525672904290, 0.00606910653387789, 0.00606211163669993, 0.00605437894886202, 0.00604578641469852, 0.00603636342822522, 0.00602610113531495, 
0.00601494155132523, 0.00600314980390327, 0.00599039453248084, 0.00597741589424509, 0.00596267377455868, 0.00594813880209655, 0.00593205379805961, 0.00591540333198250, 
0.00589808690052833, 0.00587972989282672, 0.00586128734004678, 0.00584139221452850, 0.00582115054736086, 0.00579969134709878, 0.00577790201469266, 0.00575509504545748, 
0.00573192298998399, 0.00570777861250159, 0.00568316642545793, 0.00565757686128727, 0.00563147234239205, 0.00560444087154133, 0.00557692734453731, 0.00554855435612888, 
0.00551972419584414, 0.00549004893072241, 0.00545989970145767, 0.00542889931428335, 0.00539742537221889, 0.00536513893964146, 0.00533241509486141, 0.00529893784256351, 
0.00526503059762880, 0.00523039315379049, 0.00519528393985568, 0.00515946810910305, 0.00512316386346786, 0.00508624698184808, 0.00504887367893452, 0.00501099496582253, 
0.00497262699146714, 0.00493373299658250, 0.00489420623498588, 0.00485403860423785, 0.00481424852813673, 0.00477223280278857, 0.00473169248365623, 0.00468955284268770, 
0.00464779963068343, 0.00460464825220905, 0.00456156022215696, 0.00451784282884279, 0.00447414198154179, 0.00442986742204268, 0.00438546971668745, 0.00434042896145653, 
0.00429522177839473, 0.00424939516950231, 0.00420348722135172, 0.00415703554738502, 0.00411058555257416, 0.00406360815542227, 0.00401664308061543, 0.00396912079239840, 
0.00392161316870967, 0.00387355440986243, 0.00382555699048792, 0.00377704959506895, 0.00372865317317631, 0.00367976183349975, 0.00363100325859950, 0.00358174124596574, 
0.00353265733443406, 0.00348308365845938, 0.00343378958762444, 0.00338400592135627, 0.00333460199814310, 0.00328460350198885, 0.00323506931441536, 0.00318474186231553, 
0.00313512165505945, 0.00308448377238342, 0.00303527354743087, 0.00298408012351935, 0.00293445800039565, 0.00288512840396260, 0.00283412527109201, 0.00278404942218042, 
0.00273361748949242, 0.00268414140692153, 0.00263426676950411, 0.00258470295716680, 0.00253485686222383, 0.00248518758501967, 0.00243548525436868, 0.00238601381783270, 
0.00233669674897580, 0.00228761026923322, 0.00223871049041842, 0.00218996963721177, 0.00214139247713316, 0.00209294223259948, 0.00204468151104454, 0.00199658259647564, 
0.00194872602458623, 0.00190106604757396, 0.00185367729128575, 0.00180649714239756, 0.00175961611250325, 0.00171297190814002, 0.00166668113637550, 0.00162066455214233, 
0.00157503442488970, 0.00152967223986802, 0.00148466890906943, 0.00143990979242900, 0.00139547947313276, 0.00135135745612168, 0.00130756685560331, 0.00126425191781228, 
0.00122111516589616, 0.00117866383169451, 0.00113549685886290, 0.00109588295343421, 0.00105169774350639, 0.00101069578059601, 0.000970883568971010, 0.000931001117497454, 
0.000891382011561606, 0.000851487293223158, 0.000812374375990954, 0.000773394513145455, 0.000735367166639595, 0.000697608811340494, 0.000660700340823140, 0.000623999763683269, 
0.000587977722826699, 0.000552121769556217, 0.000516877376372632, 0.000481854744689647, 0.000447462473702899, 0.000413368163653855, 0.000379918194843229, 0.000346811034408436, 
0.000314346397337869, 0.000282270121591107, 0.000250843940298056, 0.000219864413943603, 0.000189518312913980, 0.000159652043314615, 0.000130337162460535, 0.000101522460573350, 
7.31488512081353e-05, 4.53634810869086e-05, 1.79220776252956e-05, -8.75000381957480e-06, -3.52380267488923e-05, -6.07302747179699e-05, -8.63859218366082e-05, -0.000110658422989979, 
-0.000135672987348502, -0.000158038013340163, -0.000182718834161682, -0.000208020109997019, -0.000223063752779904, -0.000247735016213375, -0.000268590084163264, -0.000291306404219105, 
-0.000310488418329100, -0.000330677260377738, -0.000348312520886679, -0.000366972910244881, -0.000383669251697455, -0.000401443504121743, -0.000417460223522498, -0.000434475552201623, -0.000449698934449484, -0.000465821362319299, -0.000480069347179249, -0.000495193607822494, -0.000508372396429405, -0.000522458425410067, -0.000534508605134922, -0.000547524082087984, -0.000558402454183533, -0.000570367122683910, -0.000580109977377959, -0.000591137773968283, -0.000599831070682668, -0.000610043338194475, -0.000617702529114801, -0.000627154674611770, -0.000633714444974831, -0.000642486260779654, -0.000647892827216824, -0.000656187894249034, -0.000660330946718933, -0.000668435478754011, -0.000670867964963519, -0.000679427972422059, -0.000679003259712778, -0.000692284271988876, -0.000675432749281130, -0.000689238102101884, -0.000707053158406025, -0.000700822488823546, -0.000704184938277025, -0.000699951609455988, -0.000702084347004260, -0.000699587125715329, -0.000701552559161197, -0.000699960260396518, -0.000701562144359961, -0.000700085972408138, -0.000700978318797988, -0.000699128468616956, -0.000699146857491809, -0.000696695831913496, -0.000695783521327534, -0.000692604062270113, -0.000690735948708760, -0.000686779420740873, -0.000684011485494165, -0.000679345019980268, -0.000675811138190482, -0.000670543670441799, -0.000666350155348239, -0.000660536584899437, -0.000655730318423711, -0.000649408145295391, -0.000644060044878972, -0.000637317640388987, -0.000631532852699149, -0.000624441235452662, -0.000618254890021300, -0.000610787449621752, -0.000604183313496965, -0.000596329290925752, -0.000589412242712984, -0.000581179079003090, -0.000574062372234321, -0.000565436854162197, -0.0192878684408550
};

float filter( float *in)
{
    float *taps = firCoeffs;
    unsigned int i, j, k;
    float out[SAMPLE_LENGTH] = {};
    k = 0;
    
    
    for( i = 0; i < SAMPLE_LENGTH - NUM_TAPS + 1; i++ )
    {
        out[ i ] = 0.0;
        for( j = 0; j < NUM_TAPS; j++ ){
            k = (i + sampleIndex - j)&(SAMPLE_LENGTH-1);
            out[ i ] += in[ k ] * taps[ j ];
        }
    }
    return out[0];
}

//=------------------------------------------ DSP END ------------------------------------------------------------------------------

//-------------------------------------uiu----- QAM ------------------------------------------------------------------------------------

float qam_in(){
    int j = 0;
    for(int i = 0; i < SAMPLE_LENGTH; i++){
        j = (i+sampleIndex)&(SAMPLE_LENGTH-1);
        sI[i] = samples[i]*cos(2*pi*CARRIER_FREQ*i*TIME_CONST);
        sQ[i] = -samples[i]*sin(2*pi*CARRIER_FREQ*i*TIME_CONST);
    }
    filteredSI = filter(sI);
    filteredSQ = filter(sQ);
    return sqrt(filteredSI*filteredSI + filteredSQ*filteredSQ);
}

//------------------------------------------- QAM END ------------------------------------------------------------------------------

void print_values(){
    printf("\n\r--------------------------------------SAMPLES-----------------------------------\n\r \r\n");
    print_sin(avgs);
    printf("\n\r-------------------------------------------SI-------------------------------------\n\r \r\n");
    print_sin(sI);
    printf("\n\r-----------------------------------------SQ--------------------------------------\n\r \r\n");
    print_sin(sQ);
}

int main(){
    pc.baud(115200);
    
    tick1.attach(&tick_out, 0.00006); // below 0.00005 the board can no longer output and read
    //tick2.attach(&print_values, 20);
    
    set_Values(225);
    pc.printf("Set values\n");
    
    while(1){
        if(newValue){
           avgs[avgIndex] = qam_in();
           avgIndex++;
           if(avgIndex > OUTAVG_LENGTH){
               pc.printf("you");
               avgIndex = 0;
            }
            newValue = false;
        }
    }
}