Test program with the RT black boxes

Dependencies:   mbed

Committer:
pmic
Date:
Wed May 15 13:58:10 2019 +0000
Revision:
43:a577f752dd7a
Parent:
23:1d24b1033371
Add new instantiation option to surpress the printout for rt mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 21:b3c07ac9b9ed 1 /*
pmic 23:1d24b1033371 2 PRBS: pseudo random binary sequence generator
pmic 21:b3c07ac9b9ed 3
pmic 23:1d24b1033371 4 instantiate option: PRBS(int n)
pmic 23:1d24b1033371 5
pmic 23:1d24b1033371 6 hints: - the output delivers a new excitation signal with the values -1 or 1.
pmic 23:1d24b1033371 7 - after 2^n-1 samples the sequenz repeats infinite
pmic 21:b3c07ac9b9ed 8
pmic 23:1d24b1033371 9 n: create a sequence of length 2^n-1
pmic 23:1d24b1033371 10 n = 2...19 is supported
pmic 21:b3c07ac9b9ed 11
pmic 21:b3c07ac9b9ed 12 autor: M.E. Peter
pmic 21:b3c07ac9b9ed 13 */
pmic 21:b3c07ac9b9ed 14
pmic 21:b3c07ac9b9ed 15 #include "PRBS.h"
pmic 21:b3c07ac9b9ed 16 #include "mbed.h"
pmic 21:b3c07ac9b9ed 17 #include "math.h"
pmic 21:b3c07ac9b9ed 18
pmic 21:b3c07ac9b9ed 19 using namespace std;
pmic 21:b3c07ac9b9ed 20
pmic 21:b3c07ac9b9ed 21 PRBS::PRBS(int n)
pmic 21:b3c07ac9b9ed 22 {
pmic 21:b3c07ac9b9ed 23 this->n = n;
pmic 23:1d24b1033371 24 Nprbs = (int)pow((float)2, (float)n) - 1;
pmic 21:b3c07ac9b9ed 25 getPrbsIndex(n);
pmic 21:b3c07ac9b9ed 26 reset();
pmic 21:b3c07ac9b9ed 27 }
pmic 21:b3c07ac9b9ed 28
pmic 21:b3c07ac9b9ed 29 PRBS::~PRBS() {}
pmic 21:b3c07ac9b9ed 30
pmic 21:b3c07ac9b9ed 31 void PRBS::reset()
pmic 21:b3c07ac9b9ed 32 {
pmic 23:1d24b1033371 33 ii = 1;
pmic 23:1d24b1033371 34 //default register
pmic 21:b3c07ac9b9ed 35 int initRegister[19] = {1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1};
pmic 23:1d24b1033371 36 Register = (int*)malloc(n*sizeof(int));
pmic 21:b3c07ac9b9ed 37 for(int i = 0; i < n; i++) {
pmic 21:b3c07ac9b9ed 38 Register[i] = initRegister[i];
pmic 21:b3c07ac9b9ed 39 }
pmic 21:b3c07ac9b9ed 40 }
pmic 21:b3c07ac9b9ed 41
pmic 21:b3c07ac9b9ed 42 float PRBS::update()
pmic 21:b3c07ac9b9ed 43 {
pmic 21:b3c07ac9b9ed 44 int prbs = Register[n-1];
pmic 23:1d24b1033371 45 int x = (Register[ind[0]-1] + Register[ind[1]-1])%2;
pmic 21:b3c07ac9b9ed 46 for(int i = 1; i < Nind-1; i++) {
pmic 23:1d24b1033371 47 x = (x + Register[ind[i+1]-1])%2;
pmic 21:b3c07ac9b9ed 48 }
pmic 21:b3c07ac9b9ed 49 for(int i = n-1; i > 0; i--) {
pmic 21:b3c07ac9b9ed 50 Register[i] = Register[i-1];
pmic 21:b3c07ac9b9ed 51 }
pmic 21:b3c07ac9b9ed 52 Register[0] = x;
pmic 23:1d24b1033371 53 // 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));
pmic 23:1d24b1033371 54 if(ii++ == Nprbs) reset();
pmic 21:b3c07ac9b9ed 55 return 2.0f*((float)prbs - 0.5f);
pmic 21:b3c07ac9b9ed 56 }
pmic 21:b3c07ac9b9ed 57
pmic 21:b3c07ac9b9ed 58 void PRBS::getPrbsIndex(int n)
pmic 21:b3c07ac9b9ed 59 {
pmic 21:b3c07ac9b9ed 60 switch (n) {
pmic 21:b3c07ac9b9ed 61 case 2:
pmic 21:b3c07ac9b9ed 62 Nind = 2;
pmic 21:b3c07ac9b9ed 63 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 64 ind[0] = 1;
pmic 21:b3c07ac9b9ed 65 ind[1] = n;
pmic 21:b3c07ac9b9ed 66 break;
pmic 21:b3c07ac9b9ed 67 case 3:
pmic 21:b3c07ac9b9ed 68 Nind = 2;
pmic 21:b3c07ac9b9ed 69 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 70 ind[0] = 1;
pmic 21:b3c07ac9b9ed 71 ind[1] = n;
pmic 21:b3c07ac9b9ed 72 break;
pmic 21:b3c07ac9b9ed 73 case 4:
pmic 21:b3c07ac9b9ed 74 Nind = 2;
pmic 21:b3c07ac9b9ed 75 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 76 ind[0] = 1;
pmic 21:b3c07ac9b9ed 77 ind[1] = n;
pmic 21:b3c07ac9b9ed 78 break;
pmic 21:b3c07ac9b9ed 79 case 5:
pmic 21:b3c07ac9b9ed 80 Nind = 2;
pmic 21:b3c07ac9b9ed 81 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 82 ind[0] = 2;
pmic 21:b3c07ac9b9ed 83 ind[1] = n;
pmic 21:b3c07ac9b9ed 84 break;
pmic 21:b3c07ac9b9ed 85 case 6:
pmic 21:b3c07ac9b9ed 86 Nind = 2;
pmic 21:b3c07ac9b9ed 87 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 88 ind[0] = 1;
pmic 21:b3c07ac9b9ed 89 ind[1] = n;
pmic 21:b3c07ac9b9ed 90 break;
pmic 21:b3c07ac9b9ed 91 case 7:
pmic 21:b3c07ac9b9ed 92 Nind = 2;
pmic 21:b3c07ac9b9ed 93 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 94 ind[0] = 1;
pmic 21:b3c07ac9b9ed 95 ind[1] = n;
pmic 21:b3c07ac9b9ed 96 break;
pmic 21:b3c07ac9b9ed 97 case 8:
pmic 21:b3c07ac9b9ed 98 Nind = 4;
pmic 21:b3c07ac9b9ed 99 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 100 ind[0] = 1;
pmic 21:b3c07ac9b9ed 101 ind[1] = 2;
pmic 21:b3c07ac9b9ed 102 ind[2] = 7;
pmic 21:b3c07ac9b9ed 103 ind[3] = n;
pmic 21:b3c07ac9b9ed 104 break;
pmic 21:b3c07ac9b9ed 105 case 9:
pmic 21:b3c07ac9b9ed 106 Nind = 2;
pmic 21:b3c07ac9b9ed 107 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 108 ind[0] = 4;
pmic 21:b3c07ac9b9ed 109 ind[1] = n;
pmic 21:b3c07ac9b9ed 110 break;
pmic 21:b3c07ac9b9ed 111 case 10:
pmic 21:b3c07ac9b9ed 112 Nind = 2;
pmic 21:b3c07ac9b9ed 113 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 114 ind[0] = 3;
pmic 21:b3c07ac9b9ed 115 ind[1] = n;
pmic 21:b3c07ac9b9ed 116 break;
pmic 21:b3c07ac9b9ed 117 case 11:
pmic 21:b3c07ac9b9ed 118 Nind = 2;
pmic 21:b3c07ac9b9ed 119 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 120 ind[0] = 2;
pmic 21:b3c07ac9b9ed 121 ind[1] = n;
pmic 21:b3c07ac9b9ed 122 break;
pmic 21:b3c07ac9b9ed 123 case 12:
pmic 21:b3c07ac9b9ed 124 Nind = 4;
pmic 21:b3c07ac9b9ed 125 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 126 ind[0] = 4;
pmic 21:b3c07ac9b9ed 127 ind[1] = 10;
pmic 21:b3c07ac9b9ed 128 ind[2] = 11;
pmic 21:b3c07ac9b9ed 129 ind[3] = n;
pmic 21:b3c07ac9b9ed 130 break;
pmic 21:b3c07ac9b9ed 131 case 13:
pmic 21:b3c07ac9b9ed 132 Nind = 4;
pmic 21:b3c07ac9b9ed 133 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 134 ind[0] = 8;
pmic 21:b3c07ac9b9ed 135 ind[1] = 11;
pmic 21:b3c07ac9b9ed 136 ind[2] = 12;
pmic 21:b3c07ac9b9ed 137 ind[3] = n;
pmic 21:b3c07ac9b9ed 138 break;
pmic 21:b3c07ac9b9ed 139 case 14:
pmic 21:b3c07ac9b9ed 140 Nind = 4;
pmic 21:b3c07ac9b9ed 141 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 142 ind[0] = 2;
pmic 21:b3c07ac9b9ed 143 ind[1] = 12;
pmic 21:b3c07ac9b9ed 144 ind[2] = 13;
pmic 21:b3c07ac9b9ed 145 ind[3] = n;
pmic 21:b3c07ac9b9ed 146 break;
pmic 21:b3c07ac9b9ed 147 case 15:
pmic 21:b3c07ac9b9ed 148 Nind = 2;
pmic 21:b3c07ac9b9ed 149 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 150 ind[0] = 14;
pmic 21:b3c07ac9b9ed 151 ind[1] = n;
pmic 21:b3c07ac9b9ed 152 break;
pmic 21:b3c07ac9b9ed 153 case 16:
pmic 21:b3c07ac9b9ed 154 Nind = 4;
pmic 21:b3c07ac9b9ed 155 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 156 ind[0] = 11;
pmic 21:b3c07ac9b9ed 157 ind[1] = 13;
pmic 21:b3c07ac9b9ed 158 ind[2] = 14;
pmic 21:b3c07ac9b9ed 159 ind[3] = n;
pmic 21:b3c07ac9b9ed 160 break;
pmic 21:b3c07ac9b9ed 161 case 17:
pmic 21:b3c07ac9b9ed 162 Nind = 2;
pmic 21:b3c07ac9b9ed 163 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 164 ind[0] = 14;
pmic 21:b3c07ac9b9ed 165 ind[1] = n;
pmic 21:b3c07ac9b9ed 166 break;
pmic 21:b3c07ac9b9ed 167 case 18:
pmic 21:b3c07ac9b9ed 168 Nind = 2;
pmic 21:b3c07ac9b9ed 169 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 170 ind[0] = 11;
pmic 21:b3c07ac9b9ed 171 ind[1] = n;
pmic 21:b3c07ac9b9ed 172 break;
pmic 21:b3c07ac9b9ed 173 case 19:
pmic 21:b3c07ac9b9ed 174 Nind = 4;
pmic 21:b3c07ac9b9ed 175 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 176 ind[0] = 14;
pmic 21:b3c07ac9b9ed 177 ind[1] = 17;
pmic 21:b3c07ac9b9ed 178 ind[2] = 18;
pmic 21:b3c07ac9b9ed 179 ind[3] = n;
pmic 21:b3c07ac9b9ed 180 break;
pmic 21:b3c07ac9b9ed 181 default:
pmic 21:b3c07ac9b9ed 182 Nind = 2;
pmic 21:b3c07ac9b9ed 183 ind = (int*)malloc(Nind*sizeof(int));
pmic 21:b3c07ac9b9ed 184 ind[0] = 0;
pmic 21:b3c07ac9b9ed 185 ind[1] = 0;
pmic 21:b3c07ac9b9ed 186 break;
pmic 21:b3c07ac9b9ed 187 }
pmic 23:1d24b1033371 188 }
pmic 21:b3c07ac9b9ed 189
pmic 23:1d24b1033371 190 void PRBS::printLine()
pmic 23:1d24b1033371 191 {
pmic 23:1d24b1033371 192 printf("-----------------------------------------------------------------------------------------\r\n");
pmic 21:b3c07ac9b9ed 193 }
pmic 21:b3c07ac9b9ed 194
pmic 23:1d24b1033371 195 void PRBS::printPRBSind()
pmic 23:1d24b1033371 196 {
pmic 23:1d24b1033371 197 printLine();
pmic 23:1d24b1033371 198 for(int i = 0; i < Nind; i++) {
pmic 23:1d24b1033371 199 printf("%6i \r\n", ind[i]);
pmic 23:1d24b1033371 200 }
pmic 23:1d24b1033371 201 }
pmic 23:1d24b1033371 202
pmic 23:1d24b1033371 203 void PRBS::printPRBSregister()
pmic 23:1d24b1033371 204 {
pmic 23:1d24b1033371 205 printLine();
pmic 23:1d24b1033371 206 for(int i = 0; i < n; i++) {
pmic 23:1d24b1033371 207 printf("%6i \r\n", Register[i]);
pmic 23:1d24b1033371 208 }
pmic 23:1d24b1033371 209 }