From 192cb80c0801ae844aeae63cfb271abcfb966b55 Mon Sep 17 00:00:00 2001 From: Malcolm Robb Date: Mon, 8 Apr 2013 17:55:17 +0100 Subject: [PATCH 1/4] Speed up the I/Q to magnitude calculation Increase the speed of the I/Q to magnitude calculation lookup by expanding the table to 65536 entries (256*256*2 bytes). At runtime, this allows us to pick up raw I/Q bytes as a 16 bit value and index into the magnitude table to get a 16 bit result. This removes the need for subtracting 127, and then correcting for -ve numbers, so should be faster, at the expense of a larger data table. Change the maglut lookup table from 129*129 to 256*256 Initialise the maglut buffer accordingly Change the data->maglut lookup to use the new maglut buffer --- dump1090.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/dump1090.c b/dump1090.c index 0a1ec5e..c962816 100644 --- a/dump1090.c +++ b/dump1090.c @@ -133,7 +133,7 @@ struct { pthread_t reader_thread; pthread_mutex_t data_mutex; /* Mutex to synchronize buffer access. */ pthread_cond_t data_cond; /* Conditional variable associated. */ - unsigned char *data; /* Raw IQ samples buffer */ + uint16_t *data; /* Raw IQ samples buffer */ uint16_t *magnitude; /* Magnitude vector */ long long timestampBlk; /* Timestamp of the start of the current block */ int fd; /* --ifile option file descriptor. */ @@ -328,10 +328,12 @@ void modesInit(void) { * We scale to 0-255 range multiplying by 1.4 in order to ensure that * every different I/Q pair will result in a different magnitude value, * not losing any resolution. */ - Modes.maglut = malloc(129*129*2); - for (i = 0; i <= 128; i++) { - for (q = 0; q <= 128; q++) { - Modes.maglut[i*129+q] = round(sqrt(i*i+q*q)*360); + Modes.maglut = malloc(256*256*2); + for (i = 0; i <= 255; i++) { + int mag_i = i-127; + for (q = 0; q <= 255; q++) { + int mag_q = q - 127; + Modes.maglut[i*256+q] = round(sqrt((mag_i*mag_i)+(mag_q*mag_q))*360); } } @@ -1236,7 +1238,7 @@ void displayModesMessage(struct modesMessage *mm) { * pointed by Modes.magnitude. */ void computeMagnitudeVector(void) { uint16_t *m = &Modes.magnitude[MODES_PREAMBLE_SAMPLES+MODES_LONG_MSG_SAMPLES]; - unsigned char *p = Modes.data; + uint16_t *p = Modes.data; uint32_t j; memcpy(Modes.magnitude,&Modes.magnitude[MODES_ASYNC_BUF_SAMPLES], MODES_PREAMBLE_SIZE+MODES_LONG_MSG_SIZE); @@ -1244,12 +1246,7 @@ void computeMagnitudeVector(void) { /* Compute the magnitudo vector. It's just SQRT(I^2 + Q^2), but * we rescale to the 0-255 range to exploit the full resolution. */ for (j = 0; j < MODES_ASYNC_BUF_SAMPLES; j ++) { - int i = (*p++)-127; - int q = (*p++)-127; - - if (i < 0) i = -i; - if (q < 0) q = -q; - *m++ = Modes.maglut[i*129+q]; + *m++ = Modes.maglut[*p++]; } } @@ -2538,10 +2535,10 @@ int main(int argc, char **argv) { showHelp(); exit(0); } else if (!strcmp(argv[j],"--ppm") && more) { - Modes.ppm_error = atoi(argv[++j]); - } else if (!strcmp(argv[j],"--quiet")) { - Modes.quiet = 1; - } else { + Modes.ppm_error = atoi(argv[++j]); + } else if (!strcmp(argv[j],"--quiet")) { + Modes.quiet = 1; + } else { fprintf(stderr, "Unknown or not enough arguments for option '%s'.\n\n", argv[j]); From 8ba18ebcdb2dfdcee482ece28361075caad91c28 Mon Sep 17 00:00:00 2001 From: Malcolm Robb Date: Mon, 8 Apr 2013 18:25:11 +0100 Subject: [PATCH 2/4] Type Conversion required when using --file --- dump1090.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dump1090.c b/dump1090.c index c962816..323de38 100644 --- a/dump1090.c +++ b/dump1090.c @@ -445,7 +445,7 @@ void readDataFromFile(void) { } toread = MODES_ASYNC_BUF_SIZE; - p = Modes.data; + p = (unsigned char *) Modes.data; while(toread) { nread = read(Modes.fd, p, toread); if (nread <= 0) { From ff34693d1ec8cf58525ca7b18816cd4f3955cc14 Mon Sep 17 00:00:00 2001 From: Malcolm Robb Date: Tue, 9 Apr 2013 23:56:20 +0100 Subject: [PATCH 3/4] Make Things more Windows friendly Change the following so that M$ compilers and debuggers complain less 1) change all long long data types to uint64_t. 2) Typecast all malloc function returns to the correct pointer types. 3) Explicitly typecast all float to int conversions. 4) Remove inline variable declaration. Allowed in C++, but not in C. --- dump1090.c | 130 +++++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 63 deletions(-) diff --git a/dump1090.c b/dump1090.c index e4becca..abfaabc 100644 --- a/dump1090.c +++ b/dump1090.c @@ -27,22 +27,26 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "rtl-sdr.h" -#include "anet.h" +#ifndef _WIN32 + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include "rtl-sdr.h" + #include "anet.h" +#else + #include "dump1090.h" //Put everything Windows specific in here + #include "rtl-sdr.h" +#endif #define MODES_DEFAULT_RATE 2000000 #define MODES_DEFAULT_FREQ 1090000000 @@ -122,7 +126,7 @@ struct aircraft { int even_cprlat; int even_cprlon; double lat, lon; /* Coordinated obtained from CPR encoded data. */ - long long odd_cprtime, even_cprtime; + uint64_t odd_cprtime, even_cprtime; int squawk; struct aircraft *next; /* Next aircraft in our linked list. */ }; @@ -135,7 +139,7 @@ struct { pthread_cond_t data_cond; /* Conditional variable associated. */ uint16_t *data; /* Raw IQ samples buffer */ uint16_t *magnitude; /* Magnitude vector */ - long long timestampBlk; /* Timestamp of the start of the current block */ + uint64_t timestampBlk; /* Timestamp of the start of the current block */ int fd; /* --ifile option file descriptor. */ int data_ready; /* Data ready to be processed. */ uint32_t *icao_cache; /* Recently seen ICAO addresses cache. */ @@ -183,19 +187,19 @@ struct { /* Interactive mode */ struct aircraft *aircrafts; - long long interactive_last_update; /* Last screen update in milliseconds */ + uint64_t interactive_last_update; /* Last screen update in milliseconds */ /* Statistics */ - long long stat_valid_preamble; - long long stat_demodulated; - long long stat_goodcrc; - long long stat_badcrc; - long long stat_fixed; - long long stat_single_bit_fix; - long long stat_two_bits_fix; - long long stat_http_requests; - long long stat_sbs_connections; - long long stat_out_of_phase; + uint64_t stat_valid_preamble; + uint64_t stat_demodulated; + uint64_t stat_goodcrc; + uint64_t stat_badcrc; + uint64_t stat_fixed; + uint64_t stat_single_bit_fix; + uint64_t stat_two_bits_fix; + uint64_t stat_http_requests; + uint64_t stat_sbs_connections; + uint64_t stat_out_of_phase; } Modes; /* The struct we use to store information about a decoded message. */ @@ -209,7 +213,7 @@ struct modesMessage { int errorbit; /* Bit corrected. -1 if no bit corrected. */ int aa1, aa2, aa3; /* ICAO Address bytes 1 2 and 3 */ int phase_corrected; /* True if phase correction was applied. */ - long long timestampMsg; /* Timestamp of the message. */ + uint64_t timestampMsg; /* Timestamp of the message. */ unsigned char signalLevel; /* Signal Amplitude */ /* DF 11 */ @@ -257,12 +261,12 @@ int modesMessageLenByType(int type); /* ============================= Utility functions ========================== */ -static long long mstime(void) { +static uint64_t mstime(void) { struct timeval tv; - long long mst; + uint64_t mst; gettimeofday(&tv, NULL); - mst = ((long long)tv.tv_sec)*1000; + mst = ((uint64_t)tv.tv_sec)*1000; mst += tv.tv_usec/1000; return mst; } @@ -309,12 +313,12 @@ void modesInit(void) { Modes.timestampBlk = 0; /* Allocate the ICAO address cache. We use two uint32_t for every * entry because it's a addr / timestamp pair for every entry. */ - Modes.icao_cache = malloc(sizeof(uint32_t)*MODES_ICAO_CACHE_LEN*2); + Modes.icao_cache = (uint32_t *) malloc(sizeof(uint32_t)*MODES_ICAO_CACHE_LEN*2); memset(Modes.icao_cache,0,sizeof(uint32_t)*MODES_ICAO_CACHE_LEN*2); Modes.aircrafts = NULL; Modes.interactive_last_update = 0; - if ((Modes.data = malloc(MODES_ASYNC_BUF_SIZE)) == NULL || - (Modes.magnitude = malloc(MODES_ASYNC_BUF_SIZE+MODES_PREAMBLE_SIZE+MODES_LONG_MSG_SIZE)) == NULL) { + if ((Modes.data = (uint16_t *) malloc(MODES_ASYNC_BUF_SIZE)) == NULL || + (Modes.magnitude = (uint16_t *) malloc(MODES_ASYNC_BUF_SIZE+MODES_PREAMBLE_SIZE+MODES_LONG_MSG_SIZE)) == NULL) { fprintf(stderr, "Out of memory allocating data buffer.\n"); exit(1); } @@ -870,8 +874,8 @@ int decodeAC12Field(unsigned char *msg, int *unit) { if (q_bit) { /* N is the 11 bit integer resulting from the removal of bit * Q */ - *unit = MODES_UNIT_FEET; int n = ((msg[5]>>1)<<4) | ((msg[6]&0xF0) >> 4); + *unit = MODES_UNIT_FEET; /* The final altitude is due to the resulting number multiplied * by 25, minus 1000. */ return n*25-1000; @@ -1091,8 +1095,8 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) { mm->vert_rate = ((msg[8]&7) << 6) | ((msg[9]&0xfc) >> 2); /* Compute velocity and angle from the two speed * components. */ - mm->velocity = sqrt(mm->ns_velocity*mm->ns_velocity+ - mm->ew_velocity*mm->ew_velocity); + mm->velocity = (int) sqrt(mm->ns_velocity*mm->ns_velocity+ + mm->ew_velocity*mm->ew_velocity); if (mm->velocity) { int ewv = mm->ew_velocity; int nsv = mm->ns_velocity; @@ -1103,7 +1107,7 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) { heading = atan2(ewv,nsv); /* Convert to degrees. */ - mm->heading = heading * 360 / (M_PI*2); + mm->heading = (int) (heading * 360 / (M_PI*2)); /* We don't want negative values but a 0-360 scale. */ if (mm->heading < 0) mm->heading += 360; } else { @@ -1111,8 +1115,8 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) { } } else if (mm->mesub == 3 || mm->mesub == 4) { mm->heading_is_valid = msg[5] & (1<<2); - mm->heading = (360.0/128) * (((msg[5] & 3) << 5) | - (msg[6] >> 3)); + mm->heading = (int) (360.0/128) * (((msg[5] & 3) << 5) | + (msg[6] >> 3)); } } } @@ -1343,6 +1347,7 @@ void detectModeS(uint16_t *m, uint32_t mlen) { for (j = 0; j < mlen; j++) { int low, high, delta, i, errors; int good_message = 0; + int msglen; if (use_correction) goto good_preamble; /* We already checked it. */ @@ -1454,8 +1459,7 @@ good_preamble: bits[i+7]; } - int msgtype = msg[0]>>3; - int msglen = modesMessageLenByType(msgtype)/8; + msglen = modesMessageLenByType(msg[0] >> 3) / 8; /* Last check, high and low bits are different enough in magnitude * to mark this as real message and not just noise? */ @@ -1577,7 +1581,7 @@ void useModesMessage(struct modesMessage *mm) { /* Return a new aircraft structure for the interactive mode linked list * of aircrafts. */ struct aircraft *interactiveCreateAircraft(uint32_t addr) { - struct aircraft *a = malloc(sizeof(*a)); + struct aircraft *a = (struct aircraft *) malloc(sizeof(*a)); a->addr = addr; snprintf(a->hexaddr,sizeof(a->hexaddr),"%06x",(int)addr); @@ -1711,7 +1715,7 @@ void decodeCPR(struct aircraft *a) { double lon1 = a->odd_cprlon; /* Compute the Latitude Index "j" */ - int j = floor(((59*lat0 - 60*lat1) / 131072) + 0.5); + int j = (int) floor(((59*lat0 - 60*lat1) / 131072) + 0.5); double rlat0 = AirDlat0 * (cprModFunction(j,60) + lat0 / 131072); double rlat1 = AirDlat1 * (cprModFunction(j,59) + lat1 / 131072); @@ -1725,15 +1729,15 @@ void decodeCPR(struct aircraft *a) { if (a->even_cprtime > a->odd_cprtime) { /* Use even packet. */ int ni = cprNFunction(rlat0,0); - int m = floor((((lon0 * (cprNLFunction(rlat0)-1)) - - (lon1 * cprNLFunction(rlat0))) / 131072) + 0.5); + int m = (int) floor((((lon0 * (cprNLFunction(rlat0)-1)) - + (lon1 * cprNLFunction(rlat0))) / 131072) + 0.5); a->lon = cprDlonFunction(rlat0,0) * (cprModFunction(m,ni)+lon0/131072); a->lat = rlat0; } else { /* Use odd packet. */ int ni = cprNFunction(rlat1,1); - int m = floor((((lon0 * (cprNLFunction(rlat1)-1)) - - (lon1 * cprNLFunction(rlat1))) / 131072.0) + 0.5); + int m = (int) floor((((lon0 * (cprNLFunction(rlat1)-1)) - + (lon1 * cprNLFunction(rlat1))) / 131072.0) + 0.5); a->lon = cprDlonFunction(rlat1,1) * (cprModFunction(m,ni)+lon1/131072); a->lat = rlat1; } @@ -1796,7 +1800,7 @@ struct aircraft *interactiveReceiveData(struct modesMessage *mm) { } /* If the two data is less than 10 seconds apart, compute * the position. */ - if (abs(a->even_cprtime - a->odd_cprtime) <= 10000) { + if (abs((int)(a->even_cprtime - a->odd_cprtime)) <= 10000) { decodeCPR(a); } } else if (mm->metype == 19) { @@ -1828,11 +1832,12 @@ void interactiveShowData(void) { while(a && count < Modes.interactive_rows) { int altitude = a->altitude, speed = a->speed, msgs = a->messages; char squawk[5] = "0"; + char spacer = '\0'; /* Convert units to metric if --metric was specified. */ if (Modes.metric) { - altitude /= 3.2828; - speed *= 1.852; + altitude = (int) (altitude / 3.2828); + speed = (int) (speed * 1.852); } if (altitude > 99999) { @@ -1849,7 +1854,6 @@ void interactiveShowData(void) { msgs = 99999; } - char spacer = '\0'; if ((int)(now - a->seen) < 10) { spacer = ' '; } @@ -1893,7 +1897,7 @@ void interactiveRemoveStaleAircrafts(void) { * for more than 256 samples in order to reduce example file size. */ void snipMode(int level) { int i, q; - long long c = 0; + uint64_t c = 0; while ((i = getchar()) != EOF && (q = getchar()) != EOF) { if (abs(i-127) < level && abs(q-127) < level) { @@ -1974,7 +1978,7 @@ void modesAcceptClients(void) { } anetNonBlock(Modes.aneterr, fd); - c = malloc(sizeof(*c)); + c = (struct client *) malloc(sizeof(*c)); c->service = services[j]; c->fd = fd; c->buflen = 0; @@ -2183,7 +2187,7 @@ int decodeHexMessage(struct client *c) { char *aircraftsToJson(int *len) { struct aircraft *a = Modes.aircrafts; int buflen = 1024; /* The initial buffer is incremented as needed. */ - char *buf = malloc(buflen), *p = buf; + char *buf = (char *) malloc(buflen), *p = buf; int l; l = snprintf(p,buflen,"[\n"); @@ -2193,8 +2197,8 @@ char *aircraftsToJson(int *len) { /* Convert units to metric if --metric was specified. */ if (Modes.metric) { - altitude /= 3.2828; - speed *= 1.852; + altitude = (int) (altitude / 3.2828); + speed = (int) (speed * 1.852); } if (a->lat != 0 && a->lon != 0) { @@ -2209,7 +2213,7 @@ char *aircraftsToJson(int *len) { if (buflen < 256) { int used = p-buf; buflen += 1024; /* Our increment. */ - buf = realloc(buf,used+buflen); + buf = (char *) realloc(buf,used+buflen); p = buf+used; } } @@ -2283,7 +2287,7 @@ int handleHTTPRequest(struct client *c) { if (stat("gmap.html",&sbuf) != -1 && (fd = open("gmap.html",O_RDONLY)) != -1) { - content = malloc(sbuf.st_size); + content = (char *) malloc(sbuf.st_size); if (read(fd,content,sbuf.st_size) == -1) { snprintf(content,sbuf.st_size,"Error reading from file: %s", strerror(errno)); @@ -2486,11 +2490,11 @@ int main(int argc, char **argv) { if (!strcmp(argv[j],"--device-index") && more) { Modes.dev_index = atoi(argv[++j]); } else if (!strcmp(argv[j],"--gain") && more) { - Modes.gain = atof(argv[++j])*10; /* Gain is in tens of DBs */ + Modes.gain = (int) atof(argv[++j])*10; /* Gain is in tens of DBs */ } else if (!strcmp(argv[j],"--enable-agc")) { Modes.enable_agc++; } else if (!strcmp(argv[j],"--freq") && more) { - Modes.freq = strtoll(argv[++j],NULL,10); + Modes.freq = (int) strtoll(argv[++j],NULL,10); } else if (!strcmp(argv[j],"--ifile") && more) { Modes.filename = strdup(argv[++j]); } else if (!strcmp(argv[j],"--no-fix")) { From 70031e12343d5196df176c58bab4712307f2ba2b Mon Sep 17 00:00:00 2001 From: Malcolm Robb Date: Wed, 10 Apr 2013 00:48:29 +0100 Subject: [PATCH 4/4] Create a preamble pointer in the message detector loop Create a pointer, pPreamble, which points to the start of the preamble in the analogue sample buffer m[]. So pPreamble = &m[p] Then use this pointer to perform the preamble detection tests. It should save a few cpu cycles per test because accessing pPointer[2] should be quicker than m[p+2]. Also move the decision on whether to try OutOfPhase correction to the end of the first pass, rather than automatically going into phase correction if the first pass fails. This saves two memcpy's if the decision in the second pass is to not do phase correction. --- dump1090.c | 76 +++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/dump1090.c b/dump1090.c index abfaabc..4955410 100644 --- a/dump1090.c +++ b/dump1090.c @@ -1258,13 +1258,13 @@ void computeMagnitudeVector(void) { * Return 1 if the message is out of fase right-size * Return 0 if the message is not particularly out of phase. * - * Note: this function will access m[-1], so the caller should make sure to + * Note: this function will access pPreamble[-1], so the caller should make sure to * call it only if we are not at the start of the current buffer. */ -int detectOutOfPhase(uint16_t *m) { - if (m[3] > m[2]/3) return 1; - if (m[10] > m[9]/3) return 1; - if (m[6] > m[7]/3) return -1; - if (m[-1] > m[1]/3) return -1; +int detectOutOfPhase(uint16_t *pPreamble) { + if (pPreamble[ 3] > pPreamble[2]/3) return 1; + if (pPreamble[10] > pPreamble[9]/3) return 1; + if (pPreamble[ 6] > pPreamble[7]/3) return -1; + if (pPreamble[-1] > pPreamble[1]/3) return -1; return 0; } @@ -1298,8 +1298,6 @@ int detectOutOfPhase(uint16_t *m) { * correct way. */ void applyPhaseCorrection(uint16_t *m) { int j; - - m += MODES_PREAMBLE_SAMPLES; /* Skip preamble. */ for (j = 0; j < MODES_LONG_MSG_SAMPLES; j += 2) { if (m[j] > m[j+1]) { /* One */ @@ -1347,28 +1345,29 @@ void detectModeS(uint16_t *m, uint32_t mlen) { for (j = 0; j < mlen; j++) { int low, high, delta, i, errors; int good_message = 0; + uint16_t *pPreamble; int msglen; + pPreamble = &m[j]; if (use_correction) goto good_preamble; /* We already checked it. */ /* 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 (!(m[j] > m[j+1] && - m[j+1] < m[j+2] && - m[j+2] > m[j+3] && - m[j+3] < m[j] && - m[j+4] < m[j] && - m[j+5] < m[j] && - m[j+6] < m[j] && - m[j+7] > m[j+8] && - m[j+8] < m[j+9] && - m[j+9] > m[j+6])) + if (!(pPreamble[0] > pPreamble[1] && + pPreamble[1] < pPreamble[2] && + pPreamble[2] > pPreamble[3] && + pPreamble[3] < pPreamble[0] && + pPreamble[4] < pPreamble[0] && + pPreamble[5] < pPreamble[0] && + pPreamble[6] < pPreamble[0] && + pPreamble[7] > pPreamble[8] && + pPreamble[8] < pPreamble[9] && + pPreamble[9] > pPreamble[6])) { if (Modes.debug & MODES_DEBUG_NOPREAMBLE && - m[j] > MODES_DEBUG_NOPREAMBLE_LEVEL) - dumpRawMessage("Unexpected ratio among first 10 samples", - msg, m, j); + *pPreamble > MODES_DEBUG_NOPREAMBLE_LEVEL) + dumpRawMessage("Unexpected ratio among first 10 samples", msg, m, j); continue; } @@ -1376,31 +1375,27 @@ void detectModeS(uint16_t *m, uint32_t mlen) { * 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. */ - high = (m[j]+m[j+2]+m[j+7]+m[j+9])/6; - if (m[j+4] >= high || - m[j+5] >= high) + high = (pPreamble[0]+pPreamble[2]+pPreamble[7]+pPreamble[9])/6; + if (pPreamble[4] >= high || + pPreamble[5] >= high) { if (Modes.debug & MODES_DEBUG_NOPREAMBLE && - m[j] > MODES_DEBUG_NOPREAMBLE_LEVEL) - dumpRawMessage( - "Too high level in samples between 3 and 6", - msg, m, j); + *pPreamble > MODES_DEBUG_NOPREAMBLE_LEVEL) + dumpRawMessage("Too high level in samples between 3 and 6", msg, m, j); continue; } /* 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 (m[j+11] >= high || - m[j+12] >= high || - m[j+13] >= high || - m[j+14] >= high) + if (pPreamble[11] >= high || + pPreamble[12] >= high || + pPreamble[13] >= high || + pPreamble[14] >= high) { if (Modes.debug & MODES_DEBUG_NOPREAMBLE && - m[j] > MODES_DEBUG_NOPREAMBLE_LEVEL) - dumpRawMessage( - "Too high level in samples between 10 and 15", - msg, m, j); + *pPreamble > MODES_DEBUG_NOPREAMBLE_LEVEL) + dumpRawMessage("Too high level in samples between 10 and 15", msg, m, j); continue; } Modes.stat_valid_preamble++; @@ -1410,10 +1405,9 @@ good_preamble: * magnitude correction. */ if (use_correction) { memcpy(aux,m+j+MODES_PREAMBLE_SAMPLES,sizeof(aux)); - if (j && detectOutOfPhase(m+j)) { - applyPhaseCorrection(m+j); - Modes.stat_out_of_phase++; - } + applyPhaseCorrection(m+j+MODES_PREAMBLE_SAMPLES); + Modes.stat_out_of_phase++; + /* TODO ... apply other kind of corrections. */ } @@ -1537,7 +1531,7 @@ good_preamble: } /* Retry with phase correction if possible. */ - if (!good_message && !use_correction) { + if (!good_message && !use_correction && j && detectOutOfPhase(pPreamble)) { j--; use_correction = 1; } else {