improved the autocorreleate detection. lf search -1uc got some more textual improvements

This commit is contained in:
iceman1001 2023-12-18 10:04:19 +01:00
commit 7fa09a556a
2 changed files with 72 additions and 53 deletions

View file

@ -814,9 +814,13 @@ static int Cmdaskrawdemod(const char *Cmd) {
int AutoCorrelate(const int *in, int *out, size_t len, size_t window, bool SaveGrph, bool verbose) {
// sanity check
if (window > len) window = len;
if (window > len) {
window = len;
}
if (verbose) PrintAndLogEx(INFO, "performing " _YELLOW_("%zu") " correlations", g_GraphTraceLen - window);
if (verbose) {
PrintAndLogEx(INFO, "performing " _YELLOW_("%zu") " correlations", g_GraphTraceLen - window);
}
//test
double autocv = 0.0; // Autocovariance value
@ -830,6 +834,9 @@ int AutoCorrelate(const int *in, int *out, size_t len, size_t window, bool SaveG
int *correl_buf = calloc(MAX_GRAPH_TRACE_LEN, sizeof(int));
uint8_t peak_cnt = 0;
size_t peaks[10] = {0};
for (size_t i = 0; i < len - window; ++i) {
for (size_t j = 0; j < (len - i); j++) {
@ -844,44 +851,38 @@ int AutoCorrelate(const int *in, int *out, size_t len, size_t window, bool SaveG
double ac_value = autocv / variance;
// keep track of which distance is repeating.
if (ac_value > 1) {
// A value near 1.0 or more indicates a correlation in the signal
if (ac_value > 0.95) {
correlation = i - lastmax;
lastmax = i;
if ((correlation > 1) && peak_cnt < ARRAYLEN(peaks)) {
peaks[peak_cnt++] = correlation;
}
}
}
//
int hi = 0, idx = 0;
int distance = 0, hi_1 = 0, idx_1 = 0;
for (size_t i = 0; i <= len; ++i) {
if (correl_buf[i] > hi) {
hi = correl_buf[i];
idx = i;
// Find shorts distance between peaks
int distance = -1;
for (size_t i = 0; i < ARRAYLEN(peaks); ++i) {
if (peaks[i] < 2) {
continue;
}
if (distance == -1) {
distance = peaks[i];
continue;
}
if (peaks[i] < distance) {
distance = peaks[i];
}
}
for (size_t i = idx + 1; i <= window; ++i) {
if (correl_buf[i] > hi_1) {
hi_1 = correl_buf[i];
idx_1 = i;
}
}
int foo = ABS(hi - hi_1);
int bar = (int)((int)((hi + hi_1) / 2) * 0.04);
int retval = correlation;
if (foo < bar) {
distance = (idx_1 - idx);
retval = distance;
if (distance > -1) {
if (verbose) {
PrintAndLogEx(SUCCESS, "possible visible correlation "_YELLOW_("%4d") " samples", distance);
}
} else if (correlation > 1) {
if (verbose) {
PrintAndLogEx(SUCCESS, "possible correlation " _YELLOW_("%4zu") " samples", correlation);
PrintAndLogEx(SUCCESS, "possible correlation at "_YELLOW_("%4d") " samples", distance);
}
} else {
PrintAndLogEx(HINT, "no repeating pattern found, try increasing window size");
@ -892,20 +893,12 @@ int AutoCorrelate(const int *in, int *out, size_t len, size_t window, bool SaveG
if (SaveGrph) {
//g_GraphTraceLen = g_GraphTraceLen - window;
memcpy(out, correl_buf, len * sizeof(int));
if (distance > 0) {
setClockGrid(distance, idx);
retval = distance;
} else {
setClockGrid(correlation, idx);
}
g_CursorCPos = idx_1;
g_CursorDPos = idx_1 + retval;
setClockGrid(distance, 0);
g_DemodBufferLen = 0;
RepaintGraphWindow();
}
free(correl_buf);
return retval;
return distance;
}
static int CmdAutoCorr(const char *Cmd) {
@ -1413,7 +1406,8 @@ int NRZrawDemod(int clk, int invert, int maxErr, bool verbose) {
if (errCnt > 0 && (verbose || g_debugMode)) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Errors during Demoding (shown as 7 in bit stream): %d", errCnt);
if (verbose || g_debugMode) {
PrintAndLogEx(SUCCESS, "NRZ demoded bitstream:");
PrintAndLogEx(SUCCESS, "NRZ demoded bitstream");
PrintAndLogEx(INFO, "---------------------");
// Now output the bitstream to the scrollback by line of 16 bits
printDemodBuff(0, false, invert, false);
}

View file

@ -1555,11 +1555,11 @@ out:
return retval;
}
static int check_autocorrelate(int clock) {
static int check_autocorrelate(const char *prefix, int clock) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, _CYAN_("Performing auto correlations..."));
for (int win = 4000; win < 30000; win += 2000) {
PrintAndLogEx(INFO, _CYAN_("%s - auto correlations"), prefix);
for (int win = 2000; win < 30000; win += 2000) {
int ans = AutoCorrelate(g_GraphBuffer, g_GraphBuffer, g_GraphTraceLen, win, false, false);
if (ans == -1) {
continue;
@ -1912,68 +1912,93 @@ int CmdLFfind(const char *Cmd) {
PrintAndLogEx(INFO, _CYAN_("Checking for unknown tags...") "\n");
// FSK
PrintAndLogEx(INFO, "FSK clock.......... " NOLF);
int clock = GetFskClock("", false);
if (clock) {
PrintAndLogEx(NORMAL, _GREEN_("detected"));
if (FSKrawDemod(0, 0, 0, 0, true) == PM3_SUCCESS) {
PrintAndLogEx(INFO, _GREEN_("FSK") " modulation detected!");
check_autocorrelate(clock);
check_autocorrelate("FSK", clock);
if (search_cont) {
found++;
} else {
goto out;
}
}
} else {
PrintAndLogEx(INFO, "FSK demodulation... " _RED_("failed"));
}
} else {
PrintAndLogEx(NORMAL, _RED_("no"));
}
// ASK
PrintAndLogEx(INFO, "ASK clock.......... " NOLF);
clock = GetAskClock("", false);
if (clock) {
PrintAndLogEx(NORMAL, _GREEN_("detected"));
bool st = true;
if (ASKDemod_ext(0, 0, 0, 0, false, true, false, 1, &st) == PM3_SUCCESS) {
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(INFO, _GREEN_("ASK") " modulation / Manchester encoding detected!");
PrintAndLogEx(INFO, "if it does not look right it could instead be ASK/Biphase - try " _YELLOW_("'data rawdemod --ab'"));
check_autocorrelate(clock);
check_autocorrelate("ASK", clock);
if (search_cont) {
found++;
} else {
goto out;
}
} else {
PrintAndLogEx(INFO, "ASK demodulation... " _RED_("failed"));
}
} else {
PrintAndLogEx(NORMAL, _RED_("no"));
}
// NZR
PrintAndLogEx(INFO, "NRZ clock.......... " NOLF);
clock = GetNrzClock("", false);
if (clock) {
if (NRZrawDemod(0, 0, 0,false) == PM3_SUCCESS) {
PrintAndLogEx(NORMAL, _GREEN_("detected"));
if (NRZrawDemod(0, 0, 0, true) == PM3_SUCCESS) {
PrintAndLogEx(INFO, _GREEN_("NRZ") " modulation detected!");
check_autocorrelate(clock);
check_autocorrelate("NRZ", clock);
if (search_cont) {
found++;
} else {
goto out;
}
}
} else {
PrintAndLogEx(INFO, "NRZ demodulation... " _RED_("failed"));
}
} else {
PrintAndLogEx(NORMAL, _RED_("no"));
}
// PSK
PrintAndLogEx(INFO, "PSK clock.......... " NOLF);
clock = GetPskClock("", false);
if (clock) {
if (clock) {
PrintAndLogEx(NORMAL, _GREEN_("detected"));
if (CmdPSK1rawDemod("") == PM3_SUCCESS) {
PrintAndLogEx(INFO, "Possible " _GREEN_("PSK1") " modulation detected!");
PrintAndLogEx(INFO, " Could also be PSK2 - try " _YELLOW_("'data rawdemod --p2'"));
PrintAndLogEx(INFO, " Could also be PSK3 - [currently not supported]");
PrintAndLogEx(INFO, " Could also be NRZ - try " _YELLOW_("'data rawdemod --nr"));
check_autocorrelate(clock);
check_autocorrelate("PSK", clock);
if (search_cont) {
found++;
} else {
goto out;
}
} else {
PrintAndLogEx(INFO, "PSK demodulation... " _RED_("failed"));
}
} else {
PrintAndLogEx(NORMAL, _RED_("no"));
}
if (found == 0) {