- // Error check: if bitidx becomes too large, we do not
- // have a Manchester encoded bitstream or the clock is really
- // wrong!
- if (bitidx > (bytelength*2/clock+8) ) {
- PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
- return 0;
- }
- // Then switch depending on lc length:
- // Tolerance is 1/4 of clock rate (arbitrary)
- if (abs(lc-clock/2) < tolerance) {
- // Short pulse : either "1" or "0"
- bitStream[bitidx++] = data[i-1];
- } else if (abs(lc-clock) < tolerance) {
- // Long pulse: either "11" or "00"
- bitStream[bitidx++] = data[i-1];
- bitStream[bitidx++] = data[i-1];
- } else {
- // Error
- warnings++;
- PrintAndLog("Warning: Manchester decode error for pulse width detection.");
- if (warnings > 10) {
- PrintAndLog("Error: too many detection errors, aborting.");
- return 0;
- }
- }
- }
- }
- }
- // At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
- // Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
- // to stop output at the final bitidx2 value, not bitidx
- for (i = 0; i < bitidx; i += 2) {
- if ((bitStream[i] == 0) && (bitStream[i+1] == 1)) {
- bitStream[bit2idx++] = 1 ^ invert;
- }
- else if ((bitStream[i] == 1) && (bitStream[i+1] == 0)) {
- bitStream[bit2idx++] = 0 ^ invert;
- }
- else {
- // We cannot end up in this state, this means we are unsynchronized,
- // move up 1 bit:
- i++;
- warnings++;
- PrintAndLog("Unsynchronized, resync...");
- if (warnings > 10) {
- PrintAndLog("Error: too many decode errors, aborting.");
- return 0;
- }
- }
- }
-
- // PrintAndLog(" Manchester decoded bitstream : %d bits", (bit2idx-16));
- // uint8_t mod = (bit2idx-16) % blocksize;
- // uint8_t div = (bit2idx-16) / blocksize;
-
- // // Now output the bitstream to the scrollback by line of 16 bits
- // for (i = 0; i < div*blocksize; i+=blocksize) {
- // PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
- // }
- // if ( mod > 0 ){
- // PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
- // }
-
- if ( bit2idx > 0 )
- memcpy(dataout, bitStream, bit2idx);
-
- free(bitStream);
- return bit2idx;
+ sample = data[i];
+
+ // remove DC offset and mix to complex baseband
+ x = (sample - 127.5f) * cexpf( _Complex_I * 2 * M_PI * fc * i );
+
+ // apply low-pass filter, removing spectral image (IIR using direct-form II)
+ iir_buf[2] = iir_buf[1];
+ iir_buf[1] = iir_buf[0];
+ iir_buf[0] = x - a[1]*iir_buf[1] - a[2]*iir_buf[2];
+ x = b[0]*iir_buf[0] +
+ b[1]*iir_buf[1] +
+ b[2]*iir_buf[2];
+
+ // compute instantaneous frequency by looking at phase difference
+ // between adjacent samples
+ float freq = cargf(x*conjf(x_prime));
+ x_prime = x; // retain this sample for next iteration
+
+ output[i] =(freq > 0)? 10 : -10;
+ }
+
+ // show data
+ for (j=0; j<adjustedLen; ++j)
+ data[j] = output[j];
+
+ free(output);