+l = sign(sin(cumsum(l)));\r
+h = sign(sin(cumsum(h)));\r
+ */\r
+\r
+// 2M*16/134.2k = 238\r
+ static const int LowTone[] = {\r
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1\r
+ };\r
+// 2M*16/123.2k = 260\r
+ static const int HighTone[] = {\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1,\r
+ 1, 1, 1, 1, 1, 1, 1, 1\r
+ };\r
+ int lowLen = sizeof(LowTone)/sizeof(int);\r
+ int highLen = sizeof(HighTone)/sizeof(int);\r
+ int convLen = (highLen>lowLen)?highLen:lowLen;\r
+ WORD crc;\r
+ int i, j, TagType;\r
+ int lowSum = 0, highSum = 0;;\r
+ int lowTot = 0, highTot = 0;\r
+\r
+ for(i = 0; i < GraphTraceLen - convLen; i++) {\r
+ lowSum = 0;\r
+ highSum = 0;;\r
+\r
+ for(j = 0; j < lowLen; j++) {\r
+ lowSum += LowTone[j]*GraphBuffer[i+j];\r
+ }\r
+ for(j = 0; j < highLen; j++) {\r
+ highSum += HighTone[j]*GraphBuffer[i+j];\r
+ }\r
+ lowSum = abs((100*lowSum) / lowLen);\r
+ highSum = abs((100*highSum) / highLen);\r
+ lowSum = (lowSum<0)?-lowSum:lowSum;\r
+ highSum = (highSum<0)?-highSum:highSum;\r
+\r
+ GraphBuffer[i] = (highSum << 16) | lowSum;\r
+ }\r
+\r
+ for(i = 0; i < GraphTraceLen - convLen - 16; i++) {\r
+ lowTot = 0;\r
+ highTot = 0;\r
+ // 16 and 15 are f_s divided by f_l and f_h, rounded\r
+ for(j = 0; j < 16; j++) {\r
+ lowTot += (GraphBuffer[i+j] & 0xffff);\r
+ }\r
+ for(j = 0; j < 15; j++) {\r
+ highTot += (GraphBuffer[i+j] >> 16);\r
+ }\r
+ GraphBuffer[i] = lowTot - highTot;\r
+ }\r
+\r
+ GraphTraceLen -= (convLen + 16);\r
+\r
+ RepaintGraphWindow();\r
+\r
+ // TI tag data format is 16 prebits, 8 start bits, 64 data bits,\r
+ // 16 crc CCITT bits, 8 stop bits, 15 end bits\r
+\r
+ // the 16 prebits are always low\r
+ // the 8 start and stop bits of a tag must match\r
+ // the start/stop prebits of a ro tag are 01111110\r
+ // the start/stop prebits of a rw tag are 11111110\r
+ // the 15 end bits of a ro tag are all low\r
+ // the 15 end bits of a rw tag match bits 15-1 of the data bits\r
+\r
+ // Okay, so now we have unsliced soft decisions;\r
+ // find bit-sync, and then get some bits.\r
+ // look for 17 low bits followed by 6 highs (common pattern for ro and rw tags)\r
+ int max = 0, maxPos = 0;\r
+ for(i = 0; i < 6000; i++) {\r
+ int j;\r
+ int dec = 0;\r
+ // searching 17 consecutive lows\r
+ for(j = 0; j < 17*lowLen; j++) {\r
+ dec -= GraphBuffer[i+j];\r
+ }\r
+ // searching 7 consecutive highs\r
+ for(; j < 17*lowLen + 6*highLen; j++) {\r
+ dec += GraphBuffer[i+j];\r
+ }\r
+ if(dec > max) {\r
+ max = dec;\r
+ maxPos = i;\r
+ }\r
+ }\r
+\r
+ // place a marker in the buffer to visually aid location\r
+ // of the start of sync\r