public int classify()
{
// Empirically determined threshold.
double thresh = .1;
// We are looking for bandwidth shifts both towards(-) and away(+) around the key tone.
bool towards = false;
bool away = false;
int velocityT = 0;
int velocityA = 0;
// Soundwave states to ignore the first 30-40 hz (~3 bins) because of random noise.
for (int i = 4; i < radius; i++)
{
// We look in both directions for velocities that exceed our thresh value.
// We then set the corresponding velocity_ variable to the maximum distance
// from the center that exceeds the threshold.
if (data[radius - i] > thresh)
{
velocityA = -1*(i - 3);
away = true;
}
if (data[radius + i] > thresh)
{
velocityT = i - 3;
towards = true;
}
}
// If there is movement in both directions, set the flag to true,
// and if the previous major velocity is in the same direction,
// assume that current one is still the gesture's intended direction.
if ((towards == true) && (away == true))
{
isBoth = true;
if (Math.Abs(velocityA) > velocityT)
{
if (prior > 0)
{
inverse_state = velocityA;
updateXY(velocityT);
return velocityT;
}
inverse_state = velocityT;
updateXY(velocityA);
return velocityA;
}
else
{
if (prior < 0)
{
inverse_state = velocityT;
updateXY(velocityA);
return velocityA;
}
inverse_state = velocityA;
updateXY(velocityT);
return velocityT;
}
}
// If only one direction is detected, return it.
if (towards)
{
updateXY(velocityT);
return velocityT;
}
if (away)
{
updateXY(velocityA);
return velocityA;
}
return 0;
}