-//demod GraphBuffer wave to 0s and 1s for each wave - 0s for short waves 1s for long waves
-size_t fsk_wave_demod(int size)
-{
- uint32_t last_transition = 0;
- uint32_t idx = 1;
- uint32_t maxVal = 0;
- // we don't care about actual value, only if it's more or less than a
- // threshold essentially we capture zero crossings for later analysis
- for(idx=1; idx<size; idx++){
- if(maxVal<GraphBuffer[idx]) maxVal = GraphBuffer[idx];
- }
- // set close to the top of the wave threshold with 13% margin for error
- // less likely to get a false transition up there.
- // (but have to be careful not to go too high and miss some short waves)
- uint32_t threshold_value = (uint32_t)(maxVal*.87);
- idx=1;
- // int threshold_value = 100;
-
- // sync to first lo-hi transition, and threshold
- // PrintAndLog("FSK init complete size: %d",size);//debug
- // Need to threshold first sample
- if(GraphBuffer[0] < threshold_value) GraphBuffer[0] = 0;
- else GraphBuffer[0] = 1;
- size_t numBits = 0;
- // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
- // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
- // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
- for(idx = 1; idx < size; idx++) {
- // threshold current value
- if (GraphBuffer[idx] < threshold_value) GraphBuffer[idx] = 0;
- else GraphBuffer[idx] = 1;
- // Check for 0->1 transition
- if (GraphBuffer[idx-1] < GraphBuffer[idx]) { // 0 -> 1 transition
- if (idx-last_transition<6){
- // do nothing with extra garbage (shouldn't be any) noise tolerance?
- } else if(idx-last_transition < 9) {
- GraphBuffer[numBits]=1;
- // Other fsk demods reverse this making the short waves 1 and long waves 0
- // this is really backwards... smaller waves will typically be 0 and larger 1 [marshmellow]
- // but will leave as is and invert when needed later
- } else{
- GraphBuffer[numBits]=0;
- }
- last_transition = idx;
- numBits++;
- // PrintAndLog("numbits %d",numBits);
- }
- }
- return numBits; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
-}
-uint32_t myround(float f)
-{
- if (f >= UINT_MAX) return UINT_MAX;
- return (uint32_t) (f + (float)0.5);
-}
-
-//by marshmellow (from holiman's base)
-//translate 11111100000 to 10
-size_t aggregate_bits(int size, uint8_t rfLen, uint8_t maxConsequtiveBits, uint8_t invert) //,uint8_t l2h_crossing_value
-{
- int lastval=GraphBuffer[0];
- uint32_t idx=0;
- size_t numBits=0;
- uint32_t n=1;
- uint32_t n2=0;
- for( idx=1; idx < size; idx++) {
-
- if (GraphBuffer[idx]==lastval) {
- n++;
- continue;
- }
- // if lastval was 1, we have a 1->0 crossing
- if ( GraphBuffer[idx-1]==1 ) {
- n=myround((float)(n+1)/((float)(rfLen)/(float)8)); //-2 noise tolerance
-
- // n=(n+1) / h2l_crossing_value;
- //truncating could get us into trouble
- //now we will try with actual clock (RF/64 or RF/50) variable instead
- //then devide with float casting then truncate after more acurate division
- //and round to nearest int
- //like n = (((float)n)/(float)rfLen/(float)10);
- } else {// 0->1 crossing
- n=myround((float)(n+1)/((float)(rfLen-2)/(float)10)); // as int 120/6 = 20 as float 120/(64/10) = 18 (18.75)
- //n=(n+1) / l2h_crossing_value;
- }
- if (n == 0) n = 1; //this should never happen... should we error if it does?
-
- if (n < maxConsequtiveBits) // Consecutive //when the consecutive bits are low - the noise tolerance can be high
- //if it is high then we must be careful how much noise tolerance we allow
- {
- if (invert==0){ // do not invert bits
- for (n2=0; n2<n; n2++){
- GraphBuffer[numBits+n2]=GraphBuffer[idx-1];
- }
- //memset(GraphBuffer+numBits, GraphBuffer[idx-1] , n);
- }else{ // invert bits
- for (n2=0; n2<n; n2++){
- GraphBuffer[numBits+n2]=GraphBuffer[idx-1]^1;
- }
- //memset(GraphBuffer+numBits, GraphBuffer[idx-1]^1 , n);
- }
- numBits += n;
- }
- n=0;
- lastval=GraphBuffer[idx];
- }//end for
- return numBits;
-}
-
-//by marshmellow (from holiman's base)
-// full fsk demod from GraphBuffer wave to decoded 1s and 0s (no mandemod)
-size_t fskdemod(uint8_t rfLen, uint8_t invert)
-{
- //uint8_t h2l_crossing_value = 6;
- //uint8_t l2h_crossing_value = 5;
-
- // if (rfLen==64) //currently only know settings for RF/64 change from default if option entered
- // {
- // h2l_crossing_value=8; //or 8 as 64/8 = 8
- // l2h_crossing_value=6; //or 6.4 as 64/10 = 6.4
- // }
- size_t size = GraphTraceLen;
- // FSK demodulator
- size = fsk_wave_demod(size);
- size = aggregate_bits(size,rfLen,192,invert);
- // size = aggregate_bits(size, h2l_crossing_value, l2h_crossing_value,192, invert); //192=no limit to same values
- //done messing with GraphBuffer - repaint
- RepaintGraphWindow();
- return size;
-}
-uint32_t bytebits_to_byte(int* src, int numbits)
-{
- uint32_t num = 0;
- for(int i = 0 ; i < numbits ; i++)
- {
- num = (num << 1) | (*src);
- src++;
- }
- return num;
-}
-
-//by marshmellow
-//fsk demod and print binary