Test program with the RT black boxes

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PRBS.cpp Source File

PRBS.cpp

00001 /*
00002    PRBS: pseudo random binary sequence generator
00003 
00004     instantiate option: PRBS(int n)
00005     
00006     hints: - the output delivers a new excitation signal with the values -1 or 1.
00007            - after 2^n-1 samples the sequenz repeats infinite
00008 
00009         n:  create a sequence of length 2^n-1
00010             n = 2...19 is supported
00011 
00012         autor: M.E. Peter
00013 */
00014 
00015 #include "PRBS.h"
00016 #include "mbed.h"
00017 #include "math.h"
00018 
00019 using namespace std;
00020 
00021 PRBS::PRBS(int n)
00022 {
00023     this->n = n;
00024     Nprbs = (int)pow((float)2, (float)n) - 1;
00025     getPrbsIndex(n);
00026     reset();
00027 }
00028 
00029 PRBS::~PRBS() {}
00030 
00031 void PRBS::reset()
00032 {
00033     ii = 1;
00034     //default register
00035     int initRegister[19] = {1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1};
00036     Register = (int*)malloc(n*sizeof(int));
00037     for(int i = 0; i < n; i++) {
00038         Register[i] = initRegister[i];
00039     }
00040 }
00041 
00042 float PRBS::update()
00043 {
00044     int prbs = Register[n-1];
00045     int x = (Register[ind[0]-1] + Register[ind[1]-1])%2;
00046     for(int i = 1; i < Nind-1; i++) {
00047         x = (x + Register[ind[i+1]-1])%2;
00048     }
00049     for(int i = n-1; i > 0; i--) {
00050         Register[i] = Register[i-1];
00051     }
00052     Register[0] = x;
00053     // printf("%4i : %4i %4i %4i %4i %4i %4i %4i %4i , %4.4f \r\n", ii, Register[0], Register[1], Register[2], Register[3], Register[4], Register[5], Register[6], Register[7], 2.0f*((float)prbs - 0.5f));
00054     if(ii++ == Nprbs) reset();
00055     return 2.0f*((float)prbs - 0.5f);
00056 }
00057 
00058 void PRBS::getPrbsIndex(int n)
00059 {
00060     switch (n) {
00061         case 2:
00062             Nind = 2;
00063             ind = (int*)malloc(Nind*sizeof(int));
00064             ind[0] = 1;
00065             ind[1] = n;
00066             break;
00067         case 3:
00068             Nind = 2;
00069             ind = (int*)malloc(Nind*sizeof(int));
00070             ind[0] = 1;
00071             ind[1] = n;
00072             break;
00073         case 4:
00074             Nind = 2;
00075             ind = (int*)malloc(Nind*sizeof(int));
00076             ind[0] = 1;
00077             ind[1] = n;
00078             break;
00079         case 5:
00080             Nind = 2;
00081             ind = (int*)malloc(Nind*sizeof(int));
00082             ind[0] = 2;
00083             ind[1] = n;
00084             break;
00085         case 6:
00086             Nind = 2;
00087             ind = (int*)malloc(Nind*sizeof(int));
00088             ind[0] = 1;
00089             ind[1] = n;
00090             break;
00091         case 7:
00092             Nind = 2;
00093             ind = (int*)malloc(Nind*sizeof(int));
00094             ind[0] = 1;
00095             ind[1] = n;
00096             break;
00097         case 8:
00098             Nind = 4;
00099             ind = (int*)malloc(Nind*sizeof(int));
00100             ind[0] = 1;
00101             ind[1] = 2;
00102             ind[2] = 7;
00103             ind[3] = n;
00104             break;
00105         case 9:
00106             Nind = 2;
00107             ind = (int*)malloc(Nind*sizeof(int));
00108             ind[0] = 4;
00109             ind[1] = n;
00110             break;
00111         case 10:
00112             Nind = 2;
00113             ind = (int*)malloc(Nind*sizeof(int));
00114             ind[0] = 3;
00115             ind[1] = n;
00116             break;
00117         case 11:
00118             Nind = 2;
00119             ind = (int*)malloc(Nind*sizeof(int));
00120             ind[0] = 2;
00121             ind[1] = n;
00122             break;
00123         case 12:
00124             Nind = 4;
00125             ind = (int*)malloc(Nind*sizeof(int));
00126             ind[0] = 4;
00127             ind[1] = 10;
00128             ind[2] = 11;
00129             ind[3] = n;
00130             break;
00131         case 13:
00132             Nind = 4;
00133             ind = (int*)malloc(Nind*sizeof(int));
00134             ind[0] = 8;
00135             ind[1] = 11;
00136             ind[2] = 12;
00137             ind[3] = n;
00138             break;
00139         case 14:
00140             Nind = 4;
00141             ind = (int*)malloc(Nind*sizeof(int));
00142             ind[0] = 2;
00143             ind[1] = 12;
00144             ind[2] = 13;
00145             ind[3] = n;
00146             break;
00147         case 15:
00148             Nind = 2;
00149             ind = (int*)malloc(Nind*sizeof(int));
00150             ind[0] = 14;
00151             ind[1] = n;
00152             break;
00153         case 16:
00154             Nind = 4;
00155             ind = (int*)malloc(Nind*sizeof(int));
00156             ind[0] = 11;
00157             ind[1] = 13;
00158             ind[2] = 14;
00159             ind[3] = n;
00160             break;
00161         case 17:
00162             Nind = 2;
00163             ind = (int*)malloc(Nind*sizeof(int));
00164             ind[0] = 14;
00165             ind[1] = n;
00166             break;
00167         case 18:
00168             Nind = 2;
00169             ind = (int*)malloc(Nind*sizeof(int));
00170             ind[0] = 11;
00171             ind[1] = n;
00172             break;
00173         case 19:
00174             Nind = 4;
00175             ind = (int*)malloc(Nind*sizeof(int));
00176             ind[0] = 14;
00177             ind[1] = 17;
00178             ind[2] = 18;
00179             ind[3] = n;
00180             break;
00181         default:
00182             Nind = 2;
00183             ind = (int*)malloc(Nind*sizeof(int));
00184             ind[0] = 0;
00185             ind[1] = 0;
00186             break;
00187     }
00188 }
00189 
00190 void PRBS::printLine()
00191 {
00192     printf("-----------------------------------------------------------------------------------------\r\n");
00193 }
00194 
00195 void PRBS::printPRBSind()
00196 {
00197     printLine();
00198     for(int i = 0; i < Nind; i++) {
00199         printf("%6i \r\n", ind[i]);
00200     }
00201 }
00202 
00203 void PRBS::printPRBSregister()
00204 {
00205     printLine();
00206     for(int i = 0; i < n; i++) {
00207         printf("%6i \r\n", Register[i]);
00208     }
00209 }