- //get high and low peak
- int peak, low;
- getHiLo(dest, loopCnt, &peak, &low, 75, 75);
-
- int ii;
- int clkCnt;
- int tol = 0;
- int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
- int errCnt=0;
- //test each valid clock from smallest to greatest to see which lines up
- for(clkCnt=0; clkCnt < 6; ++clkCnt){
- if (clk[clkCnt] == 32){
- tol=1;
- }else{
- tol=0;
- }
- bestErr[clkCnt]=1000;
- //try lining up the peaks by moving starting point (try first 256)
- for (ii=0; ii< loopCnt; ++ii){
- if ((dest[ii] >= peak) || (dest[ii] <= low)){
- errCnt=0;
- // now that we have the first one lined up test rest of wave array
- for (i=0; i<((int)(size/clk[clkCnt])-1); ++i){
- if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){
- }else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){
- }else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){
- }else{ //error no peak detected
- errCnt++;
- }
- }
- //if we found no errors this is correct one - return this clock
- if(errCnt==0) return clk[clkCnt];
- //if we found errors see if it is lowest so far and save it as best run
- if(errCnt<bestErr[clkCnt]) bestErr[clkCnt]=errCnt;
- }
- }
- }
- int iii=0;
- int best=0;
- for (iii=0; iii<8;++iii){
- if (bestErr[iii]<bestErr[best]){
- // current best bit to error ratio vs new bit to error ratio
- if (((size/clk[best])/bestErr[best] < (size/clk[iii])/bestErr[iii]) ){
- best = iii;
- }
- }
- }
- return clk[best];
+ //get high and low peak
+ int peak, low;
+ getHiLo(dest, loopCnt, &peak, &low, 75, 75);
+
+ int ii;
+ int clkCnt;
+ int tol = 0;
+ int bestErr[]={1000,1000,1000,1000,1000,1000,1000,1000,1000};
+ int errCnt=0;
+ //test each valid clock from smallest to greatest to see which lines up
+ for(clkCnt=0; clkCnt < 8; ++clkCnt){
+ if (clk[clkCnt] == 32){
+ tol=1;
+ }else{
+ tol=0;
+ }
+ bestErr[clkCnt]=1000;
+ //try lining up the peaks by moving starting point (try first 256)
+ for (ii=0; ii < loopCnt; ++ii){
+ if ((dest[ii] >= peak) || (dest[ii] <= low)){
+ errCnt=0;
+ // now that we have the first one lined up test rest of wave array
+ for (i=0; i<((int)((size-ii-tol)/clk[clkCnt])-1); ++i){
+ if (dest[ii+(i*clk[clkCnt])]>=peak || dest[ii+(i*clk[clkCnt])]<=low){
+ }else if(dest[ii+(i*clk[clkCnt])-tol]>=peak || dest[ii+(i*clk[clkCnt])-tol]<=low){
+ }else if(dest[ii+(i*clk[clkCnt])+tol]>=peak || dest[ii+(i*clk[clkCnt])+tol]<=low){
+ }else{ //error no peak detected
+ errCnt++;
+ }
+ }
+ //if we found no errors then we can stop here
+ // this is correct one - return this clock
+ //PrintAndLog("DEBUG: clk %d, err %d, ii %d, i %d",clk[clkCnt],errCnt,ii,i);
+ if(errCnt==0 && clkCnt<6) return clk[clkCnt];
+ //if we found errors see if it is lowest so far and save it as best run
+ if(errCnt<bestErr[clkCnt]) bestErr[clkCnt]=errCnt;
+ }
+ }
+ }
+ uint8_t iii=0;
+ uint8_t best=0;
+ for (iii=0; iii<8; ++iii){
+ if (bestErr[iii]<bestErr[best]){
+ if (bestErr[iii]==0) bestErr[iii]=1;
+ // current best bit to error ratio vs new bit to error ratio
+ if (((size/clk[best])/bestErr[best] < (size/clk[iii])/bestErr[iii]) ){
+ best = iii;
+ }
+ }
+ }
+ return clk[best];