]>
Commit | Line | Data |
---|---|---|
1 | #include <data_operations.h> | |
2 | ||
3 | extern void PrintAndLog(char *fmt, ...); | |
4 | ||
5 | int autoCorr(const int* in, int *out, size_t len, int window) | |
6 | { | |
7 | static int CorrelBuffer[MAX_GRAPH_TRACE_LEN]; | |
8 | ||
9 | if (window == 0) { | |
10 | PrintAndLog("needs a window"); | |
11 | return 0; | |
12 | } | |
13 | if (window >= len) { | |
14 | PrintAndLog("window must be smaller than trace (%d samples)", | |
15 | len); | |
16 | return 0; | |
17 | } | |
18 | ||
19 | PrintAndLog("performing %d correlations", len - window); | |
20 | ||
21 | for (int i = 0; i < len - window; ++i) { | |
22 | int sum = 0; | |
23 | for (int j = 0; j < window; ++j) { | |
24 | sum += (in[j]*in[i + j]) / 256; | |
25 | } | |
26 | CorrelBuffer[i] = sum; | |
27 | } | |
28 | //GraphTraceLen = GraphTraceLen - window; | |
29 | memcpy(out, CorrelBuffer, len * sizeof (int)); | |
30 | return 0; | |
31 | } | |
32 | int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t down) | |
33 | { | |
34 | int lastValue = in[0]; | |
35 | out[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in. | |
36 | ||
37 | for (int i = 1; i < len; ++i) { | |
38 | // Apply first threshold to samples heading up | |
39 | if (in[i] >= up && in[i] > lastValue) | |
40 | { | |
41 | lastValue = out[i]; // Buffer last value as we overwrite it. | |
42 | out[i] = 1; | |
43 | } | |
44 | // Apply second threshold to samples heading down | |
45 | else if (in[i] <= down && in[i] < lastValue) | |
46 | { | |
47 | lastValue = out[i]; // Buffer last value as we overwrite it. | |
48 | out[i] = -1; | |
49 | } | |
50 | else | |
51 | { | |
52 | lastValue = out[i]; // Buffer last value as we overwrite it. | |
53 | out[i] = out[i-1]; | |
54 | } | |
55 | } | |
56 | out[0] = out[1]; // Align with first edited sample. | |
57 | return 0; | |
58 | } |