- for (;i < size; i++) {
- if ((dest[i] >= peak && !lastWasHigh) || (dest[i] <= low && lastWasHigh)) {
- lastWasHigh = (dest[i] >= peak);
- if (i-transition1 < lowestTransition) lowestTransition = i-transition1;
- transition1 = i;
- }
- }
- if (lowestTransition == 255) lowestTransition = 0;
- if (g_debugMode==2) prnt("DEBUG NRZ: detectstrongNRZclk smallest wave: %d",lowestTransition);
- return lowestTransition;
-}
-
-//by marshmellow
-//detect nrz clock by reading #peaks vs no peaks(or errors)
-int DetectNRZClock_ext(uint8_t dest[], size_t size, int clock, size_t *clockStartIdx) {
- size_t i=0;
- uint8_t clk[]={8,16,32,40,50,64,100,128,255};
- size_t loopCnt = 4096; //don't need to loop through entire array...
- if (size == 0) return 0;
- if (size<loopCnt) loopCnt = size-20;
- //if we already have a valid clock quit
- for (; i < 8; ++i)
- if (clk[i] == clock) return clock;
-
- //get high and low peak
- int peak, low;
- if (getHiLo(dest, loopCnt, &peak, &low, 75, 75) < 1) return 0;
-
- int lowestTransition = DetectStrongNRZClk(dest, size-20, peak, low);
- size_t ii;
- uint8_t clkCnt;
- uint8_t tol = 0;
- uint16_t smplCnt = 0;
- int16_t peakcnt = 0;
- int16_t peaksdet[] = {0,0,0,0,0,0,0,0};
- uint16_t maxPeak = 255;
- bool firstpeak = false;
- //test for large clipped waves
- for (i=0; i<loopCnt; i++){
- if (dest[i] >= peak || dest[i] <= low){
- if (!firstpeak) continue;
- smplCnt++;
- } else {
- firstpeak=true;
- if (smplCnt > 6 ){
- if (maxPeak > smplCnt){
- maxPeak = smplCnt;
- //prnt("maxPk: %d",maxPeak);
- }
- peakcnt++;
- //prnt("maxPk: %d, smplCnt: %d, peakcnt: %d",maxPeak,smplCnt,peakcnt);
- smplCnt=0;
- }
- }
- }
- bool errBitHigh = 0;
- bool bitHigh = 0;
- uint8_t ignoreCnt = 0;
- uint8_t ignoreWindow = 4;
- bool lastPeakHigh = 0;
- int lastBit = 0;
- size_t bestStart[]={0,0,0,0,0,0,0,0,0};
- peakcnt=0;
- //test each valid clock from smallest to greatest to see which lines up
- for(clkCnt=0; clkCnt < 8; ++clkCnt){
- //ignore clocks smaller than smallest peak
- if (clk[clkCnt] < maxPeak - (clk[clkCnt]/4)) continue;
- //try lining up the peaks by moving starting point (try first 256)
- for (ii=20; ii < loopCnt; ++ii){
- if ((dest[ii] >= peak) || (dest[ii] <= low)){
- peakcnt = 0;
- bitHigh = false;
- ignoreCnt = 0;
- lastBit = ii-clk[clkCnt];
- //loop through to see if this start location works
- for (i = ii; i < size-20; ++i) {
- //if we are at a clock bit
- if ((i >= lastBit + clk[clkCnt] - tol) && (i <= lastBit + clk[clkCnt] + tol)) {
- //test high/low
- if (dest[i] >= peak || dest[i] <= low) {
- //if same peak don't count it
- if ((dest[i] >= peak && !lastPeakHigh) || (dest[i] <= low && lastPeakHigh)) {
- peakcnt++;
- }
- lastPeakHigh = (dest[i] >= peak);
- bitHigh = true;
- errBitHigh = false;
- ignoreCnt = ignoreWindow;
- lastBit += clk[clkCnt];
- } else if (i == lastBit + clk[clkCnt] + tol) {
- lastBit += clk[clkCnt];
- }
- //else if not a clock bit and no peaks
- } else if (dest[i] < peak && dest[i] > low){
- if (ignoreCnt==0){
- bitHigh=false;
- if (errBitHigh==true) peakcnt--;
- errBitHigh=false;
- } else {
- ignoreCnt--;
- }
- // else if not a clock bit but we have a peak
- } else if ((dest[i]>=peak || dest[i]<=low) && (!bitHigh)) {
- //error bar found no clock...
- errBitHigh=true;
- }
- }
- if(peakcnt>peaksdet[clkCnt]) {
- bestStart[clkCnt]=ii;
- peaksdet[clkCnt]=peakcnt;
- }
- }
- }
- }
- int iii=7;
- uint8_t best=0;
- for (iii=7; iii > 0; iii--){
- if ((peaksdet[iii] >= (peaksdet[best]-1)) && (peaksdet[iii] <= peaksdet[best]+1) && lowestTransition) {
- if (clk[iii] > (lowestTransition - (clk[iii]/8)) && clk[iii] < (lowestTransition + (clk[iii]/8))) {
- best = iii;
- }
- } else if (peaksdet[iii] > peaksdet[best]){
- best = iii;
- }
- if (g_debugMode==2) prnt("DEBUG NRZ: Clk: %d, peaks: %d, maxPeak: %d, bestClk: %d, lowestTrs: %d",clk[iii],peaksdet[iii],maxPeak, clk[best], lowestTransition);
- }
- *clockStartIdx = bestStart[best];
- return clk[best];
-}
-
-int DetectNRZClock(uint8_t dest[], size_t size, int clock) {
- size_t bestStart=0;
- return DetectNRZClock_ext(dest, size, clock, &bestStart);
-}
-
-//by marshmellow
-//countFC is to detect the field clock lengths.
-//counts and returns the 2 most common wave lengths
-//mainly used for FSK field clock detection
-uint16_t countFC(uint8_t *BitStream, size_t size, uint8_t fskAdj) {
- uint8_t fcLens[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- uint16_t fcCnts[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- uint8_t fcLensFnd = 0;
- uint8_t lastFCcnt = 0;
- uint8_t fcCounter = 0;
- size_t i;
- if (size < 180) return 0;
-
- // prime i to first up transition
- for (i = 160; i < size-20; i++)
- if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1])
- break;
-
- for (; i < size-20; i++){
- if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1]){
- // new up transition
- fcCounter++;
- if (fskAdj){
- //if we had 5 and now have 9 then go back to 8 (for when we get a fc 9 instead of an 8)
- if (lastFCcnt==5 && fcCounter==9) fcCounter--;
- //if fc=9 or 4 add one (for when we get a fc 9 instead of 10 or a 4 instead of a 5)
- if ((fcCounter==9) || fcCounter==4) fcCounter++;
- // save last field clock count (fc/xx)
- lastFCcnt = fcCounter;
- }
- // find which fcLens to save it to:
- for (int ii=0; ii<15; ii++){
- if (fcLens[ii]==fcCounter){
- fcCnts[ii]++;
- fcCounter=0;
- break;
- }
- }
- if (fcCounter>0 && fcLensFnd<15){
- //add new fc length
- fcCnts[fcLensFnd]++;
- fcLens[fcLensFnd++]=fcCounter;
- }
- fcCounter=0;
- } else {
- // count sample
- fcCounter++;
- }
- }