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;
}
setGraphBuf(got, sizeof(got));
if (is_justnoise(GraphBuffer, sizeof(got)))
return 0;
return 1;
}

View file

@ -7,14 +7,7 @@
//-----------------------------------------------------------------------------
// Graph utilities
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "ui.h"
#include "graph.h"
#include "lfdemod.h"
#include "cmddata.h" //for g_debugmode
int GraphBuffer[MAX_GRAPH_TRACE_LEN];
int GraphTraceLen;
@ -81,7 +74,6 @@ 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){
@ -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;
*/
}

View file

@ -10,7 +10,13 @@
#ifndef GRAPH_H__
#define GRAPH_H__
#include <stdio.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);
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);

View file

@ -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;

View file

@ -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);