+//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 *mostFC)
+{
+ uint8_t fcLens[] = {0,0,0,0,0,0,0,0,0,0};
+ uint16_t fcCnts[] = {0,0,0,0,0,0,0,0,0,0};
+ uint8_t fcLensFnd = 0;
+ uint8_t lastFCcnt=0;
+ uint32_t fcCounter = 0;
+ size_t i;
+ if (size == 0) return 0;
+
+ // prime i to first up transition
+ for (i = 1; i < size-1; i++)
+ if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1])
+ break;
+
+ for (; i < size-1; i++){
+ if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1]){
+ // new up transition
+ fcCounter++;
+
+ //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 odd and not rc/5 add one (for when we get a fc 9 instead of 10)
+ if ((fcCounter==9 && fcCounter & 1) || fcCounter==4) fcCounter++;
+
+ // save last field clock count (fc/xx)
+ // find which fcLens to save it to:
+ for (int ii=0; ii<10; ii++){
+ if (fcLens[ii]==fcCounter){
+ fcCnts[ii]++;
+ fcCounter=0;
+ break;
+ }
+ }
+ if (fcCounter>0 && fcLensFnd<10){
+ //add new fc length
+ fcCnts[fcLensFnd]++;
+ fcLens[fcLensFnd++]=fcCounter;
+ }
+ fcCounter=0;
+ } else {
+ // count sample
+ fcCounter++;
+ }
+ }
+
+ uint8_t best1=9, best2=9, best3=9;
+ uint16_t maxCnt1=0;
+ // go through fclens and find which ones are bigest 2
+ for (i=0; i<10; i++){
+ // PrintAndLog("DEBUG: FC %d, Cnt %d, Errs %d",fcLens[i],fcCnts[i],errCnt);
+ // get the 3 best FC values
+ if (fcCnts[i]>maxCnt1) {
+ best3=best2;
+ best2=best1;
+ maxCnt1=fcCnts[i];
+ best1=i;
+ } else if(fcCnts[i]>fcCnts[best2]){
+ best3=best2;
+ best2=i;
+ } else if(fcCnts[i]>fcCnts[best3]){
+ best3=i;
+ }
+ }
+ uint8_t fcH=0, fcL=0;
+ if (fcLens[best1]>fcLens[best2]){
+ fcH=fcLens[best1];
+ fcL=fcLens[best2];
+ } else{
+ fcH=fcLens[best2];
+ fcL=fcLens[best1];
+ }
+
+ *mostFC=fcLens[best1];
+ // TODO: take top 3 answers and compare to known Field clocks to get top 2
+
+ uint16_t fcs = (((uint16_t)fcH)<<8) | fcL;
+ // PrintAndLog("DEBUG: Best %d best2 %d best3 %d",fcLens[best1],fcLens[best2],fcLens[best3]);
+
+ return fcs;
+}
+
+//by marshmellow
+//countPSK_FC is to detect the psk carrier clock length.
+//counts and returns the 1 most common wave length
+uint8_t countPSK_FC(uint8_t *BitStream, size_t size)
+{
+ uint8_t fcLens[] = {0,0,0,0,0,0,0,0,0,0};
+ uint16_t fcCnts[] = {0,0,0,0,0,0,0,0,0,0};
+ uint8_t fcLensFnd = 0;
+ uint32_t fcCounter = 0;
+ size_t i;
+ if (size == 0) return 0;
+
+ // prime i to first up transition
+ for (i = 1; i < size-1; i++)
+ if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1])
+ break;
+
+ for (; i < size-1; i++){
+ if (BitStream[i] > BitStream[i-1] && BitStream[i] >= BitStream[i+1]){
+ // new up transition
+ fcCounter++;
+
+ // save last field clock count (fc/xx)
+ // find which fcLens to save it to:
+ for (int ii=0; ii<10; ii++){
+ if (fcLens[ii]==fcCounter){
+ fcCnts[ii]++;
+ fcCounter=0;
+ break;
+ }
+ }
+ if (fcCounter>0 && fcLensFnd<10){
+ //add new fc length
+ fcCnts[fcLensFnd]++;
+ fcLens[fcLensFnd++]=fcCounter;
+ }
+ fcCounter=0;
+ } else {
+ // count sample
+ fcCounter++;
+ }
+ }
+
+ uint8_t best1=9;
+ uint16_t maxCnt1=0;
+ // go through fclens and find which ones are bigest
+ for (i=0; i<10; i++){
+ //PrintAndLog("DEBUG: FC %d, Cnt %d",fcLens[i],fcCnts[i]);
+ // get the best FC value
+ if (fcCnts[i]>maxCnt1) {
+ maxCnt1=fcCnts[i];
+ best1=i;
+ }
+ }
+ return fcLens[best1];
+}
+
+//by marshmellow - demodulate PSK1 wave
+//uses wave lengths (# Samples)
+int pskRawDemod(uint8_t dest[], size_t *size, int *clock, int *invert)
+{
+ uint16_t loopCnt = 4096; //don't need to loop through entire array...
+ if (size == 0) return -1;
+ if (*size<loopCnt) loopCnt = *size;
+
+ uint8_t curPhase = *invert;
+ size_t i, waveStart=0, 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);
+ if (fc!=2 && fc!=4 && fc!=8) return -1;
+ //PrintAndLog("DEBUG: FC: %d",fc);
+ *clock = DetectPSKClock(dest, *size, *clock);
+ if (*clock==0) return -1;
+ int avgWaveVal=0, lastAvgWaveVal=0;
+ //find first full wave
+ 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){
+ 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;
+ break;
+ }
+ waveStart=0;
+ avgWaveVal=0;
+ }
+ }
+ avgWaveVal+=dest[i+1];
+ }
+ //PrintAndLog("DEBUG: firstFullWave: %d, waveLen: %d",firstFullWave,fullWaveLen);
+ lastClkBit = firstFullWave; //set start of wave as clock align
+ waveStart = 0;
+ errCnt=0;
+ size_t numBits=0;
+ //PrintAndLog("DEBUG: clk: %d, lastClkBit: %d", *clock, lastClkBit);
+
+ 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]){
+ if (waveStart == 0) {
+ waveStart = i+1;
+ waveLenCnt=0;
+ avgWaveVal = dest[i+1];
+ } else { //waveEnd
+ waveEnd = i+1;
+ waveLenCnt = waveEnd-waveStart;
+ lastAvgWaveVal = avgWaveVal/waveLenCnt;
+ if (waveLenCnt > fc){
+ //PrintAndLog("DEBUG: avgWaveVal: %d, waveSum: %d",lastAvgWaveVal,avgWaveVal);
+ //if this wave is a phase shift
+ //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++;
+ lastClkBit += *clock;
+ } else if (i<lastClkBit+10){
+ //noise after a phase shift - ignore
+ } else { //phase shift before supposed to based on clock
+ errCnt++;
+ dest[numBits] = 77;
+ numBits++;
+ }
+ } else if (i+1 > lastClkBit + *clock + tol + fc){
+ lastClkBit += *clock; //no phase shift but clock bit
+ dest[numBits] = curPhase;
+ numBits++;
+ }
+ avgWaveVal=0;
+ waveStart=i+1;
+ }
+ }
+ avgWaveVal+=dest[i+1];
+ }
+ *size = numBits;
+ return errCnt;
+}