-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalProcessor.hpp
More file actions
89 lines (47 loc) · 1.95 KB
/
SignalProcessor.hpp
File metadata and controls
89 lines (47 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class SignalProcessor {
public:
SignalProcessor() {
}
// Find out delay between two signals using cross-correlation
// We assume delay is positive
static int GetDelay(int16_t* buffer1, int16_t* buffer2, size_t samplesQty) {
long long* correlation = new long long[samplesQty]();
for (unsigned int i = 0; i < samplesQty; i++) {
for (unsigned int j = 0; j < samplesQty; j++) {
//std::cout << buffer1[j] << " ";
correlation[i] += buffer1[j] * buffer2[(i+j < samplesQty)*(i+j)];
}
}
// Find correlation peak
unsigned int peak = 0;
long long maxValue = 0;
for (unsigned int i = 0; i < samplesQty; i++) {
if (correlation[i] > maxValue) {
maxValue = correlation[i];
peak = i;
}
}
delete[] correlation;
return peak;
}
// We assume delay is positive
static void Delay(int16_t* buffer, unsigned int samplesQty, int delay) {
for (int i = samplesQty-1; i >= 0; i--) {
buffer[i] = (i-delay >= 0)*buffer[(i-delay)];
}
}
// Scale a signal using integer math, with numerator and denumerator of scale
static void Scale(int16_t* buffer, size_t samplesQty, int num, int denum) {
for (int i = 0; i < samplesQty; i++) {
buffer[i] = buffer[i]*num/denum;
}
}
// Subtract buffer 2 from buffer 1
static void Subtract(int16_t* buffer1, int16_t* buffer2, size_t samplesQty) {
for (int i = 0; i < samplesQty; i++) {
buffer1[i] -= buffer2[i];
}
}
~SignalProcessor() {
}
};