-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.cpp
142 lines (118 loc) · 3.71 KB
/
test.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <cstdio>
#include <vector>
#include "RingBuffer.h"
#include <cmath>
double gaussian(double v, double std_dev)
{
static double c = 1.0/sqrt(2.0*M_PI);
return (c/std_dev) * exp(-0.5*pow(v/std_dev,2));
}
int main()
{
const int nFreq = 8;
const int dist = 8;
const int length = 16;
const int BUFFERSIZE = nFreq*dist*length*2;
RingBuffer<double, BUFFERSIZE> buffer;
while(buffer.getNumberOfEntries() != buffer.getMaxEntries())
buffer.add(0);
RingBuffer<double, BUFFERSIZE> buffer2;
RingBuffer<double, 3> buffer3;
double max[nFreq] = { 0 };
RingBuffer<double, dist*length*10> maxBuffer[nFreq];
//FILE *f = fopen("mag2.txt", "r");
double mag;
int p = 0;
int t = 0;
while(fscanf(stdin, "%lf", &mag) > 0)
{
buffer.add(mag);
if(mag > max[p])
max[p] = mag;
maxBuffer[p].add(mag);
p = (p + 1) % nFreq;
//-- after each row (at each cycle)
if(p == 0)
{
t++;
//-- add all magnitudes at potential peak positions
double sum = 0;
for(int j=0; j<length; j++)
for(int f=0; f<nFreq; f++)
{
int p = (dist * j) * nFreq + f;
//sum += buffer[p] / max[nFreq-1-f];
double maxFromBuffer = 0;
for(int i=0;i<maxBuffer[nFreq-1-f].getNumberOfEntries(); i++)
if(maxFromBuffer < maxBuffer[nFreq-1-f][i])
maxFromBuffer = maxBuffer[nFreq-1-f][i];
sum += buffer[p] / maxFromBuffer;
}
buffer2.add(sum);
//-- search for 5 peaks (the tip of the triangle)
const int peaks = 5;
if(buffer2.getNumberOfEntries() > peaks/2 * dist + 2 + dist*(length-1) )
{
double v[peaks];
for(int i=0; i<peaks; i++)
v[i] = buffer2[dist*i];
//-- calc
double diff[peaks-1];
double diffCombined = 1.0;
for(int i=0; i<peaks-1; i++)
diffCombined *= diff[i] = (v[i] - v[i+1]) * (i<peaks/2 ? -1.0 : 1.0);
//-- evaluate symmetry
double error = 0;
for(int i=0; i<peaks/2-1; i++)
error += fabs(diff[i] - diff[peaks-2-i]);
double confidence = std::max(0.0, gaussian(error, 0.5) * diffCombined);
buffer3.add(confidence);
//printf("%lf\n", confidence);
bool failed = false;
if(buffer3[1] > buffer3[0] && buffer3[1] > buffer3[2] && buffer3[1] > 0.5)
{
int startPos = peaks/2 * dist + 2 + dist*(length-1);
printf("message position %d\n", t - startPos);
char data[length / 2 + 1] = { 0 };
for(int i=0; i<length; i++)
{
int index1 = -1, index2 = -1;
double max1 = 0, max2 = -1;
for(int f=0; f<nFreq; f++)
{
int x = startPos*nFreq - i*dist*nFreq - f - 1;
double v = buffer[x];
if(max1 < max2 && v > max1)
{
max1 = v;
index1 = f;
}
else if(max2 < max1 && v > max2)
{
max2 = v;
index2 = f;
}
}
//printf("%d %d %lf %lf \n", index1, index2, max1, max2);
if((index1 < nFreq/2) == (index2 < nFreq/2))
{
fprintf(stderr, "error!\n");
failed = true;
}
int row = std::min(index1, index2);
int col = std::max(index1, index2) - (nFreq/2);
int tone = (row << 2) | col;
//printf("tone %d\n", tone);
data[i/2] |= tone << (((i+1) & 1) * 4);
}
if(!failed)
fprintf(stderr, "%s\n", data);
}
}
else
printf("0\n");
}
}
//fclose(f);
return 0;
}