Beispiel #1
0
 // TouchDataPoint constructor that copies from another TouchDataPoint
 public TouchDataPoint(TouchDataPoint other)
 {
     this.t     = other.t;
     this.x     = other.x;
     this.y     = other.y;
     this.t_abs = other.t_abs;
 }
Beispiel #2
0
    void Update()
    {
        float timeSinceStart;

        if (recording)
        {
            if (Input.touches.Length > 1)
            {
                touchesRaw = new Vector2(Input.touches[1].position[0], Input.touches[1].position[1]);
            }
            else
            {
                touchesRaw = new Vector2(0, 0);
            }
        }
        else
        {
            if (Input.touches.Length == 0)
            {
                touchesRaw = new Vector2(0, 0);
            }
            else
            {
                touchesRaw = new Vector2(Input.touches[0].position[0], Input.touches[0].position[1]);
            }
        }

        touches     = touchesRaw - touchesLast; // the displacement between the current and the last touch coordinates
        touchesLast = touchesRaw;

        report = "Coordinates: " + touches + "\ntouch count: " + Input.touchCount + "\n";

        // Initialize low pass to first point
        if (touch_smoothed == null)
        {
            timeSinceStart = Time.realtimeSinceStartup;
            touch_smoothed = new TouchDataPoint(timeSinceStart, touches);
            touch_buffer.Add(new TouchDataPoint(touch_smoothed));
        }

        // Low pass filter to attenuate jitter. If we were double-integrating,
        // this would cause drift, but we're not.
        timeSinceStart = Time.realtimeSinceStartup;
        touch_smoothed = new TouchDataPoint(timeSinceStart,
                                            ALPHA * touches + (1 - ALPHA) *
                                            touch_smoothed.toVector());

        // Add this toucheration to our history buffer
        touch_buffer.Add(new TouchDataPoint(touch_smoothed));


        // Loop over history buffer, update time offsets of past touch values
        int i = 0;
        int n = touch_buffer.Count;

        while (i < n - 1)
        {
            touch_buffer[i].t -= Time.deltaTime;
            if (touch_buffer[i].t < LOOKBACK)
            {
                touch_buffer.RemoveAt(i);
                i--;
                n--;
            }
            i++;
        }

        if (recording)
        {
            record.Add(new TouchDataPoint(touch_smoothed));
            report += "\nrecording...";
            if (record[0].t_abs - Time.realtimeSinceStartup < LOOKBACK)
            {
                stopRecording();
            }
        }
        else
        {
            if (record.Count > 0)
            {
                updateAllTimes(record, record.Last().t_abs);
            }

            float  minDTW         = float.PositiveInfinity;
            string matchedPattern = "stationary"; // the key to patternDict

            //iterate through patternDict to get the best-matched pattern for the current data in touch_buffer
            foreach (KeyValuePair <string, List <TouchDataPoint> > item in patternDict)
            {
                float tempDTW = getDTW(touch_buffer, item.Value);
                if (tempDTW < minDTW)
                {
                    minDTW         = tempDTW;
                    matchedPattern = item.Key;
                }
            }

            if (minDTW < MATCH_THRESHOLD)
            {
                if (timeSinceDetected < DTW_RESULT_DURATION)
                {
                    report += "\n" + matchedPattern + "\nDTWscore:" + minDTW;
                    //pattern.lastRecognition = Time.realtimeSinceStartup;
                    timeSinceDetected += Time.deltaTime;
                }
                else
                {
                    timeSinceDetected = 0;
                    timeSinceStart    = 0; // CHECK REDUNDANCY
                }
            }
            else
            {
                report        += "\npattern not recognized";
                timeSinceStart = 0; // CHECK REDUNDANCY
            }
        }
    }