diff --git a/firmware/baseband/proc_adsbrx.cpp b/firmware/baseband/proc_adsbrx.cpp index a82cbc81..faef978f 100644 --- a/firmware/baseband/proc_adsbrx.cpp +++ b/firmware/baseband/proc_adsbrx.cpp @@ -32,7 +32,7 @@ using namespace adsb; void ADSBRXProcessor::execute(const buffer_c8_t& buffer) { int8_t re, im; - float mag; + uint32_t mag; uint32_t c; uint8_t bit, byte{}; @@ -45,9 +45,9 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) { for (size_t i = 0; i < buffer.count; i++) { // Compute sample's magnitude - re = buffer.p[i].real(); // make re float and scale it - im = buffer.p[i].imag(); // make re float and scale it - mag = ((re * re) + (im * im)) * (k*k); + re = (int32_t)buffer.p[i].real(); // make re float and scale it + im = (int32_t)buffer.p[i].imag(); // make re float and scale it + mag = ((uint32_t)(re*re) + (uint32_t)(im*im)); if (decoding) { // Decode @@ -70,19 +70,20 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) { byte = bit | (byte << 1); bit_count++; - // Only DF 17 are used so reset if anything else if found + // Perform checks at the end of the first byte if (!(bit_count & 7)) { // Store the byte frame.push_byte(byte); // Check at the end of the first byte of the message - if ( (bit_count == 8) && !(byte & (0x10<<3)) ) { + uint8_t df = (byte >> 3); + if ( (bit_count == 8) && !(df & 0x10) ) { msgLen = 56; // DFs 16 or greater are long 112. DFs 15 or less are short 56. } - // Abondon all frames that arent DF17 - if ( (bit_count == 8) && ((byte>>3) != 17) ) { + // Abondon all frames that arent DF17 or DF18 extended squitters + if ( (bit_count == 8) && !((df == 17)||(df == 18)) ) { decoding = false; bit = (prev_mag > mag) ? 1 : 0; frame.clear(); @@ -91,67 +92,59 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) { } // Second sample of each bit sample_count++; } - //else + // Continue looking for preamble even if in a packet // switch is new preamble id higher magnitude + + // Shift the preamble + for (c = 0; c < (ADSB_PREAMBLE_LENGTH ); c++) { shifter[c] = shifter[c + 1]; } + shifter[ADSB_PREAMBLE_LENGTH] = mag; + + // First check of relations between the first 10 samples + // representing a valid preamble. We don't even investigate further + // if this simple test is not passed + if (shifter[0] < shifter[1] && + shifter[1] > shifter[2] && + shifter[2] < shifter[3] && + shifter[3] > shifter[4] && + shifter[4] < shifter[1] && + shifter[5] < shifter[1] && + shifter[6] < shifter[1] && + shifter[7] < shifter[1] && + shifter[8] > shifter[9] && + shifter[9] < shifter[10] && + shifter[10]> shifter[11] ) { - // Look for preamble - - // Shift - for (c = 0; c < (ADSB_PREAMBLE_LENGTH - 1); c++) + // The samples between the two spikes must be < than the average + // of the high spikes level. We don't test bits too near to + // the high levels as signals can be out of phase so part of the + // energy can be in the near samples + int32_t thisAmp = (shifter[1] + shifter[3] + shifter[8] + shifter[10]); + int32_t high = thisAmp / 9; + if ( + shifter[5] < high && + shifter[6] < high && + // Similarly samples in the range 11-13 must be low, as it is the + // space between the preamble and real data. Again we don't test + // bits too near to high levels, see above + shifter[12] < high && + shifter[13] < high && + shifter[14] < high ) { - shifter[c] = shifter[c + 1]; - } - shifter[15] = mag; - - // First check of relations between the first 10 samples - // representing a valid preamble. We don't even investigate further - // if this simple test is not passed - if (shifter[0] > shifter[1] && - shifter[1] < shifter[2] && - shifter[2] > shifter[3] && - shifter[3] < shifter[0] && - shifter[4] < shifter[0] && - shifter[5] < shifter[0] && - shifter[6] < shifter[0] && - shifter[7] > shifter[8] && - shifter[8] < shifter[9] && - shifter[9] > shifter[6]) - { - // The samples between the two spikes must be < than the average - // of the high spikes level. We don't test bits too near to - // the high levels as signals can be out of phase so part of the - // energy can be in the near samples - float thisAmp = (shifter[0] + shifter[2] + shifter[7] + shifter[9]); - float high = thisAmp / 7.0f; - if (shifter[4] < high && - shifter[5] < high) + if ((decoding == false) || // New preamble + ((decoding == true) && (thisAmp > amp))) // Higher power than existing packet { + decoding = true; + msgLen = 112; + amp = thisAmp; + sample_count = 0; + bit_count = 0; + frame.clear(); + } + } // 4 & 5 low and 11-14 low + } // Check for preamble pattern - // Similarly samples in the range 11-14 must be low, as it is the - // space between the preamble and real data. Again we don't test - // bits too near to high levels, see above - if (shifter[11] < high && - shifter[12] < high && - shifter[13] < high && - shifter[14] < high) - { - if ( (decoding == false) || // New preamble - ((decoding == true)&& (thisAmp > amp)) ) // Higher power than existing packet - { - decoding = true; - msgLen = 112; - amp = thisAmp; - sample_count = 0; - bit_count = 0; - frame.clear(); - } - - } // 11-14 low - } // 4 & 5 high - } // Check for preamble pattern - } - + // Store mag for next time prev_mag = mag; } } diff --git a/firmware/baseband/proc_adsbrx.hpp b/firmware/baseband/proc_adsbrx.hpp index 8d09e6b9..5f4cd42a 100644 --- a/firmware/baseband/proc_adsbrx.hpp +++ b/firmware/baseband/proc_adsbrx.hpp @@ -40,8 +40,6 @@ public: void on_message(const Message* const message) override; private: - static constexpr float k = 1.0f / 128.0f; - static constexpr size_t baseband_fs = 2000000; BasebandThread baseband_thread { baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive }; @@ -49,20 +47,17 @@ private: ADSBFrame frame { }; bool configured { false }; - float prev_mag { 0 }; - float threshold { }, threshold_low { }, threshold_high { }; - //size_t null_count{ 0 }; + uint32_t prev_mag { 0 }; size_t bit_count { 0 }, sample_count { 0 }; size_t msgLen{ 112 }; - //std::pair shifter[ADSB_PREAMBLE_LENGTH]; - float shifter[ADSB_PREAMBLE_LENGTH]; + uint32_t shifter[ADSB_PREAMBLE_LENGTH+1]; bool decoding { }; bool preamble { }, active { }; uint16_t bit_pos { 0 }; uint8_t cur_bit { 0 }; uint32_t sample { 0 }; - int8_t re { }, im { }; - float amp {0.0f}; + int32_t re { }, im { }; + int32_t amp {0}; }; #endif diff --git a/firmware/common/message.hpp b/firmware/common/message.hpp index 685022c3..a9821d83 100644 --- a/firmware/common/message.hpp +++ b/firmware/common/message.hpp @@ -377,7 +377,7 @@ class ADSBFrameMessage : public Message { public: constexpr ADSBFrameMessage( const adsb::ADSBFrame& frame, - const float amp + const uint32_t amp ) : Message { ID::ADSBFrame }, frame { frame }, amp(amp) @@ -385,7 +385,7 @@ public: } adsb::ADSBFrame frame; - float amp; + uint32_t amp; }; class AFSKDataMessage : public Message {