]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/lfdemod.c
Merge branch 'master' of https://github.com/Proxmark/proxmark3
[proxmark3-svn] / common / lfdemod.c
index a14f2613fd11817bac9a7998357b23e13444205f..7ab8a38815771ef2f7598243788a31fc01707b7e 100644 (file)
@@ -1079,10 +1079,9 @@ void psk1TOpsk2(uint8_t *BitStream, size_t size)
        size_t i=1;
        uint8_t lastBit=BitStream[0];
        for (; i<size; i++){
-               if ( BitStream[i] == 77 ){
-                       
-               } 
-               else if (lastBit!=BitStream[i]){
+               if (BitStream[i]==77){
+                       //ignore errors
+               } else if (lastBit!=BitStream[i]){
                        lastBit=BitStream[i];
                        BitStream[i]=1;
                } else {
@@ -1592,7 +1591,7 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
   if (*size<loopCnt) loopCnt = *size;
 
   uint8_t curPhase = *invert;
-  size_t i, waveStart=0, waveEnd=0, firstFullWave=0, lastClkBit=0;
+  size_t i, waveStart=1, waveEnd=0, firstFullWave=0, lastClkBit=0;
   uint8_t fc=0, fullWaveLen=0, tol=1;
   uint16_t errCnt=0, waveLenCnt=0;
   fc = countPSK_FC(dest, *size);
@@ -1604,37 +1603,32 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
   //find first phase shift
   for (i=0; i<loopCnt; i++){
     if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
-      if (waveStart == 0) {
-        waveStart = i+1;
-        avgWaveVal=dest[i+1];
-        //PrintAndLog("DEBUG: waveStart: %d",waveStart);
-      } else {
         waveEnd = i+1;
         //PrintAndLog("DEBUG: waveEnd: %d",waveEnd);
         waveLenCnt = waveEnd-waveStart;
-        lastAvgWaveVal = avgWaveVal/waveLenCnt;
-        if (waveLenCnt > fc){
+      if (waveLenCnt > fc && waveStart > fc){ //not first peak and is a large wave 
+        lastAvgWaveVal = avgWaveVal/(waveLenCnt);
           firstFullWave = waveStart;
           fullWaveLen=waveLenCnt;
           //if average wave value is > graph 0 then it is an up wave or a 1
-          if (lastAvgWaveVal > 128) curPhase^=1;
+        if (lastAvgWaveVal > 123) curPhase^=1;  //fudge graph 0 a little 123 vs 128
           break;
         } 
-        waveStart=0;
-        avgWaveVal=0;
+      waveStart = i+1;
+      avgWaveVal = 0;
       }
+    avgWaveVal+=dest[i+2];
     }
-    avgWaveVal+=dest[i+1];
-  }
   //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);  
   lastClkBit = firstFullWave; //set start of wave as clock align
+  //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
   waveStart = 0;
   errCnt=0;
   size_t numBits=0;
-  //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
   //set skipped bits
-  memset(dest+numBits, curPhase^1,firstFullWave / *clock);
+  memset(dest+numBits,curPhase^1,firstFullWave / *clock);
   numBits += (firstFullWave / *clock);
+  dest[numBits++] = curPhase; //set first read bit
   for (i = firstFullWave+fullWaveLen-1; i < *size-3; i++){
     //top edge of wave = start of new wave 
     if (dest[i]+fc < dest[i+1] && dest[i+1] >= dest[i+2]){
@@ -1652,20 +1646,17 @@ int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
           //PrintAndLog("DEBUG: phase shift at: %d, len: %d, nextClk: %d, i: %d, fc: %d",waveStart,waveLenCnt,lastClkBit+*clock-tol,i+1,fc);
           if (i+1 >= lastClkBit + *clock - tol){ //should be a clock bit
             curPhase^=1;
-            dest[numBits] = curPhase;
-            numBits++;
+            dest[numBits++] = curPhase;
             lastClkBit += *clock;
-          } else if (i<lastClkBit+10){
+          } else if (i<lastClkBit+10+fc){
             //noise after a phase shift - ignore
           } else { //phase shift before supposed to based on clock
             errCnt++;
-            dest[numBits] = 77;
-            numBits++;
+            dest[numBits++] = 77;
           }
         } else if (i+1 > lastClkBit + *clock + tol + fc){
           lastClkBit += *clock; //no phase shift but clock bit
-          dest[numBits] = curPhase;
-          numBits++;
+          dest[numBits++] = curPhase;
         }
         avgWaveVal=0;
         waveStart=i+1;
Impressum, Datenschutz