Chg: step2 in the signed vis unsigned graphbuffer signal processing.

It will break some tone-based demods like ti etc.
This commit is contained in:
Chris 2018-09-09 12:34:43 +02:00
commit d774e4c84f
8 changed files with 276 additions and 217 deletions

View file

@ -456,8 +456,8 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
int maxErr = 100;
int maxLen = 0;
uint8_t askamp = 0;
char amp = param_getchar(Cmd, 0);
uint8_t BitStream[MAX_GRAPH_TRACE_LEN] = {0};
char amp = tolower(param_getchar(Cmd, 0));
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
sscanf(Cmd, "%i %i %i %i %c", &clk, &invert, &maxErr, &maxLen, &amp);
@ -473,7 +473,7 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
clk = 0;
}
size_t BitLen = getFromGraphBuf(BitStream);
size_t BitLen = getFromGraphBuf(bits);
PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) Bitlen from grphbuff: %d", BitLen);
@ -483,13 +483,13 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
int foundclk = 0;
//amp before ST check
if (amp == 'a' || amp == 'A')
askAmp(BitStream, BitLen);
if (amp == 'a')
askAmp(bits, BitLen);
bool st = false;
size_t ststart = 0, stend = 0;
if (*stCheck)
st = DetectST(BitStream, &BitLen, &foundclk, &ststart, &stend);
st = DetectST(bits, &BitLen, &foundclk, &ststart, &stend);
if (st) {
*stCheck = st;
@ -501,10 +501,10 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
}
int startIdx = 0;
int errCnt = askdemod_ext(BitStream, &BitLen, &clk, &invert, maxErr, askamp, askType, &startIdx);
int errCnt = askdemod_ext(bits, &BitLen, &clk, &invert, maxErr, askamp, askType, &startIdx);
if (errCnt < 0 || BitLen < 16){ //if fatal error (or -1)
PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) No data found errors:%d, invert:%d, bitlen:%d, clock:%d", errCnt, invert, BitLen, clk);
PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) No data found errors:%d, invert:%c, bitlen:%d, clock:%d", errCnt, (invert)?'Y':'N', BitLen, clk);
return 0;
}
@ -516,7 +516,7 @@ int ASKDemod_ext(const char *Cmd, bool verbose, bool emSearch, uint8_t askType,
if (verbose || g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: (ASKDemod_ext) Using clock:%d, invert:%d, bits found:%d", clk, invert, BitLen);
//output
setDemodBuf(BitStream,BitLen,0);
setDemodBuf(bits, BitLen, 0);
setClockGrid(clk, startIdx);
if (verbose || g_debugMode){
@ -1429,6 +1429,7 @@ int getSamples(int n, bool silent) {
GraphTraceLen = j;
if (!silent) PrintAndLogEx(NORMAL, "Unpacked %d samples" , j );
} else {
for (int j = 0; j < n; j++) {
GraphBuffer[j] = ((int)got[j]) - 128;
@ -1437,8 +1438,10 @@ int getSamples(int n, bool silent) {
}
//ICEMAN todo
uint8_t bits[GraphTraceLen];
size_t size = getFromGraphBuf(bits);
// set signal properties low/high/mean/amplitude and is_noice detection
justNoise(GraphBuffer, GraphTraceLen);
isNoise(bits, size);
setClockGrid(0, 0);
DemodBufferLen = 0;
@ -1571,7 +1574,7 @@ int CmdLoad(const char *Cmd) {
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen);
isNoise_int(GraphBuffer, GraphTraceLen);
return 0;
}
@ -1639,7 +1642,7 @@ int CmdNorm(const char *Cmd) {
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen);
isNoise_int(GraphBuffer, GraphTraceLen);
return 0;
}
@ -1723,6 +1726,11 @@ int CmdDirectionalThreshold(const char *Cmd) {
PrintAndLogEx(INFO, "Applying Up Threshold: %d, Down Threshold: %d\n", up, down);
directionalThreshold(GraphBuffer, GraphBuffer, GraphTraceLen, up, down);
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
isNoise_int(GraphBuffer, GraphTraceLen);
RepaintGraphWindow();
return 0;
}
@ -1751,7 +1759,7 @@ int CmdZerocrossings(const char *Cmd) {
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen);
isNoise_int(GraphBuffer, GraphTraceLen);
RepaintGraphWindow();
return 0;
@ -2004,6 +2012,10 @@ int CmdDataIIR(const char *Cmd){
uint8_t k = param_get8(Cmd, 0);
//iceIIR_Butterworth(GraphBuffer, GraphTraceLen);
iceSimple_Filter(GraphBuffer, GraphTraceLen, k);
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
isNoise_int(GraphBuffer, GraphTraceLen);
RepaintGraphWindow();
return 0;
}

View file

@ -148,9 +148,8 @@ int CmdLFCommandRead(const char *Cmd) {
uint8_t cmdp = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
case 'H':
return usage_lf_cmdread();
case 'c':
param_getstr(Cmd, cmdp+1, (char *)&c.d.asBytes, sizeof(c.d.asBytes));
@ -188,16 +187,20 @@ int CmdLFCommandRead(const char *Cmd) {
int CmdFlexdemod(const char *Cmd) {
#define LONG_WAIT 100
int i, j, start, bit, sum;
int phase = 0;
int i, j, start, bit, sum, phase = 0;
for (i = 0; i < GraphTraceLen; ++i)
GraphBuffer[i] = (GraphBuffer[i] < 0) ? -1 : 1;
uint8_t data[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(data);
if (size == 0)
return 0;
for (start = 0; start < GraphTraceLen - LONG_WAIT; start++) {
int first = GraphBuffer[start];
for (i = 0; i < size; ++i)
data[i] = (data[i] < 0) ? -1 : 1;
for (start = 0; start < size - LONG_WAIT; start++) {
int first = data[start];
for (i = start; i < start + LONG_WAIT; i++) {
if (GraphBuffer[i] != first) {
if (data[i] != first) {
break;
}
}
@ -205,20 +208,21 @@ int CmdFlexdemod(const char *Cmd) {
break;
}
if (start == GraphTraceLen - LONG_WAIT) {
if (start == size - LONG_WAIT) {
PrintAndLogEx(NORMAL, "nothing to wait for");
return 0;
}
GraphBuffer[start] = 2;
GraphBuffer[start+1] = -2;
data[start] = 4;
data[start+1] = 0;
uint8_t bits[64] = {0x00};
i = start;
for (bit = 0; bit < 64; bit++) {
sum = 0;
for (int j = 0; j < 16; j++) {
sum += GraphBuffer[i++];
sum += data[i++];
}
bits[bit] = (sum > 0) ? 1 : 0;
PrintAndLogEx(NORMAL, "bit %d sum %d", bit, sum);
@ -227,7 +231,7 @@ int CmdFlexdemod(const char *Cmd) {
for (bit = 0; bit < 64; bit++) {
sum = 0;
for (j = 0; j < 16; j++)
sum += GraphBuffer[i++];
sum += data[i++];
if (sum > 0 && bits[bit] != 1) PrintAndLogEx(NORMAL, "oops1 at %d", bit);
@ -235,6 +239,7 @@ int CmdFlexdemod(const char *Cmd) {
}
// iceman, use demod buffer? blue line?
// HACK writing back to graphbuffer.
GraphTraceLen = 32 * 64;
i = 0;
@ -349,17 +354,14 @@ int CmdLFRead(const char *Cmd) {
uint32_t samples = 0;
uint8_t cmdp = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
case 'H':
return usage_lf_read();
case 's':
case 'S':
silent = true;
cmdp++;
break;
case 'd':
case 'D':
samples = param_get32ex(Cmd, cmdp, 0, 10);
cmdp +=2;
break;
@ -377,8 +379,8 @@ int CmdLFRead(const char *Cmd) {
}
int CmdLFSnoop(const char *Cmd) {
uint8_t cmdp = param_getchar(Cmd, 0);
if(cmdp == 'h' || cmdp == 'H') return usage_lf_snoop();
uint8_t cmdp = tolower(param_getchar(Cmd, 0));
if (cmdp == 'h') return usage_lf_snoop();
UsbCommand c = {CMD_LF_SNOOP_RAW_ADC_SAMPLES,{0,0,0}};
clearCommandBuffer();
@ -538,8 +540,7 @@ int CmdLFaskSim(const char *Cmd) {
uint8_t cmdp = 0;
while(param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) {
case 'H':
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h': return usage_lf_simask();
case 'i':
invert = 1;
@ -633,7 +634,7 @@ int CmdLFpskSim(const char *Cmd) {
uint8_t pskType = 1;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch(param_getchar(Cmd, cmdp)) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
return usage_lf_simpsk();
case 'i':
@ -735,6 +736,7 @@ int CmdLFSimBidir(const char *Cmd) {
return 0;
}
// ICEMAN, todo, swap from Graphbuffer.
int CmdVchDemod(const char *Cmd) {
// Is this the entire sync pattern, or does this also include some
// data bits that happen to be the same everywhere? That would be

View file

@ -411,7 +411,7 @@ int CmdEM410xRead(const char *Cmd) {
// this read loops on device side.
// uses the demod in lfops.c
int CmdEM410xRead_device(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0);
char cmdp = tolower(param_getchar(Cmd, 0));
uint8_t findone = (cmdp == '1') ? 1 : 0;
UsbCommand c = {CMD_EM410X_DEMOD, {findone, 0, 0}};
SendCommand(&c);
@ -423,8 +423,8 @@ int CmdEM410xRead_device(const char *Cmd) {
//attempts to demodulate ask while decoding manchester
//prints binary found and saves in graphbuffer for further commands
int CmdEM410xDemod(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_demod();
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 10 || cmdp == 'h') return usage_lf_em410x_demod();
uint32_t hi = 0;
uint64_t lo = 0;
@ -437,8 +437,8 @@ int CmdEM410xDemod(const char *Cmd) {
// emulate an EM410X tag
int CmdEM410xSim(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0);
if (cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_sim();
char cmdp = tolower(param_getchar(Cmd, 0));
if (cmdp == 'h') return usage_lf_em410x_sim();
uint8_t uid[5] = {0x00};
@ -594,8 +594,8 @@ int CmdEM410xWatch(const char *Cmd) {
//currently only supports manchester modulations
int CmdEM410xWatchnSpoof(const char *Cmd) {
char cmdp = param_getchar(Cmd, 0);
if (cmdp == 'h' || cmdp == 'H') return usage_lf_em410x_ws();
char cmdp = tolower(param_getchar(Cmd, 0));
if (cmdp == 'h') return usage_lf_em410x_ws();
// loops if the captured ID was in XL-format.
CmdEM410xWatch(Cmd);
@ -784,55 +784,50 @@ uint32_t OutputEM4x50_Block(uint8_t *BitStream, size_t size, bool verbose, bool
//completed by Marshmellow
int EM4x50Read(const char *Cmd, bool verbose) {
uint8_t fndClk[] = {8,16,32,40,50,64,128};
int clk = 0;
int invert = 0;
int tol = 0;
int i, j, startblock, skip, block, start, end, low, high, minClk;
bool complete = false;
int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
int clk = 0, invert = 0, tol = 0, phaseoff;
int i = 0, j = 0, startblock, skip, block, start, end, low = 0, high = 0, minClk = 255;
uint32_t Code[6];
char tmp[6];
char tmp2[20];
int phaseoff;
high = low = 0;
bool complete = false;
int tmpbuff[MAX_GRAPH_TRACE_LEN / 64];
memset(tmpbuff, 0, sizeof(tmpbuff) );
// get user entry if any
sscanf(Cmd, "%i %i", &clk, &invert);
// first get high and low values
for (i = 0; i < GraphTraceLen; i++) {
if (GraphBuffer[i] > high)
high = GraphBuffer[i];
else if (GraphBuffer[i] < low)
low = GraphBuffer[i];
}
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
size_t size = getFromGraphBuf(bits);
isNoise(bits, size);
signal_t *sp = getSignalProperties();
high = sp->high;
low = sp->low;
i = 0;
j = 0;
minClk = 255;
// get to first full low to prime loop and skip incomplete first pulse
while ((GraphBuffer[i] < high) && (i < GraphTraceLen))
while ((bits[i] < high) && (i < size))
++i;
while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
while ((bits[i] > low) && (i < size))
++i;
skip = i;
// populate tmpbuff buffer with pulse lengths
while (i < GraphTraceLen) {
while (i < size) {
// measure from low to low
while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
while ((bits[i] > low) && (i < size))
++i;
start= i;
while ((GraphBuffer[i] < high) && (i < GraphTraceLen))
while ((bits[i] < high) && (i < size))
++i;
while ((GraphBuffer[i] > low) && (i < GraphTraceLen))
while ((bits[i] > low) && (i < size))
++i;
if (j >= (MAX_GRAPH_TRACE_LEN/64)) {
break;
}
tmpbuff[j++] = i - start;
if (i-start < minClk && i < GraphTraceLen) {
if (i-start < minClk && i < size) {
minClk = i - start;
}
}
@ -867,11 +862,13 @@ int EM4x50Read(const char *Cmd, bool verbose) {
startblock = i + 4;
// skip over the remainder of LW
skip += tmpbuff[i+1] + tmpbuff[i+2] + clk;
skip += (tmpbuff[i+1] + tmpbuff[i+2] + clk);
if (tmpbuff[i+3] > clk)
phaseoff = tmpbuff[i+3] - clk;
else
phaseoff = 0;
// now do it again to find the end
end = skip;
for (i += 3; i < j - 4 ; ++i) {
@ -897,6 +894,7 @@ int EM4x50Read(const char *Cmd, bool verbose) {
return 0;
}
} else if (start < 0) return 0;
start = skip;
snprintf(tmp2, sizeof(tmp2),"%d %d 1000 %d", clk, invert, clk * 47);
// save GraphBuffer - to restore it later
@ -926,7 +924,9 @@ int EM4x50Read(const char *Cmd, bool verbose) {
phaseoff = tmpbuff[i+1] - clk;
else
phaseoff = 0;
i += 2;
if (ASKDemod(tmp2, false, false, 1) < 1) {
save_restoreGB(GRAPH_RESTORE);
return 0;
@ -972,19 +972,19 @@ int EM4x50Read(const char *Cmd, bool verbose) {
}
int CmdEM4x50Read(const char *Cmd) {
uint8_t ctmp = param_getchar(Cmd, 0);
if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em4x50_read();
uint8_t ctmp = tolower(param_getchar(Cmd, 0));
if ( ctmp == 'h' ) return usage_lf_em4x50_read();
return EM4x50Read(Cmd, true);
}
int CmdEM4x50Write(const char *Cmd){
uint8_t ctmp = param_getchar(Cmd, 0);
if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em4x50_write();
uint8_t ctmp = tolower(param_getchar(Cmd, 0));
if ( ctmp == 'h' ) return usage_lf_em4x50_write();
PrintAndLogEx(NORMAL, "no implemented yet");
return 0;
}
int CmdEM4x50Dump(const char *Cmd){
uint8_t ctmp = param_getchar(Cmd, 0);
if ( ctmp == 'H' || ctmp == 'h' ) return usage_lf_em4x50_dump();
uint8_t ctmp = tolower(param_getchar(Cmd, 0));
if ( ctmp == 'h' ) return usage_lf_em4x50_dump();
PrintAndLogEx(NORMAL, "no implemented yet");
return 0;
}
@ -1000,6 +1000,11 @@ bool downloadSamplesEM(){
return false;
}
setGraphBuf(got, sizeof(got));
if (isNoise(got, sizeof(got))) {
PrintAndLogEx(DEBUG, "No tag found");
return false;
}
return true;
}
@ -1146,12 +1151,7 @@ int EM4x05ReadWord_ext(uint8_t addr, uint32_t pwd, bool usePwd, uint32_t *word)
if ( !downloadSamplesEM() ) {
return -1;
}
int testLen = (GraphTraceLen < 1000) ? GraphTraceLen : 1000;
if (justNoise(GraphBuffer, testLen)) {
PrintAndLogEx(DEBUG, "No tag found");
return -1;
}
return demodEM4x05resp(word);
}

View file

@ -184,8 +184,10 @@ int CmdIndalaDemodAlt(const char *Cmd) {
int count = 0;
int i, j;
// worst case with GraphTraceLen=64000 is < 4096
// worst case with GraphTraceLen=40000 is < 4096
// under normal conditions it's < 2048
uint8_t data[MAX_GRAPH_TRACE_LEN] = {0};
size_t datasize = getFromGraphBuf(data);
uint8_t rawbits[4096];
int rawbit = 0;
@ -197,9 +199,9 @@ int CmdIndalaDemodAlt(const char *Cmd) {
// PrintAndLogEx(NORMAL, "Expecting a bit less than %d raw bits", GraphTraceLen / 32);
// loop through raw signal - since we know it is psk1 rf/32 fc/2 skip every other value (+=2)
for (i = 0; i < GraphTraceLen-1; i += 2) {
for (i = 0; i < datasize-1; i += 2) {
count += 1;
if ((GraphBuffer[i] > GraphBuffer[i + 1]) && (state != 1)) {
if ((data[i] > data[i + 1]) && (state != 1)) {
// appears redundant - marshmellow
if (state == 0) {
for (j = 0; j < count - 8; j += 16) {
@ -212,7 +214,7 @@ int CmdIndalaDemodAlt(const char *Cmd) {
}
state = 1;
count = 0;
} else if ((GraphBuffer[i] < GraphBuffer[i + 1]) && (state != 0)) {
} else if ((data[i] < data[i + 1]) && (state != 0)) {
//appears redundant
if (state == 1) {
for (j = 0; j < count - 8; j += 16) {

View file

@ -504,12 +504,13 @@ int CmdT55xxDetect(const char *Cmd){
// detect configuration?
bool tryDetectModulation(){
uint8_t hits = 0;
t55xx_conf_block_t tests[15];
int bitRate=0;
uint8_t fc1 = 0, fc2 = 0, ans = 0;
int clk = 0, firstClockEdge = 0;
int bitRate = 0, clk = 0, firstClockEdge = 0;
uint8_t hits = 0, fc1 = 0, fc2 = 0, ans = 0;
ans = fskClocks(&fc1, &fc2, (uint8_t *)&clk, &firstClockEdge);
if (ans && ((fc1==10 && fc2==8) || (fc1==8 && fc2==5))) {
if ( FSKrawDemod("0 0", false) && test(DEMOD_FSK, &tests[hits].offset, &bitRate, clk, &tests[hits].Q5)){
tests[hits].modulation = DEMOD_FSK;
@ -1305,15 +1306,13 @@ bool AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){
return false;
}
//uint8_t got[12288];
uint8_t got[7679];
if ( !GetFromDevice(BIG_BUF, got, sizeof(got), 0, NULL, 6000, true)) {
uint8_t got[8000];
if ( !GetFromDevice(BIG_BUF, got, sizeof(got), 0, NULL, 4000, true)) {
PrintAndLogEx(WARNING, "command execution time out");
return false;
}
setGraphBuf(got, sizeof(got));
return !justNoise(GraphBuffer, sizeof(got));
return !isNoise(got, sizeof(got));
}
char * GetBitRateStr(uint32_t id, bool xmode) {

View file

@ -105,7 +105,6 @@ int32_t compute_mean_int(int *in, size_t N) {
return mean / (int)N;
}
void zeromean(uint8_t* data, size_t size) {
// zero mean data
@ -119,12 +118,12 @@ void zeromean(uint8_t* data, size_t size) {
data[i] -= accum;
}
//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();
if ( bits == NULL || size < 100 ) return true;
//zeromean(bits, size);
int32_t sum = 0;
for ( size_t i = 0; i < size; i++) {
@ -138,7 +137,7 @@ bool isNoise_int(int *bits, uint32_t size) {
signalprop.amplitude = ABS(signalprop.high - signalprop.mean);
signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD;
if (g_debugMode == 1)
if (g_debugMode)
printSignal();
return signalprop.isnoise;
@ -149,6 +148,7 @@ bool isNoise_int(int *bits, uint32_t size) {
bool isNoise(uint8_t *bits, uint32_t size) {
resetSignal();
if ( bits == NULL || size < 100 ) return true;
zeromean(bits, size);
uint32_t sum = 0;
for ( uint32_t i = 0; i < size; i++) {
@ -162,7 +162,7 @@ bool isNoise(uint8_t *bits, uint32_t size) {
signalprop.amplitude = signalprop.high - signalprop.mean;
signalprop.isnoise = signalprop.amplitude < NOICE_AMPLITUDE_THRESHOLD;
if (g_debugMode == 1)
if (g_debugMode)
printSignal();
return signalprop.isnoise;
@ -170,22 +170,20 @@ bool isNoise(uint8_t *bits, uint32_t size) {
//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 *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
// just noise - no super good detection. good enough
if (signalprop.isnoise) return -1;
//void getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo) {
// add fuzz.
*high = (signalprop.high * fuzzHi) / 100;
if ( signalprop.low < 0 ) {
*low = (signalprop.low * fuzzLo) / 100;
} else {
*low = signalprop.low * (100 + (100 - fuzzLo))/100;
uint8_t range = signalprop.high - signalprop.low;
*low = signalprop.low + ((range * (100-fuzzLo))/100);
}
if (g_debugMode > 0)
if (g_debugMode)
prnt("getHiLo fuzzed: High %d | Low %d", *high, *low);
return 1;
}
// by marshmellow
@ -362,13 +360,17 @@ void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i) {
// load wave counters
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low) {
size_t i = 0, firstLow, firstHigh;
size_t testsize = (size < 512) ? size : 512;
//size_t testsize = (size < 512) ? size : 512;
if ( getHiLo(samples, testsize, high, low, 80, 80) == -1 ) {
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG STT: just noise detected - quitting");
return false; //just noise
return false;
}
//getHiLo(samples, testsize, high, low, 80, 80);
getHiLo(high, low, 80, 80);
// get to first full low to prime loop and skip incomplete first pulse
getNextHigh(samples, size, *high, &i);
getNextLow(samples, size, *low, &i);
@ -469,7 +471,8 @@ int ManchesterEncode(uint8_t *bits, size_t size) {
// by marshmellow
// to detect a wave that has heavily clipped (clean) samples
uint8_t DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low) {
// loop 512 samples, if 300 of them is deemed maxed out, we assume the wave is clipped.
bool DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low) {
bool allArePeaks = true;
uint16_t cntPeaks = 0;
size_t loopEnd = 512 + 160;
@ -484,8 +487,9 @@ uint8_t DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low
else
cntPeaks++;
}
if (!allArePeaks){
if (cntPeaks > 300) return true;
if (cntPeaks > 250) return true;
}
return allArePeaks;
}
@ -505,6 +509,8 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo
size_t minClk = 512;
int shortestWaveIdx = 0;
if (g_debugMode == 1) prnt("DEBUG ASK: DetectStrongAskClock: hi %d | low %d ", high, low);
// get to first full low to prime loop and skip incomplete first pulse
getNextHigh(dest, size, high, &i);
getNextLow(dest, size, low, &i);
@ -546,18 +552,29 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
size -= 60; //sometimes there is a strange end wave - filter out this....
//if we already have a valid clock
uint8_t clockFnd = 0;
for (; i < clkEnd; ++i)
if (clk[i] == *clock) clockFnd = i;
for (; i < clkEnd; ++i) {
if (clk[i] == *clock) {
//clock found but continue to find best startpos
clockFnd = i;
}
}
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG DetectASKClock: just noise detected - quitting");
return -1;
}
//get high and low peak
int peak, low;
if (getHiLo(dest, loopCnt, &peak, &low, 75, 75) < 1) return -1;
int peak_hi, peak_low;
//getHiLo(dest, loopCnt, &peak_hi, &peak_low, 75, 75);
getHiLo(&peak_hi, &peak_low, 75, 75);
//test for large clean peaks
if (!clockFnd){
if (DetectCleanAskWave(dest, size, peak, low)==1){
int ans = DetectStrongAskClock(dest, size, peak, low, clock);
if (DetectCleanAskWave(dest, size, peak_hi, peak_low)){
int ans = DetectStrongAskClock(dest, size, peak_hi, peak_low, clock);
if (g_debugMode == 2) prnt("DEBUG ASK: detectaskclk Clean Ask Wave Detected: clk %i, ShortestWave: %i", *clock ,ans);
if (ans > 0){
return ans; //return shortest wave start position
@ -597,16 +614,16 @@ int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr) {
//try lining up the peaks by moving starting point (try first few clocks)
for (ii=0; ii < loopCnt; ii++){
if (dest[ii] < peak && dest[ii] > low) continue;
if (dest[ii] < peak_hi && dest[ii] > peak_low) continue;
errCnt = 0;
// now that we have the first one lined up test rest of wave array
loopEnd = ((size-ii-tol) / clk[clkCnt]) - 1;
for (i=0; i < loopEnd; ++i){
arrLoc = ii + (i * clk[clkCnt]);
if (dest[arrLoc] >= peak || dest[arrLoc] <= low){
}else if (dest[arrLoc-tol] >= peak || dest[arrLoc-tol] <= low){
}else if (dest[arrLoc+tol] >= peak || dest[arrLoc+tol] <= low){
if (dest[arrLoc] >= peak_hi || dest[arrLoc] <= peak_low){
} else if (dest[arrLoc-tol] >= peak_hi || dest[arrLoc-tol] <= peak_low){
} else if (dest[arrLoc+tol] >= peak_hi || dest[arrLoc+tol] <= peak_low){
} else { //error no peak detected
errCnt++;
}
@ -701,9 +718,17 @@ int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx)
// size must be larger than 20 here
if (size < loopCnt) loopCnt = size-20;
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG DetectNZRClock: just noise detected - quitting");
return 0;
}
//get high and low peak
int peak, low;
if (getHiLo(dest, loopCnt, &peak, &low, 90, 90) < 1) return 0;
//getHiLo(dest, loopCnt, &peak, &low, 90, 90);
getHiLo(&peak, &low, 90, 90);
bool strong = false;
int lowestTransition = DetectStrongNRZClk(dest, size-20, peak, low, &strong);
@ -1431,11 +1456,18 @@ int cleanAskRawDemod(uint8_t *bits, size_t *size, int clk, int invert, int high,
//by marshmellow
//attempts to demodulate ask modulations, askType == 0 for ask/raw, askType==1 for ask/manchester
int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx) {
if (*size == 0) return -1;
int start = DetectASKClock(bits, *size, clk, maxErr); //clock default
int start = DetectASKClock(bits, *size, clk, maxErr);
if (*clk == 0 || start < 0) return -3;
if (*invert != 1) *invert = 0;
// amplify signal data.
// ICEMAN todo,
if (amp == 1) askAmp(bits, *size);
if (g_debugMode == 2) prnt("DEBUG ASK: clk %d, beststart %d, amp %d", *clk, start, amp);
//start pos from detect ask clock is 1/2 clock offset
@ -1443,11 +1475,18 @@ int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr,
*startIdx = start - (*clk/2);
uint16_t initLoopMax = 1024;
if (initLoopMax > *size) initLoopMax = *size;
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG askdemod_ext: just noise detected - quitting");
return -2;
}
// Detect high and lows
//25% clip in case highs and lows aren't clipped [marshmellow]
int high, low;
if (getHiLo(bits, initLoopMax, &high, &low, 75, 75) < 1)
return -2; //just noise
//getHiLo(bits, initLoopMax, &high, &low, 75, 75);
getHiLo(&high, &low, 75, 75);
size_t errCnt = 0;
// if clean clipped waves detected run alternate demod
@ -1461,11 +1500,13 @@ int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr,
uint8_t alignPos = 0;
errCnt = manrawdecode(bits, size, 0, &alignPos);
*startIdx += *clk/2 * alignPos;
if (g_debugMode)
prnt("DEBUG: (askdemod_ext) CLEAN: startIdx %i, alignPos %u", *startIdx, alignPos);
}
return errCnt;
}
if (g_debugMode) prnt("DEBUG: (askdemod_ext) Weak wave detected: startIdx %i", *startIdx);
int lastBit; //set first clock check - can go negative
@ -1530,8 +1571,17 @@ int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, int *invert, int *startId
if (gLen > *size)
gLen = *size-20;
// just noise - no super good detection. good enough
if (signalprop.isnoise) {
if (g_debugMode == 2) prnt("DEBUG nrzRawDemod: just noise detected - quitting");
return -3;
}
int high, low;
if (getHiLo(dest, gLen, &high, &low, 75, 75) < 1) return -3; //25% fuzz on high 25% fuzz on low
//getHiLo(dest, gLen, &high, &low, 75, 75);
getHiLo(&high, &low, 75, 75);
getHiLo(&high, &low, 75, 75);
uint8_t bit=0;
//convert wave samples to 1's and 0's

View file

@ -36,13 +36,6 @@ 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);
// buffer is unsigned on DEVIE
#ifdef ON_DEVICE
#define justNoise(a, b) isNoise((a), (b))
#else
#define justNoise(a, b) isNoise_int((a), (b))
#endif
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);
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low);
@ -58,14 +51,15 @@ extern uint32_t bytebits_to_byte(uint8_t *src, size_t numbits);
extern uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits);
extern uint16_t countFC(uint8_t *bits, size_t size, uint8_t fskAdj);
extern int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr);
extern uint8_t DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low);
extern bool DetectCleanAskWave(uint8_t *dest, size_t size, uint8_t high, uint8_t low);
extern uint8_t detectFSKClk(uint8_t *bits, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge);
extern int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx);
extern int DetectPSKClock(uint8_t *dest, size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc);
extern int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clock);
extern bool DetectST(uint8_t *buffer, size_t *size, int *foundclock, size_t *ststart, size_t *stend);
extern size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx);
extern int getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
//extern void getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
extern void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
extern uint32_t manchesterEncode2Bytes(uint16_t datain);
extern int ManchesterEncode(uint8_t *bits, size_t size);
extern int manrawdecode(uint8_t *bits, size_t *size, uint8_t invert, uint8_t *alignPos);