+ switch(Demod->state) {
+ case DEMOD_UNSYNCD:
+ // initialize SOF correlator. We are looking for 12 samples low and 12 samples high.
+ Demod->SOF_low = 0;
+ Demod->SOF_high = 12;
+ Demod->SOF_last = 23;
+ memset(Demod->SOF_correlator, 0x00, Demod->SOF_last + 1);
+ Demod->SOF_correlator[Demod->SOF_last] = AMPLITUDE(ci,cq);
+ Demod->SOF_corr = Demod->SOF_correlator[Demod->SOF_last];
+ Demod->SOF_corr_prev = Demod->SOF_corr;
+ // initialize Demodulator
+ Demod->posCount = 0;
+ Demod->bitCount = 0;
+ Demod->len = 0;
+ Demod->state = DEMOD_AWAIT_SOF_1;
+ break;
+
+ case DEMOD_AWAIT_SOF_1:
+ // calculate the correlation in real time. Look at differences only.
+ Demod->SOF_corr += Demod->SOF_correlator[Demod->SOF_low++];
+ Demod->SOF_corr -= 2*Demod->SOF_correlator[Demod->SOF_high++];
+ Demod->SOF_last++;
+ Demod->SOF_low &= (SOF_CORRELATOR_LEN-1);
+ Demod->SOF_high &= (SOF_CORRELATOR_LEN-1);
+ Demod->SOF_last &= (SOF_CORRELATOR_LEN-1);
+ Demod->SOF_correlator[Demod->SOF_last] = AMPLITUDE(ci,cq);
+ Demod->SOF_corr += Demod->SOF_correlator[Demod->SOF_last];
+
+ // if correlation increases for 10 consecutive samples, we are close to maximum correlation
+ if (Demod->SOF_corr > Demod->SOF_corr_prev + SUBCARRIER_DETECT_THRESHOLD) {
+ Demod->posCount++;
+ } else {
+ Demod->posCount = 0;
+ }