Changed amp to integer

This commit is contained in:
heurist1 2021-11-06 10:15:17 +00:00
parent 79b24ebe13
commit a571e40c59
3 changed files with 62 additions and 74 deletions

View File

@ -32,7 +32,7 @@ using namespace adsb;
void ADSBRXProcessor::execute(const buffer_c8_t& buffer) { void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
int8_t re, im; int8_t re, im;
float mag; uint32_t mag;
uint32_t c; uint32_t c;
uint8_t bit, byte{}; 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++) { for (size_t i = 0; i < buffer.count; i++) {
// Compute sample's magnitude // Compute sample's magnitude
re = buffer.p[i].real(); // make re float and scale it re = (int32_t)buffer.p[i].real(); // make re float and scale it
im = buffer.p[i].imag(); // make re float and scale it im = (int32_t)buffer.p[i].imag(); // make re float and scale it
mag = ((re * re) + (im * im)) * (k*k); mag = ((uint32_t)(re*re) + (uint32_t)(im*im));
if (decoding) { if (decoding) {
// Decode // Decode
@ -70,19 +70,20 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
byte = bit | (byte << 1); byte = bit | (byte << 1);
bit_count++; 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)) { if (!(bit_count & 7)) {
// Store the byte // Store the byte
frame.push_byte(byte); frame.push_byte(byte);
// Check at the end of the first byte of the message // 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. msgLen = 56; // DFs 16 or greater are long 112. DFs 15 or less are short 56.
} }
// Abondon all frames that arent DF17 // Abondon all frames that arent DF17 or DF18 extended squitters
if ( (bit_count == 8) && ((byte>>3) != 17) ) { if ( (bit_count == 8) && !((df == 17)||(df == 18)) ) {
decoding = false; decoding = false;
bit = (prev_mag > mag) ? 1 : 0; bit = (prev_mag > mag) ? 1 : 0;
frame.clear(); frame.clear();
@ -91,53 +92,47 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
} // Second sample of each bit } // Second sample of each bit
sample_count++; sample_count++;
} }
//else
// Continue looking for preamble even if in a packet // Continue looking for preamble even if in a packet
// switch is new preamble id higher magnitude // switch is new preamble id higher magnitude
{
// Look for preamble
// Shift // Shift the preamble
for (c = 0; c < (ADSB_PREAMBLE_LENGTH - 1); c++) for (c = 0; c < (ADSB_PREAMBLE_LENGTH ); c++) { shifter[c] = shifter[c + 1]; }
{ shifter[ADSB_PREAMBLE_LENGTH] = mag;
shifter[c] = shifter[c + 1];
}
shifter[15] = mag;
// First check of relations between the first 10 samples // First check of relations between the first 10 samples
// representing a valid preamble. We don't even investigate further // representing a valid preamble. We don't even investigate further
// if this simple test is not passed // if this simple test is not passed
if (shifter[0] > shifter[1] && if (shifter[0] < shifter[1] &&
shifter[1] < shifter[2] && shifter[1] > shifter[2] &&
shifter[2] > shifter[3] && shifter[2] < shifter[3] &&
shifter[3] < shifter[0] && shifter[3] > shifter[4] &&
shifter[4] < shifter[0] && shifter[4] < shifter[1] &&
shifter[5] < shifter[0] && shifter[5] < shifter[1] &&
shifter[6] < shifter[0] && shifter[6] < shifter[1] &&
shifter[7] > shifter[8] && shifter[7] < shifter[1] &&
shifter[8] < shifter[9] && shifter[8] > shifter[9] &&
shifter[9] > shifter[6]) shifter[9] < shifter[10] &&
shifter[10]> shifter[11] )
{ {
// The samples between the two spikes must be < than the average // The samples between the two spikes must be < than the average
// of the high spikes level. We don't test bits too near to // 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 // the high levels as signals can be out of phase so part of the
// energy can be in the near samples // energy can be in the near samples
float thisAmp = (shifter[0] + shifter[2] + shifter[7] + shifter[9]); int32_t thisAmp = (shifter[1] + shifter[3] + shifter[8] + shifter[10]);
float high = thisAmp / 7.0f; int32_t high = thisAmp / 9;
if (shifter[4] < high && if (
shifter[5] < high) shifter[5] < high &&
{ shifter[6] < high &&
// Similarly samples in the range 11-13 must be low, as it is the
// 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 // space between the preamble and real data. Again we don't test
// bits too near to high levels, see above // bits too near to high levels, see above
if (shifter[11] < high &&
shifter[12] < high && shifter[12] < high &&
shifter[13] < high && shifter[13] < high &&
shifter[14] < high) shifter[14] < high )
{ {
if ( (decoding == false) || // New preamble if ((decoding == false) || // New preamble
((decoding == true)&& (thisAmp > amp)) ) // Higher power than existing packet ((decoding == true) && (thisAmp > amp))) // Higher power than existing packet
{ {
decoding = true; decoding = true;
msgLen = 112; msgLen = 112;
@ -146,12 +141,10 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
bit_count = 0; bit_count = 0;
frame.clear(); frame.clear();
} }
} // 4 & 5 low and 11-14 low
} // 11-14 low
} // 4 & 5 high
} // Check for preamble pattern } // Check for preamble pattern
}
// Store mag for next time
prev_mag = mag; prev_mag = mag;
} }
} }

View File

@ -40,8 +40,6 @@ public:
void on_message(const Message* const message) override; void on_message(const Message* const message) override;
private: private:
static constexpr float k = 1.0f / 128.0f;
static constexpr size_t baseband_fs = 2000000; static constexpr size_t baseband_fs = 2000000;
BasebandThread baseband_thread { baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive }; BasebandThread baseband_thread { baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive };
@ -49,20 +47,17 @@ private:
ADSBFrame frame { }; ADSBFrame frame { };
bool configured { false }; bool configured { false };
float prev_mag { 0 }; uint32_t prev_mag { 0 };
float threshold { }, threshold_low { }, threshold_high { };
//size_t null_count{ 0 };
size_t bit_count { 0 }, sample_count { 0 }; size_t bit_count { 0 }, sample_count { 0 };
size_t msgLen{ 112 }; size_t msgLen{ 112 };
//std::pair<float, uint8_t> shifter[ADSB_PREAMBLE_LENGTH]; uint32_t shifter[ADSB_PREAMBLE_LENGTH+1];
float shifter[ADSB_PREAMBLE_LENGTH];
bool decoding { }; bool decoding { };
bool preamble { }, active { }; bool preamble { }, active { };
uint16_t bit_pos { 0 }; uint16_t bit_pos { 0 };
uint8_t cur_bit { 0 }; uint8_t cur_bit { 0 };
uint32_t sample { 0 }; uint32_t sample { 0 };
int8_t re { }, im { }; int32_t re { }, im { };
float amp {0.0f}; int32_t amp {0};
}; };
#endif #endif

View File

@ -377,7 +377,7 @@ class ADSBFrameMessage : public Message {
public: public:
constexpr ADSBFrameMessage( constexpr ADSBFrameMessage(
const adsb::ADSBFrame& frame, const adsb::ADSBFrame& frame,
const float amp const uint32_t amp
) : Message { ID::ADSBFrame }, ) : Message { ID::ADSBFrame },
frame { frame }, frame { frame },
amp(amp) amp(amp)
@ -385,7 +385,7 @@ public:
} }
adsb::ADSBFrame frame; adsb::ADSBFrame frame;
float amp; uint32_t amp;
}; };
class AFSKDataMessage : public Message { class AFSKDataMessage : public Message {