CHG: some average / mean functions to enhance justNoise functions.

FIX: 'lf t55xx' - verifying that collected signal is not just noise..
This commit is contained in:
iceman1001 2017-10-30 16:44:04 +01:00
commit 6f948be842
5 changed files with 105 additions and 55 deletions

View file

@ -1315,6 +1315,10 @@ int AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){
return 0; return 0;
} }
setGraphBuf(got, sizeof(got)); setGraphBuf(got, sizeof(got));
if (is_justnoise(GraphBuffer, sizeof(got)))
return 0;
return 1; return 1;
} }
@ -1755,12 +1759,12 @@ bool tryDetectP1(bool getData) {
ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge); ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, false, &firstClockEdge);
if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) { if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {
if ( FSKrawDemod("0 0", false) && 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) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; return true;
} }
if ( FSKrawDemod("0 1", false) && 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) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; 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...) // try psk clock detect. if successful it cannot be any other type of modulation... (in theory...)
clk = GetPskClock("", false, false); clk = GetPskClock("", false, false);
if (clk>0) { if (clk > 0) {
// allow undo // allow undo
// save_restoreGB(1); // save_restoreGB(1);
// skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise) // skip first 160 samples to allow antenna to settle in (psk gets inverted occasionally otherwise)
//CmdLtrim("160"); //CmdLtrim("160");
if ( PSKDemod("0 0 6", false) && 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) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
//save_restoreGB(0); //save_restoreGB(0);
return true; return true;
} }
if ( PSKDemod("0 1 6", false) && 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) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
//save_restoreGB(0); //save_restoreGB(0);
return true; return true;
@ -1789,7 +1793,7 @@ bool tryDetectP1(bool getData) {
// PSK2 - needs a call to psk1TOpsk2. // PSK2 - needs a call to psk1TOpsk2.
if ( PSKDemod("0 0 6", false)) { if ( PSKDemod("0 0 6", false)) {
psk1TOpsk2(DemodBuffer, DemodBufferLen); 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) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
//save_restoreGB(0); //save_restoreGB(0);
return true; return true;
@ -1805,23 +1809,23 @@ bool tryDetectP1(bool getData) {
clk = GetAskClock("", false, false); clk = GetAskClock("", false, false);
if (clk>0) { if (clk>0) {
if ( ASKDemod_ext("0 0 1", false, false, 1, &st) && 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) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; return true;
} }
st = true; st = true;
if ( ASKDemod_ext("0 1 1", false, false, 1, &st) && if ( ASKDemod_ext("0 1 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) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; return true;
} }
if ( ASKbiphaseDemod("0 0 0 2", false) && if ( ASKbiphaseDemod("0 0 0 2", false) &&
preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) &&
(DemodBufferLen == 32 || DemodBufferLen == 64) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; return true;
} }
if ( ASKbiphaseDemod("0 0 1 2", false) && if ( ASKbiphaseDemod("0 0 1 2", false) &&
preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) &&
(DemodBufferLen == 32 || DemodBufferLen == 64) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; return true;
} }
@ -1829,14 +1833,14 @@ bool tryDetectP1(bool getData) {
// try NRZ clock detect. it could be another type even if successful. // try NRZ clock detect. it could be another type even if successful.
clk = GetNrzClock("", false, false); //has the most false positives :( clk = GetNrzClock("", false, false); //has the most false positives :(
if (clk>0) { if (clk > 0) {
if ( NRZrawDemod("0 0 1", false) && if ( NRZrawDemod("0 0 1", false) &&
preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) &&
(DemodBufferLen == 32 || DemodBufferLen == 64) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; return true;
} }
if ( NRZrawDemod("0 1 1", false) && if ( NRZrawDemod("0 1 1", false) &&
preambleSearchEx(DemodBuffer,preamble,sizeof(preamble),&DemodBufferLen,&startIdx,false) && preambleSearchEx(DemodBuffer, preamble, sizeof(preamble), &DemodBufferLen, &startIdx, false) &&
(DemodBufferLen == 32 || DemodBufferLen == 64) ) { (DemodBufferLen == 32 || DemodBufferLen == 64) ) {
return true; return true;
} }

View file

@ -7,14 +7,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Graph utilities // Graph utilities
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "ui.h"
#include "graph.h" #include "graph.h"
#include "lfdemod.h"
#include "cmddata.h" //for g_debugmode
int GraphBuffer[MAX_GRAPH_TRACE_LEN]; int GraphBuffer[MAX_GRAPH_TRACE_LEN];
int GraphTraceLen; int GraphTraceLen;
@ -81,13 +74,12 @@ void setGraphBuf(uint8_t *buf, size_t size) {
return; return;
} }
size_t getFromGraphBuf(uint8_t *buf) { size_t getFromGraphBuf(uint8_t *buf) {
if (buf == NULL ) return 0; if (buf == NULL ) return 0;
uint32_t i; uint32_t i;
for (i=0; i < GraphTraceLen; ++i){ for (i=0; i < GraphTraceLen; ++i){
if (GraphBuffer[i] > 127) GraphBuffer[i] = 127; //trim if (GraphBuffer[i] > 127) GraphBuffer[i] = 127; //trim
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; return i;
} }
@ -110,17 +102,19 @@ void DetectHighLowInGraph(int *high, int *low, bool addFuzz) {
if ( loopMax > GraphTraceLen) if ( loopMax > GraphTraceLen)
loopMax = GraphTraceLen; loopMax = GraphTraceLen;
*high = -255; *low = 255;
for (uint8_t i = 0; i < loopMax; ++i) { for (uint8_t i = 0; i < loopMax; ++i) {
if (GraphBuffer[i] > *high) if (GraphBuffer[i] > *high) *high = GraphBuffer[i];
*high = GraphBuffer[i]; if (GraphBuffer[i] < *low) *low = GraphBuffer[i];
else if (GraphBuffer[i] < *low)
*low = GraphBuffer[i];
} }
//12% fuzz in case highs and lows aren't clipped //12% fuzz in case highs and lows aren't clipped
if (addFuzz) { if (addFuzz) {
*high = (int)(*high * .88); *high *= .88;
*low = (int)(*low * .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 // 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 //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; bool isNoise = true;
for(int i=0; i < size && isNoise; i++){ for(int i=0; i < size && isNoise; i++)
isNoise = bits[i] < THRESHOLD; isNoise = bits[i] < THRESHOLD;
}
return isNoise; return isNoise;
*/
} }

View file

@ -10,7 +10,13 @@
#ifndef GRAPH_H__ #ifndef GRAPH_H__
#define GRAPH_H__ #define GRAPH_H__
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "ui.h"
#include "lfdemod.h"
#include "cmddata.h" //for g_debugmode
void AppendGraph(int redraw, int clock, int bit); void AppendGraph(int redraw, int clock, int bit);
int ClearGraph(int redraw); 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 GetNrzClock(const char str[], bool printAns, bool verbose);
uint8_t GetFskClock(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); 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 setGraphBuf(uint8_t *buff, size_t size);
void save_restoreGB(uint8_t saveOpt); void save_restoreGB(uint8_t saveOpt);

View file

@ -60,26 +60,62 @@ void dummy(char *fmt, ...){}
# define prnt dummy # define prnt dummy
#endif #endif
//test samples are not just noise // Function to compute mean for a series
uint8_t justNoise(uint8_t *bits, size_t size) { // 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; uint8_t val = 1;
for(size_t idx = 0; idx < size && val; idx++) for(size_t idx = 0; idx < size && val; idx++)
val = bits[idx] < FSK_PSK_THRESHOLD; val = bits[idx] < FSK_PSK_THRESHOLD;
return val; return val;
*/
} }
//by marshmellow //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 //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) { int getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
*high=0;
*low=255; *high = 0; *low = 255;
// get high and low thresholds // get high and low thresholds
for (size_t i=0; i < size; i++){ for (size_t i=0; i < size; i++){
if (BitStream[i] > *high) *high = BitStream[i]; if (bits[i] > *high) *high = bits[i];
if (BitStream[i] < *low) *low = BitStream[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; *high = ((*high-128)*fuzzHi + 12800)/100;
*low = ((*low-128)*fuzzLo + 12800)/100; *low = ((*low-128)*fuzzLo + 12800)/100;
return 1; 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. // 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 i = 0;
size_t waveSizeCnt = 0; size_t waveSizeCnt = 0;
uint8_t thresholdCnt = 0; uint8_t thresholdCnt = 0;
bool isAboveThreshold = dest[i++] >= FSK_PSK_THRESHOLD; bool isAboveThreshold = src[i++] >= FSK_PSK_THRESHOLD;
for (; i < size-20; i++ ) { for (; i < size-20; i++ ) {
if(dest[i] < FSK_PSK_THRESHOLD && isAboveThreshold) { if(src[i] < FSK_PSK_THRESHOLD && isAboveThreshold) {
thresholdCnt++; thresholdCnt++;
if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break; if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break;
isAboveThreshold = false; isAboveThreshold = false;
waveSizeCnt = 0; waveSizeCnt = 0;
} else if (dest[i] >= FSK_PSK_THRESHOLD && !isAboveThreshold) { } else if (src[i] >= FSK_PSK_THRESHOLD && !isAboveThreshold) {
thresholdCnt++; thresholdCnt++;
if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break; if (thresholdCnt > 2 && waveSizeCnt < expWaveSize+1) break;
isAboveThreshold = true; isAboveThreshold = true;
@ -224,7 +260,7 @@ size_t findModStart(uint8_t dest[], size_t size, uint8_t expWaveSize) {
} }
if (thresholdCnt > 10) break; 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; return i;
} }
@ -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]){ if (dest[idx] == dest[idx+1]){
return -5; //not manchester data return -5; //not manchester data
} }
*hi2 = (*hi2<<1)|(*hi>>31); *hi2 = (*hi2 << 1) | (*hi >> 31);
*hi = (*hi<<1)|(*lo>>31); *hi = (*hi << 1) | (*lo >> 31);
//Then, shift in a 0 or one into low //Then, shift in a 0 or one into low
*lo <<= 1; *lo <<= 1;
if (dest[idx] && !dest[idx+1]) // 1 0 if (dest[idx] && !dest[idx+1]) // 1 0

View file

@ -21,7 +21,10 @@
#include "parity.h" // for parity test #include "parity.h" // for parity test
//generic //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 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(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); extern int askdemod_ext(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx);