-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonar.cpp
164 lines (137 loc) · 4.22 KB
/
sonar.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "sonar.h"
SONAR::SONAR()
{
qDebug() << "Sonar started";
QAudioFormat format;
format.setSampleRate(48000);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
inputPlayer.initializeAudio(format);
outputPlayer.initializeAudio(format);
max_time = 2 * max_distance / (1.0 * air_speed);
float desired_length = format.sampleRate() * max_time;
sample_desired_length = desired_length; // to int truncature
}
QList<float> SONAR::getDistance(QList<float> data)
{
QList<float> distances;
if (data.length() < sound.length())
{
qWarning() << "Sound lenght should be smaller than data. Data lenght : " << data.length() << " Sound length : " << sound.length();
return distances;
}
float max = 0;
for (int i = 0; i < data.length() - sound.length(); i++)
{
// get max pos
int max_pos = sound.length();
if (max_pos > data.length() - i)
max_pos = data.length() - i;
float distance = 0;
for (int pos = 0; pos < sound.length(); pos++)
{
distance += abs(data[i + pos] * sound[pos]);
}
if (distance > max)
max = distance;
if (distanceCompensation)
distance *= i / 1000.0;
if (distanceMediumRemoval)
{
if (distanceInitialisationRemaining > 0)
{
float a = distance / distanceInitialisations;
if (distanceThresholdList.length() - 1 < i) // does the i element exist ?
distanceThresholdList.append(a);
else
distanceThresholdList[i] += a;
}
else
{
distance -= distanceThresholdList[i];
}
}
distances.append(distance);
}
// distance medium removal control
if (distanceMediumRemoval)
{
if (distanceInitialisationRemaining > 0)
{
qDebug() << "Distance initialisationRemainig : " << distanceInitialisationRemaining;
distanceInitialisationRemaining--;
}
else
{
if (distanceInitialisationDone == false)
{
distanceInitialisationDone = true;
qDebug() << "Distance initialisation done";
}
}
}
return distances;
}
int SONAR::getStart(QList<float> data)
{
const float a = 0.8; // coef
const float threshold = 0.4; //
const int delta = 50; // since we detect the singla while the signal has already risen (at the max) we have to go back at the begin of the perturbation
float s = 0;
for (int i = 0; i < data.length(); i++)
{
s = data[i] * a + s * (1 - a);
//qDebug() << s;
if (s > threshold)
{
if (i > delta)
{
return i - delta;
}
else
{
return i;
}
}
}
return 0;
}
void SONAR::startSound()
{
sound = chrip.getChrip();
inputPlayer.Record(800); // 300 ms
outputPlayer.Play(sound);
}
sonarData SONAR::getResults()
{
sonarData snData;
snData.signal = inputPlayer.getRecording();
if(snData.signal.length() < sample_desired_length){ // invalid data, we fill until we have enough data
for(int pos= snData.signal.length(); pos < sample_desired_length; pos++)
snData.signal.append(0);
}
else{
// we have valid data, we can analyse it
// detect the start and remove first elements
/*
We do this to have allways the same array, begin at the same time
We truncate the end to have always the same lenght
*/
int start = getStart(snData.signal);
for (int i = 0; i < start; i++)
{
snData.signal.removeFirst();
}
for (int i = start + sample_desired_length; i < snData.signal.length(); i++)
{
snData.signal.removeLast();
}
}
snData.distance = getDistance(snData.signal);
// test if playback work
//outputPlayer.Play(data);
return snData;
}