Merge pull request from RfidResearchGroup/computeSignalProperties

Replace isNoise by computeSignalProperties and removeSignalOffset
This commit is contained in:
Iceman 2019-03-09 08:35:37 +01:00 committed by GitHub
commit 17435d366f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 129 deletions

View file

@ -204,8 +204,9 @@ uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averag
dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]); dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
} }
// Ensure that noise check is performed for any device-side processing // Ensure that DC offset removal and noise check is performed for any device-side processing
isNoise(dest, bufsize); removeSignalOffset(dest, bufsize);
computeSignalProperties(dest, bufsize);
return data.numbits; return data.numbits;
} }
@ -451,4 +452,4 @@ uint32_t doCotagAcquisitionManchester() {
} }
} }
return sample_counter; return sample_counter;
} }

View file

@ -1393,15 +1393,13 @@ int CmdHide(const char *Cmd) {
//zero mean GraphBuffer //zero mean GraphBuffer
int CmdHpf(const char *Cmd) { int CmdHpf(const char *Cmd) {
int i, accum = 0; uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits);
for (i = 10; i < GraphTraceLen; ++i) removeSignalOffset(bits, size);
accum += GraphBuffer[i]; // push it back to graph
setGraphBuf(bits, size);
accum /= (GraphTraceLen - 10); // set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
for (i = 0; i < GraphTraceLen; ++i)
GraphBuffer[i] -= accum;
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
@ -1471,12 +1469,11 @@ int getSamples(int n, bool silent) {
GraphTraceLen = n; GraphTraceLen = n;
} }
//ICEMAN todo
uint8_t bits[GraphTraceLen]; uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits); size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noice detection // set signal properties low/high/mean/amplitude and is_noise detection
isNoise(bits, size); computeSignalProperties(bits, size);
setClockGrid(0, 0); setClockGrid(0, 0);
DemodBufferLen = 0; DemodBufferLen = 0;
RepaintGraphWindow(); RepaintGraphWindow();
@ -1610,15 +1607,19 @@ int CmdLoad(const char *Cmd) {
fclose(f); fclose(f);
PrintAndLogEx(SUCCESS, "loaded %d samples", GraphTraceLen); PrintAndLogEx(SUCCESS, "loaded %d samples", GraphTraceLen);
uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noise detection
removeSignalOffset(bits, size);
// push it back to graph
setGraphBuf(bits, size);
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
setClockGrid(0,0); setClockGrid(0,0);
DemodBufferLen = 0; DemodBufferLen = 0;
RepaintGraphWindow(); RepaintGraphWindow();
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits);
isNoise(bits, size);
return 0; return 0;
} }
@ -1683,11 +1684,11 @@ int CmdNorm(const char *Cmd) {
} }
} }
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
uint8_t bits[GraphTraceLen]; uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits); size_t size = getFromGraphBuf(bits);
isNoise(bits, size); // set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
} }
@ -1773,11 +1774,11 @@ int CmdDirectionalThreshold(const char *Cmd) {
directionalThreshold(GraphBuffer, GraphBuffer, GraphTraceLen, up, down); directionalThreshold(GraphBuffer, GraphBuffer, GraphTraceLen, up, down);
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection // set signal properties low/high/mean/amplitude and isnoice detection
uint8_t bits[GraphTraceLen]; uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits); size_t size = getFromGraphBuf(bits);
isNoise(bits, size); // set signal properties low/high/mean/amplitude and is_noice detection
computeSignalProperties(bits, size);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
@ -1805,11 +1806,10 @@ int CmdZerocrossings(const char *Cmd) {
} }
} }
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
uint8_t bits[GraphTraceLen]; uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits); size_t size = getFromGraphBuf(bits);
isNoise(bits, size); // set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
@ -2063,13 +2063,11 @@ int CmdDataIIR(const char *Cmd){
//iceIIR_Butterworth(GraphBuffer, GraphTraceLen); //iceIIR_Butterworth(GraphBuffer, GraphTraceLen);
iceSimple_Filter(GraphBuffer, GraphTraceLen, k); iceSimple_Filter(GraphBuffer, GraphTraceLen, k);
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
uint8_t bits[GraphTraceLen]; uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits); size_t size = getFromGraphBuf(bits);
isNoise(bits, size); // set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(bits, size);
RepaintGraphWindow(); RepaintGraphWindow();
return 0; return 0;
} }

View file

@ -800,7 +800,7 @@ int EM4x50Read(const char *Cmd, bool verbose) {
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0}; uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(bits); size_t size = getFromGraphBuf(bits);
isNoise(bits, size); computeSignalProperties(bits, size);
signal_t *sp = getSignalProperties(); signal_t *sp = getSignalProperties();
high = sp->high; high = sp->high;
@ -999,9 +999,12 @@ bool downloadSamplesEM(){
PrintAndLogEx(WARNING, "command execution time out"); PrintAndLogEx(WARNING, "command execution time out");
return false; return false;
} }
setGraphBuf(got, sizeof(got));
if (isNoise(got, sizeof(got))) { setGraphBuf(got, sizeof(got));
// set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(got, sizeof(got));
RepaintGraphWindow();
if (getSignalProperties()->isnoise) {
PrintAndLogEx(DEBUG, "No tag found"); PrintAndLogEx(DEBUG, "No tag found");
return false; return false;
} }

View file

@ -1331,7 +1331,10 @@ bool AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password )
return false; return false;
} }
setGraphBuf(got, sizeof(got)); setGraphBuf(got, sizeof(got));
return !isNoise(got, sizeof(got)); // set signal properties low/high/mean/amplitude and is_noise detection
computeSignalProperties(got, sizeof(got));
RepaintGraphWindow();
return !getSignalProperties()->isnoise;
} }
char * GetBitRateStr(uint32_t id, bool xmode) { char * GetBitRateStr(uint32_t id, bool xmode) {

View file

@ -463,7 +463,7 @@ bool GetFromDevice(DeviceMemType_t memtype, uint8_t *dest, uint32_t bytes, uint3
return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_FLASHMEM_DOWNLOADED); return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_FLASHMEM_DOWNLOADED);
} }
case SIM_MEM: { case SIM_MEM: {
//UsbCommand c = {CMD_DOWNLOAND_SIM_MEM, {start_index, bytes, 0}}; //UsbCommand c = {CMD_DOWNLOAD_SIM_MEM, {start_index, bytes, 0}};
//SendCommand(&c); //SendCommand(&c);
//return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_SIMMEM); //return dl_it(dest, bytes, start_index, response, ms_timeout, show_warning, CMD_DOWNLOADED_SIMMEM);
return false; return false;

View file

@ -47,8 +47,7 @@
//********************************************************************************************** //**********************************************************************************************
#define LOWEST_DEFAULT_CLOCK 32 #define LOWEST_DEFAULT_CLOCK 32
#define FSK_PSK_THRESHOLD 123 #define FSK_PSK_THRESHOLD 123
//might not be high enough for noisy environments
#define NOICE_AMPLITUDE_THRESHOLD 10
//to allow debug print calls when used not on dev //to allow debug print calls when used not on dev
//void dummy(char *fmt, ...){} //void dummy(char *fmt, ...){}
@ -76,6 +75,7 @@ static void resetSignal(void) {
signalprop.amplitude = 0; signalprop.amplitude = 0;
signalprop.isnoise = true; signalprop.isnoise = true;
} }
static void printSignal(void) { static void printSignal(void) {
prnt("LF signal properties:"); prnt("LF signal properties:");
prnt(" high..........%d", signalprop.high); prnt(" high..........%d", signalprop.high);
@ -83,94 +83,47 @@ static void printSignal(void) {
prnt(" mean..........%d", signalprop.mean); prnt(" mean..........%d", signalprop.mean);
prnt(" amplitude.....%d", signalprop.amplitude); prnt(" amplitude.....%d", signalprop.amplitude);
prnt(" is Noise......%s", (signalprop.isnoise) ? "Yes" : "No"); prnt(" is Noise......%s", (signalprop.isnoise) ? "Yes" : "No");
prnt(" THRESHOLD noice amplitude......%d" , NOICE_AMPLITUDE_THRESHOLD); prnt(" THRESHOLD noise amplitude......%d" , NOISE_AMPLITUDE_THRESHOLD);
} }
// Function to compute mean for a series void computeSignalProperties(uint8_t *samples, uint32_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 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 mean / (int)N;
}
void zeromean(uint8_t* data, size_t size) {
// zero mean data
int i, accum = 0;
for (i = 10; i < size; ++i)
accum += data[i] - 128;
accum /= (int)(size - 10);
for (i = 0; i < size; ++i) {
if (accum > 0) {
data[i] = (data[i] >= accum)? data[i] - accum : 0;
}
if (accum < 0) {
data[i] = (255 - data[i] >= -accum)? data[i] - accum : 255;
}
}
// recompute signal characteristics:
isNoise(data, size);
}
//test samples are not just noise
// By measuring mean and look at amplitude of signal from HIGH / LOW, we can detect noise
bool isNoise_int(int *bits, uint32_t size) {
resetSignal(); resetSignal();
if ( bits == NULL || size < 100 ) return true;
int32_t sum = 0;
for ( size_t i = 0; i < size; i++) {
if ( bits[i] < signalprop.low ) signalprop.low = bits[i];
if ( bits[i] > signalprop.high ) signalprop.high = bits[i];
sum += bits[i];
}
// measure amplitude of signal
signalprop.mean = sum / (int)size;
signalprop.amplitude = ABS(signalprop.high - signalprop.mean);
signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD;
if (g_debugMode)
printSignal();
return signalprop.isnoise;
}
//test samples are not just noise
// By measuring mean and look at amplitude of signal from HIGH / LOW,
// we can detect noise
bool isNoise(uint8_t *bits, uint32_t size) {
resetSignal();
if ( bits == NULL || size < 100 ) return true;
uint32_t sum = 0; uint32_t sum = 0;
for ( uint32_t i = 0; i < size; i++) { for ( uint32_t i = 0; i < size; i++) {
if ( bits[i] < signalprop.low ) signalprop.low = bits[i]; if ( samples[i] < signalprop.low ) signalprop.low = samples[i];
if ( bits[i] > signalprop.high ) signalprop.high = bits[i]; if ( samples[i] > signalprop.high ) signalprop.high = samples[i];
sum += bits[i]; sum += samples[i];
} }
// measure amplitude of signal // measure amplitude of signal
signalprop.mean = sum / size; signalprop.mean = sum / size;
signalprop.amplitude = signalprop.high - signalprop.mean; signalprop.amplitude = signalprop.high - signalprop.mean;
signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD; // By measuring mean and look at amplitude of signal from HIGH / LOW,
// we can detect noise
signalprop.isnoise = signalprop.amplitude < NOISE_AMPLITUDE_THRESHOLD;
if (g_debugMode) if (g_debugMode)
printSignal(); printSignal();
}
return signalprop.isnoise; void removeSignalOffset(uint8_t *samples, uint32_t size) {
if ( samples == NULL || size < SIGNAL_MIN_SAMPLES ) return;
int acc_off = 0;
for (uint32_t i = SIGNAL_IGNORE_FIRST_SAMPLES; i < size; i++)
acc_off += samples[i] - 128;
acc_off /= (int)(size - SIGNAL_IGNORE_FIRST_SAMPLES);
// shift and saturate samples to center the mean
for ( uint32_t i = 0; i < size; i++) {
if (acc_off > 0) {
samples[i] = (samples[i] >= acc_off)? samples[i] - acc_off : 0;
}
if (acc_off < 0) {
samples[i] = (255 - samples[i] >= -acc_off)? samples[i] - acc_off : 255;
}
}
} }
//by marshmellow //by marshmellow
@ -1962,8 +1915,6 @@ int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx) {
if (signalprop.isnoise) return -2; if (signalprop.isnoise) return -2;
zeromean(dest, *size);
// FSK2a demodulator clock 50, invert 1, fcHigh 10, fcLow 8 // FSK2a demodulator clock 50, invert 1, fcHigh 10, fcLow 8
*size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); //awid fsk2a *size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); //awid fsk2a
@ -2029,8 +1980,6 @@ int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32
if (signalprop.isnoise) return -2; if (signalprop.isnoise) return -2;
zeromean(dest, *size);
// FSK demodulator fsk2a so invert and fc/10/8 // FSK demodulator fsk2a so invert and fc/10/8
*size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); //hid fsk2a *size = fskdemod(dest, *size, 50, 1, 10, 8, waveStartIdx); //hid fsk2a
@ -2090,8 +2039,6 @@ int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) {
if (signalprop.isnoise) return -2; if (signalprop.isnoise) return -2;
zeromean(dest, *size);
// FSK demodulator RF/64, fsk2a so invert, and fc/10/8 // FSK demodulator RF/64, fsk2a so invert, and fc/10/8
*size = fskdemod(dest, *size, 64, 1, 10, 8, waveStartIdx); //io fsk2a *size = fskdemod(dest, *size, 64, 1, 10, 8, waveStartIdx); //io fsk2a

View file

@ -20,6 +20,14 @@
#include <stdbool.h> // for bool #include <stdbool.h> // for bool
#include "parity.h" // for parity test #include "parity.h" // for parity test
#include "util.h" // for ARRAYLEN #include "util.h" // for ARRAYLEN
//might not be high enough for noisy environments
#define NOISE_AMPLITUDE_THRESHOLD 10
//ignore buffer with less than x samples
#define SIGNAL_MIN_SAMPLES 100
//ignore first x samples of the buffer
#define SIGNAL_IGNORE_FIRST_SAMPLES 10
//generic //generic
typedef struct { typedef struct {
int low; int low;
@ -28,14 +36,10 @@ typedef struct {
int amplitude; int amplitude;
bool isnoise; bool isnoise;
} signal_t; } signal_t;
extern signal_t* getSignalProperties(void); signal_t* getSignalProperties(void);
extern uint32_t compute_mean_uint(uint8_t *in, size_t N); void computeSignalProperties(uint8_t *bits, uint32_t size);
extern int32_t compute_mean_int(int *in, size_t N); void removeSignalOffset(uint8_t *samples, uint32_t size);
bool isNoise_int(int *bits, uint32_t size);
bool isNoise(uint8_t *bits, uint32_t size);
extern void zeromean(uint8_t* data, size_t size);
void getNextLow(uint8_t *samples, size_t size, int low, size_t *i); void getNextLow(uint8_t *samples, size_t size, int low, size_t *i);
void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i); void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i);
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low); bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low);