public AccelDataPoint(AccelDataPoint other) { this.t = other.t; this.x = other.x; this.y = other.y; this.z = other.z; this.t_abs = other.t_abs; }
void Update() { float timeSinceStart; accel = Input.acceleration; // Initialize low pass to first point if (accel_smoothed == null) { timeSinceStart = Time.realtimeSinceStartup; accel_smoothed = new AccelDataPoint(timeSinceStart, accel); accel_buffer.Add(new AccelDataPoint(accel_smoothed)); } // Low pass filter to attenuate jitter. If we were double-integrating, // this would cause drift, but we're not. timeSinceStart = Time.realtimeSinceStartup; accel_smoothed = new AccelDataPoint(timeSinceStart, ALPHA * accel + (1 - ALPHA) * accel_smoothed.toVector()); // Add this acceleration to our history buffer accel_buffer.Add(new AccelDataPoint(accel_smoothed)); // Loop over history buffer, update time offsets of past accel values int i = 0; int n = accel_buffer.Count; while (i < n - 1) { accel_buffer[i].t -= Time.deltaTime; if (accel_buffer[i].t < LOOKBACK) { accel_buffer.RemoveAt(i); i--; n--; } i++; } if (recording) { record.Add(new AccelDataPoint(accel_smoothed)); report = "recording..."; 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 accel_buffer foreach (KeyValuePair <string, List <AccelDataPoint> > item in patternDict) { float tempDTW = getDTW(accel_buffer, item.Value); if (tempDTW < minDTW) { minDTW = tempDTW; matchedPattern = item.Key; } } if (minDTW < MATCH_THRESHOLD) { if (timeSinceDetected < 2.0f) { report = matchedPattern + "\nDTWscore:" + minDTW; //pattern.lastRecognition = Time.realtimeSinceStartup; timeSinceDetected += Time.deltaTime; } else { timeSinceDetected = 0; timeSinceStart = 0; // CHECK REDUNDANCY } } else { report = ""; //report = "pattern not recognized"; timeSinceStart = 0; // CHECK REDUNDANCY } } }