Skip to content

Commit

Permalink
Decoder cleanups from experimental branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
mutability committed Jan 21, 2015
1 parent 0433ed3 commit 5e522fe
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 452 deletions.
29 changes: 15 additions & 14 deletions demod_2000.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
int msglen, scanlen;
uint32_t sigLevel, noiseLevel;
uint16_t snr;
int message_ok;

pPreamble = &m[j];
pPayload = &m[j+MODES_PREAMBLE_SAMPLES];
Expand All @@ -325,7 +326,6 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
// is required for every bit of the input stream, and we don't want to be memset-ing the whole
// modesMessage structure two million times per second if we don't have to..
mm.bFlags =
mm.crcok =
mm.correctedbits = 0;

if (!use_correction) // This is not a re-try with phase correction
Expand Down Expand Up @@ -544,7 +544,7 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
mm.phase_corrected = use_correction;

// Decode the received message
decodeModesMessage(&mm, msg);
message_ok = (decodeModesMessage(&mm, msg) >= 0);

// Update statistics
if (Modes.stats) {
Expand All @@ -557,16 +557,16 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
default: dstats->demodulated3++; break;
}

if (mm.crcok) {
if (!message_ok) {
dstats->badcrc++;
} else if (mm.correctedbits == 0) {
dstats->goodcrc++;
dstats->goodcrc_byphase[0]++;
} else if (mm.correctedbits > 0) {
} else {
dstats->badcrc++;
dstats->fixed++;
if (mm.correctedbits <= MODES_MAX_BITERRORS)
dstats->bit_fix[mm.correctedbits-1] += 1;
} else {
dstats->badcrc++;
}
}

Expand All @@ -576,30 +576,31 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
dumpRawMessage("Demodulated with 0 errors", msg, m, j);
else if (Modes.debug & MODES_DEBUG_BADCRC &&
mm.msgtype == 17 &&
(!mm.crcok || mm.correctedbits != 0))
(!message_ok || mm.correctedbits > 0))
dumpRawMessage("Decoded with bad CRC", msg, m, j);
else if (Modes.debug & MODES_DEBUG_GOODCRC && mm.crcok &&
else if (Modes.debug & MODES_DEBUG_GOODCRC &&
message_ok &&
mm.correctedbits == 0)
dumpRawMessage("Decoded with good CRC", msg, m, j);
}

// Skip this message if we are sure it's fine
if (mm.crcok || mm.correctedbits) {
if (message_ok) {
j += (MODES_PREAMBLE_US+msglen)*2 - 1;
}

// Pass data to the next layer
useModesMessage(&mm);

// Pass data to the next layer
useModesMessage(&mm);
}
} else {
message_ok = 0;
if (Modes.debug & MODES_DEBUG_DEMODERR && use_correction) {
printf("The following message has %d demod errors\n", errors);
dumpRawMessage("Demodulated with errors", msg, m, j);
}
}

// Retry with phase correction if enabled, necessary and possible.
if (Modes.phase_enhance && !mm.crcok && !mm.correctedbits && !use_correction && j && detectOutOfPhase(pPreamble)) {
if (Modes.phase_enhance && (!message_ok || mm.correctedbits > 0) && !use_correction && j && detectOutOfPhase(pPreamble)) {
use_correction = 1; j--;
} else {
use_correction = 0;
Expand Down
17 changes: 9 additions & 8 deletions demod_2400.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ void demodulate2400(uint16_t *m, uint32_t mlen)
// is required for every possible preamble, and we don't want to be memset-ing the whole
// modesMessage structure if we don't have to..
mm.bFlags =
mm.crcok =
mm.correctedbits = 0;

// Decode all the next 112 bits, regardless of the actual message
Expand Down Expand Up @@ -456,13 +455,15 @@ void demodulate2400(uint16_t *m, uint32_t mlen)
if ( (msglen)
// && ((2 * snr) > (int) (MODES_MSG_SQUELCH_DB * 10))
&& (errors <= MODES_MSG_ENCODER_ERRS) ) {
int message_ok;

// Set initial mm structure details
mm.timestampMsg = Modes.timestampBlk + (j*5) + try_phase;
mm.signalLevel = (snr > 255 ? 255 : (uint8_t)snr);
mm.phase_corrected = (initial_phase != try_phase);

// Decode the received message
decodeModesMessage(&mm, msg);
message_ok = (decodeModesMessage(&mm, msg) >= 0);

// Update statistics
if (Modes.stats) {
Expand All @@ -475,16 +476,16 @@ void demodulate2400(uint16_t *m, uint32_t mlen)
default: dstats->demodulated3++; break;
}

if (mm.crcok) {
dstats->goodcrc++;
dstats->goodcrc_byphase[try_phase%MODES_MAX_PHASE_STATS]++;
if (!message_ok) {
dstats->badcrc++;
} else if (mm.correctedbits > 0) {
dstats->badcrc++;
dstats->fixed++;
if (mm.correctedbits <= MODES_MAX_BITERRORS)
dstats->bit_fix[mm.correctedbits-1] += 1;
} else {
dstats->badcrc++;
dstats->goodcrc++;
dstats->goodcrc_byphase[try_phase%MODES_MAX_PHASE_STATS]++;
}
}

Expand All @@ -494,7 +495,7 @@ void demodulate2400(uint16_t *m, uint32_t mlen)
// where the preamble of the second message clobbered the last
// few bits of the first message, but the message bits didn't
// overlap)
if (mm.crcok || mm.correctedbits) {
if (message_ok) {
j += (8 + msglen - 8)*12/5 - 1;
}

Expand All @@ -506,7 +507,7 @@ void demodulate2400(uint16_t *m, uint32_t mlen)
// where trying different phases actually helps, and is much
// cheaper than trying it on every single candidate that passes
// peak detection
if (Modes.phase_enhance && !mm.crcok && !mm.correctedbits) {
if (Modes.phase_enhance && !message_ok) {
if (try_phase == initial_phase)
++Modes.stats_current.out_of_phase;
try_phase++;
Expand Down
11 changes: 8 additions & 3 deletions dump1090.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ struct modesMessage {
unsigned char msg[MODES_LONG_MSG_BYTES]; // Binary message.
int msgbits; // Number of bits in message
int msgtype; // Downlink format #
int crcok; // True if CRC was valid
uint32_t crc; // Message CRC
int correctedbits; // No. of bits corrected
char corrected[MODES_MAX_BITERRORS]; // corrected bit positions
Expand All @@ -416,7 +415,7 @@ struct modesMessage {
int remote; // If set this message is from a remote station
unsigned char signalLevel; // Signal Amplitude

// DF 11
// DF 11, DF 17
int ca; // Responder capabilities
int iid;

Expand All @@ -434,10 +433,16 @@ struct modesMessage {
int vert_rate; // Vertical rate.
int velocity; // Reported by aircraft, or computed from from EW and NS velocity

// DF 18
int cf; // Control Field

// DF4, DF5, DF20, DF21
int fs; // Flight status for DF4,5,20,21
int modeA; // 13 bits identity (Squawk).

// DF20, DF21
int bds; // BDS value implied if overlay control was used

// Fields used by multiple message types.
int altitude;
int unit;
Expand All @@ -462,7 +467,7 @@ int ModeAToModeC (unsigned int ModeA);
//
int modesMessageLenByType(int type);
void detectModeS_oversample (uint16_t *m, uint32_t mlen);
void decodeModesMessage (struct modesMessage *mm, unsigned char *msg);
int decodeModesMessage (struct modesMessage *mm, unsigned char *msg);
void displayModesMessage(struct modesMessage *mm);
void useModesMessage (struct modesMessage *mm);
void computeMagnitudeVector(uint16_t *pData);
Expand Down
2 changes: 1 addition & 1 deletion icao_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#define MODES_ICAO_FILTER_TTL 60

// Open-addressed hash table with linear probing.
// We store each address twice to handle Address/Parity and Data/Parity
// We store each address twice to handle Data/Parity
// which need to match on a partial address (top 16 bits only).

// Maintain two tables and switch between them to age out entries.
Expand Down
8 changes: 1 addition & 7 deletions interactive.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,7 @@ static void updatePosition(struct aircraft *a, struct modesMessage *mm, time_t n

struct aircraft *interactiveReceiveData(struct modesMessage *mm) {
struct aircraft *a, *aux;
time_t now;

// Return if (checking crc) AND (not crcok) AND (not fixed)
if (Modes.check_crc && (mm->crcok == 0) && (mm->correctedbits == 0))
return NULL;

now = time(NULL);
time_t now = time(NULL);

// Lookup our aircraft or create a new one
a = interactiveFindAircraft(mm->addr);
Expand Down
1 change: 0 additions & 1 deletion mode_ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ void decodeModeAMessage(struct modesMessage *mm, int ModeA)

// Not much else we can tell from a Mode A/C reply.
// Just fudge up a few bits to keep other code happy
mm->crcok = 1;
mm->correctedbits = 0;
}
//
Expand Down
Loading

0 comments on commit 5e522fe

Please sign in to comment.