From 6f948be84258a16ed85809526d72a875123b6229 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 30 Oct 2017 16:44:04 +0100 Subject: [PATCH] CHG: some average / mean functions to enhance justNoise functions. FIX: 'lf t55xx' - verifying that collected signal is not just noise.. --- client/cmdlft55xx.c | 40 ++++++++++++++------------ client/graph.c | 39 +++++++++++++------------- client/graph.h | 8 +++++- common/lfdemod.c | 68 ++++++++++++++++++++++++++++++++++----------- common/lfdemod.h | 5 +++- 5 files changed, 105 insertions(+), 55 deletions(-) diff --git a/client/cmdlft55xx.c b/client/cmdlft55xx.c index a9c7d3793..460386ee9 100644 --- a/client/cmdlft55xx.c +++ b/client/cmdlft55xx.c @@ -1315,6 +1315,10 @@ int AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){ return 0; } setGraphBuf(got, sizeof(got)); + + if (is_justnoise(GraphBuffer, sizeof(got))) + return 0; + return 1; } @@ -1755,12 +1759,12 @@ bool tryDetectP1(bool getData) { ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge); if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) { if ( FSKrawDemod("0 0", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && + preambleSearchEx(DemodBuffer, preamble,sizeof(preamble), &DemodBufferLen, &startIdx, false) && (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } if ( FSKrawDemod("0 1", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } @@ -1769,19 +1773,19 @@ bool tryDetectP1(bool getData) { // try psk clock detect. if successful it cannot be any other type of modulation... (in theory...) clk = GetPskClock("", false, false); - if (clk>0) { + if (clk > 0) { // allow undo // save_restoreGB(1); // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) //CmdLtrim("160"); if ( PSKDemod("0 0 6", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && (DemodBufferLen == 32 || DemodBufferLen == 64) ) { //save_restoreGB(0); return true; } if ( PSKDemod("0 1 6", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && (DemodBufferLen == 32 || DemodBufferLen == 64) ) { //save_restoreGB(0); return true; @@ -1789,7 +1793,7 @@ bool tryDetectP1(bool getData) { // PSK2 - needs a call to psk1TOpsk2. if ( PSKDemod("0 0 6", false)) { psk1TOpsk2(DemodBuffer, DemodBufferLen); - if (preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && + if (preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && (DemodBufferLen == 32 || DemodBufferLen == 64) ) { //save_restoreGB(0); return true; @@ -1805,39 +1809,39 @@ bool tryDetectP1(bool getData) { clk = GetAskClock("", false, false); if (clk>0) { if ( ASKDemod_ext("0 0 1", false, false, 1, &st) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } st = true; if ( ASKDemod_ext("0 1 1", false, false, 1, &st) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && - (DemodBufferLen == 32 || DemodBufferLen == 64) ) { + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && + (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } if ( ASKbiphaseDemod("0 0 0 2", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && - (DemodBufferLen == 32 || DemodBufferLen == 64) ) { + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && + (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } if ( ASKbiphaseDemod("0 0 1 2", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && - (DemodBufferLen == 32 || DemodBufferLen == 64) ) { + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && + (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } } // try NRZ clock detect. it could be another type even if successful. clk = GetNrzClock("", false, false); //has the most false positives :( - if (clk>0) { + if (clk > 0) { if ( NRZrawDemod("0 0 1", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && - (DemodBufferLen == 32 || DemodBufferLen == 64) ) { + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && + (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } if ( NRZrawDemod("0 1 1", false) && - preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && - (DemodBufferLen == 32 || DemodBufferLen == 64) ) { + preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) && + (DemodBufferLen == 32 || DemodBufferLen == 64) ) { return true; } } diff --git a/client/graph.c b/client/graph.c index 8bccb7aff..457e4ae14 100644 --- a/client/graph.c +++ b/client/graph.c @@ -7,14 +7,7 @@ //----------------------------------------------------------------------------- // Graph utilities //----------------------------------------------------------------------------- - -#include -#include -#include -#include "ui.h" #include "graph.h" -#include "lfdemod.h" -#include "cmddata.h" //for g_debugmode int GraphBuffer[MAX_GRAPH_TRACE_LEN]; int GraphTraceLen; @@ -81,13 +74,12 @@ void setGraphBuf(uint8_t *buf, size_t size) { return; } size_t getFromGraphBuf(uint8_t *buf) { - if (buf == NULL ) return 0; uint32_t i; for (i=0; i < GraphTraceLen; ++i){ if (GraphBuffer[i] > 127) GraphBuffer[i] = 127; //trim if (GraphBuffer[i] < -127) GraphBuffer[i] = -127; //trim - buf[i] = (uint8_t)(GraphBuffer[i]+128); + buf[i] = (uint8_t)(GraphBuffer[i] + 128); } return i; } @@ -110,17 +102,19 @@ void DetectHighLowInGraph(int *high, int *low, bool addFuzz) { if ( loopMax > GraphTraceLen) loopMax = GraphTraceLen; + *high = -255; *low = 255; + for (uint8_t i = 0; i < loopMax; ++i) { - if (GraphBuffer[i] > *high) - *high = GraphBuffer[i]; - else if (GraphBuffer[i] < *low) - *low = GraphBuffer[i]; + if (GraphBuffer[i] > *high) *high = GraphBuffer[i]; + if (GraphBuffer[i] < *low) *low = GraphBuffer[i]; } //12% fuzz in case highs and lows aren't clipped if (addFuzz) { - *high = (int)(*high * .88); - *low = (int)(*low * .88); + *high *= .88; + *low *= .88; + //*high = (int)(*high * .88); + //*low = (int)(*low * .88); } } @@ -272,12 +266,19 @@ uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *f } // test samples are not just noise -bool is_justnoise(int *bits, int size) { +bool is_justnoise(int *bits, uint32_t size) { //might not be high enough for noisy environments - #define THRESHOLD 15; + #define NOICE_THRESHOLD 15; + + // if we take the mean on the sample set and see if its + // below the threshold, it will be a better indicator + int32_t mean = compute_mean_int(bits, size); + return mean < NOICE_THRESHOLD; + + /* bool isNoise = true; - for(int i=0; i < size && isNoise; i++){ + for(int i=0; i < size && isNoise; i++) isNoise = bits[i] < THRESHOLD; - } return isNoise; + */ } diff --git a/client/graph.h b/client/graph.h index d4043a68a..04d32674c 100644 --- a/client/graph.h +++ b/client/graph.h @@ -10,7 +10,13 @@ #ifndef GRAPH_H__ #define GRAPH_H__ +#include #include +#include +#include +#include "ui.h" +#include "lfdemod.h" +#include "cmddata.h" //for g_debugmode void AppendGraph(int redraw, int clock, int bit); int ClearGraph(int redraw); @@ -21,7 +27,7 @@ uint8_t GetPskCarrier(const char str[], bool printAns, bool verbose); uint8_t GetNrzClock(const char str[], bool printAns, bool verbose); uint8_t GetFskClock(const char str[], bool printAns, bool verbose); uint8_t fskClocks(uint8_t *fc1, uint8_t *fc2, uint8_t *rf1, bool verbose, int *firstClockEdge); -bool is_justnoise(int *bits, int size); +bool is_justnoise(int *bits, uint32_t size); void setGraphBuf(uint8_t *buff, size_t size); void save_restoreGB(uint8_t saveOpt); diff --git a/common/lfdemod.c b/common/lfdemod.c index 70b321fe1..5dedc73ed 100644 --- a/common/lfdemod.c +++ b/common/lfdemod.c @@ -60,26 +60,62 @@ void dummy(char *fmt, ...){} # define prnt dummy #endif -//test samples are not just noise -uint8_t justNoise(uint8_t *bits, size_t size) { +// Function to compute mean for a series +// rounded to integer.. +uint32_t compute_mean_uint(uint8_t *in, size_t N) { + uint32_t mean = 0; + for (size_t i = 0; i < N; i++) + mean += in[i]; + return (uint32_t)mean/N; +} +// Function to compute mean for a series +// rounded to integer.. +int32_t compute_mean_int(int *in, size_t N) { + int32_t mean = 0; + for (size_t i = 0; i < N; i++) + mean += in[i]; + return (int32_t)mean/N; +} +//test samples are not just noise +bool justNoise(uint8_t *bits, size_t size) { + + // if we take the mean on the sample set and see if its + // below the threshold, it will be a better indicator + uint32_t mean = compute_mean_uint(bits, size); + uint8_t counter = 0; + for ( size_t i = 0; i < size && counter < 10; i++) { + if ( bits[i] > mean ) counter++; + } + if (g_debugMode == 2) prnt("DEBUG: (justNoise) mean %u | %i | counter %u", mean, FSK_PSK_THRESHOLD, counter); + return counter < 10; + + /* + // loop until a sample is larger than threshold. + // one sample above threshold is not a good indicator. uint8_t val = 1; for(size_t idx = 0; idx < size && val; idx++) val = bits[idx] < FSK_PSK_THRESHOLD; return val; + */ } //by marshmellow //get high and low values of a wave with passed in fuzz factor. also return noise test = 1 for passed or 0 for only noise -int getHiLo(uint8_t *BitStream, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) { - *high=0; - *low=255; +int getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) { + + *high = 0; *low = 255; + // get high and low thresholds for (size_t i=0; i < size; i++){ - if (BitStream[i] > *high) *high = BitStream[i]; - if (BitStream[i] < *low) *low = BitStream[i]; + if (bits[i] > *high) *high = bits[i]; + if (bits[i] < *low) *low = bits[i]; } - if (*high < FSK_PSK_THRESHOLD) return -1; // just noise + + // just noise - no super good detection. good enough + if (*high < FSK_PSK_THRESHOLD) return -1; + + // add fuzz. *high = ((*high-128)*fuzzHi + 12800)/100; *low = ((*low-128)*fuzzLo + 12800)/100; return 1; @@ -203,18 +239,18 @@ bool preambleSearchEx(uint8_t *BitStream, uint8_t *preamble, size_t pLen, size_t } // find start of modulating data (for fsk and psk) in case of beginning noise or slow chip startup. -size_t findModStart(uint8_t dest[], size_t size, uint8_t expWaveSize) { +size_t findModStart(uint8_t *src, size_t size, uint8_t expWaveSize) { size_t i = 0; size_t waveSizeCnt = 0; uint8_t thresholdCnt = 0; - bool isAboveThreshold = dest[i++] >= FSK_PSK_THRESHOLD; + bool isAboveThreshold = src[i++] >= FSK_PSK_THRESHOLD; for (; i < size-20; i++ ) { - if(dest[i] < FSK_PSK_THRESHOLD && isAboveThreshold) { + if(src[i] < FSK_PSK_THRESHOLD && isAboveThreshold) { thresholdCnt++; if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break; isAboveThreshold = false; waveSizeCnt = 0; - } else if (dest[i] >= FSK_PSK_THRESHOLD && !isAboveThreshold) { + } else if (src[i] >= FSK_PSK_THRESHOLD && !isAboveThreshold) { thresholdCnt++; if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break; isAboveThreshold = true; @@ -224,7 +260,7 @@ size_t findModStart(uint8_t dest[], size_t size, uint8_t expWaveSize) { } if (thresholdCnt > 10) break; } - if (g_debugMode == 2) prnt("DEBUG: threshold Count reached at %u, count: %u",i, thresholdCnt); + if (g_debugMode == 2) prnt("DEBUG: threshold Count reached at %u, count: %u", i, thresholdCnt); return i; } @@ -1441,7 +1477,7 @@ size_t fsk_wave_demod(uint8_t * dest, size_t size, uint8_t fchigh, uint8_t fclow size_t last_transition = 0; size_t idx = 1; - //find start of modulating data in trace + //find start of modulating data in trace idx = findModStart(dest, size, fchigh); // Need to threshold first sample if(dest[idx] < FSK_PSK_THRESHOLD) dest[0] = 0; @@ -1769,8 +1805,8 @@ int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32 if (dest[idx] == dest[idx+1]){ return -5; //not manchester data } - *hi2 = (*hi2<<1)|(*hi>>31); - *hi = (*hi<<1)|(*lo>>31); + *hi2 = (*hi2 << 1) | (*hi >> 31); + *hi = (*hi << 1) | (*lo >> 31); //Then, shift in a 0 or one into low *lo <<= 1; if (dest[idx] && !dest[idx+1]) // 1 0 diff --git a/common/lfdemod.h b/common/lfdemod.h index a9f36a91f..00398f3b4 100644 --- a/common/lfdemod.h +++ b/common/lfdemod.h @@ -21,7 +21,10 @@ #include "parity.h" // for parity test //generic -extern uint8_t justNoise(uint8_t *bits, size_t size); +extern uint32_t compute_mean_uint(uint8_t *in, size_t N); +extern int32_t compute_mean_int(int *in, size_t N); + +extern bool justNoise(uint8_t *bits, size_t size); extern size_t addParity(uint8_t *BitSource, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType); extern int askdemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType); extern int askdemod_ext(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx);