public double Compare(PoseModel other) { NDtw.Dtw dtw; double[][] firstSeries; double[][] secondSeries; double error = 0; char[] components = { 'x', 'y', 'z', 'w' }; foreach (char component in components) { firstSeries = GetAllComponents(component); secondSeries = other.GetAllComponents(component); for (int i = 0; i < numberOfJoints; i++) { dtw = new NDtw.Dtw(firstSeries[i], secondSeries[i]); error += dtw.GetCost(); } } return(error); }
void waveSource_DataAvailable(object sender, WaveInEventArgs e) { //int max = 0; byte[] buffer = e.Buffer; int bytesRecorded = e.BytesRecorded; //WriteToFile(buffer, bytesRecorded); for (int index = 0; index < e.BytesRecorded; index += 2) { short sample = (short)((buffer[index + 1] << 8) | buffer[index + 0]); float sample32 = (sample / 32768f); //int finalSample =(sample32 * 255); //if (max < finalSample) max = finalSample; //sampleAggregator.Add(sample32); queue.Enqueue(sample32); //queue.print(); } long curTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond; if (lastDetect + 2 > curTime) { Console.WriteLine("LAST GEST" + lastDetect + 5 + " " + curTime); return; } //check if sample matches int gest = 0; double lastcost = 1000; if (isSaved1 /*&& (loop++%3==0)*/){ long curTimeMs1 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; //Time Calc var cost = new Dtw ( saved1 , queue.ToArray2()).GetCost(); long curTimeMs2 = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; //Time Calc if (cost < lastcost && cost < thresh1) { gest = 1; lastcost = cost; Console.WriteLine("G1: " + cost); } //Debug.WriteLineIf( cost<thresh1, "1:"+cost); Console.WriteLine("Time DTW Took for Gesture 1 in ms: " + ( curTimeMs2 - curTimeMs1)); //Console.WriteLine("MS 2: " + curTimeMs2); } if (isSaved2 /*&& (loop++%3==0)*/) { var cost = new Dtw(saved2, queue.ToArray2()).GetCost(); if (cost < lastcost && cost < thresh2) { gest = 2; lastcost = cost; Console.WriteLine("G2: " + cost); } //Debug.WriteLineIf(cost < thresh2, "2:"+cost); } if (isSaved3 /*&& (loop++%3==0)*/) { var cost = new Dtw(saved3, queue.ToArray2()).GetCost(); if (cost < lastcost && cost < thresh3) { gest = 3; lastcost = cost; Console.WriteLine("G3: "+cost); } //Debug.WriteLineIf(cost < thresh3, "3:"+cost); } foreach (double[] s in falseSaved) { var cost = new Dtw(s, queue.ToArray2()).GetCost(); if (cost < lastcost && cost < thresh0) { gest = 0; lastcost = cost; } } thresh1 = Convert.ToDouble("0" + txtThres1.Text); thresh2 = Convert.ToDouble("0"+txtThres2.Text ); thresh3 = Convert.ToDouble("0" + txtThres3.Text); if (gest > 0) { lastDetect = DateTime.Now.Ticks / TimeSpan.TicksPerSecond; lastGesture = gest; Debug.WriteLine("DETECTED " + gest + " " + lastcost); } if (gest==1) { lbl1.Visible = true; lbl2.Visible = false; lbl3.Visible = false; SerialForm.SendLeft(); ArrowForm.ShowLeft(); } else if (gest==2) { lbl1.Visible = false; lbl2.Visible = true; lbl3.Visible = false; SerialForm.SendCenter(); ArrowForm.ShowCenter(); } else if(gest==3) { lbl1.Visible = false; lbl2.Visible = false; lbl3.Visible = true; SerialForm.SendRight(); ArrowForm.ShowRight(); } else { lbl1.Visible = false; lbl2.Visible = false; lbl3.Visible = false; ArrowForm.ShowNone(); } //else Debug.WriteLine("NONE"); //Debug.WriteLine(max.ToString()); }
public override double GetDistance(AudioFeature f, AudioFeature.DistanceType t) { if(!(f is Scms)) { new Exception("Can only handle AudioFeatures of type Scms, not of: "+f); return -1; } Scms other = (Scms)f; DistanceMeasure distanceMeasure = DistanceMeasure.Euclidean; switch (t) { case AudioFeature.DistanceType.Dtw_Euclidean: distanceMeasure = DistanceMeasure.Euclidean; break; case AudioFeature.DistanceType.Dtw_SquaredEuclidean: distanceMeasure = DistanceMeasure.SquaredEuclidean; break; case AudioFeature.DistanceType.Dtw_Manhattan: distanceMeasure = DistanceMeasure.Manhattan; break; case AudioFeature.DistanceType.Dtw_Maximum: distanceMeasure = DistanceMeasure.Maximum; break; case AudioFeature.DistanceType.UCR_Dtw: return UCRCSharp.UCR.DTW(this.GetArray(), other.GetArray()); case AudioFeature.DistanceType.CosineSimilarity: return CosineSimilarity(this, other); case AudioFeature.DistanceType.BitStringHamming: return Imghash.ImagePHash.HammingDistance(this.BitString, other.BitString); case AudioFeature.DistanceType.JaqSimilarity: //return Soundfingerprinting.Hashing.MinHash.CalculateJaqSimilarity(this.Signature, other.Signature); break; case AudioFeature.DistanceType.KullbackLeiblerDivergence: default: return Distance(this, other, new ScmsConfiguration(Analyzer.MFCC_COEFFICIENTS)); } Dtw dtw = new Dtw(this.GetArray(), other.GetArray(), distanceMeasure, true, true, null, null, null); return dtw.GetCost(); }
public void SetData(Dtw dtw) { dtwGraph.Dtw = dtw; }
/// <summary> /// Generate average of supplied series. /// </summary> public static double[] Average(List<double[]> series, int maxIterations = 100) { if (series == null || series.Count == 0) throw new Exception("series null or empty"); if (series.Count == 1) return series[0]; if (series.Select(x => x.Length).Distinct().Count() != 1) throw new Exception("Series must be of equal length"); int length = series[0].Length; //initializing the average to a simple mean of every point, without DTW //double[] average = new double[length]; //for (int i = 0; i < series[0].Length; i++) //{ // average[i] = series.Average(x => x[i]); //} //initialize to a random series from the input //var r = new Random(); //double[] average = series[r.Next(0, series.Count - 1)]; //initialize to series closest to median min/max after detrending List<double[]> tempSeries = series.Select(Detrend).ToList(); List<int> maxIndexes = tempSeries.Select(x => x.IndexOfMax()).ToList(); List<int> minIndexes = tempSeries.Select(x => x.IndexOfMin()).ToList(); double medianMaxIndex = maxIndexes.Median(); double medianMinIndex = minIndexes.Median(); var distances = maxIndexes.Select((x, i) => Math.Pow(x - medianMaxIndex, 2) + Math.Pow(minIndexes[i] - medianMinIndex, 2)).ToList(); int selectedSeries = distances.IndexOfMin(); double[] average = series[selectedSeries]; //this list will hold the values of each aligned point, //later used to construct the aligned average List<double>[] points = new List<double>[length]; for (int i = 0; i < length; i++) { points[i] = new List<double>(); } double prevTotalDist = -1; double totalDist = -2; //sometimes the process gets "stuck" in a loop between two different states //so we have to set a hard limit to end the loop int count = 0; //get the path between each series and the average while (totalDist != prevTotalDist && count < maxIterations) { prevTotalDist = totalDist; //clear the points from the last calculation foreach (var list in points) { list.Clear(); } //here we do the alignment for every series foreach (double[] ts in series) { var dtw = new Dtw(new[] { new SeriesVariable(ts, average) }); Tuple<int, int>[] path = dtw.GetPath(); //use the path to distribute the points according to the warping Array.ForEach(path, x => points[x.Item2].Add(ts[x.Item1])); } //Then simply construct the new average series by taking the mean of every List in points. average = points.Select(x => x.Average()).ToArray(); //calculate Euclidean distance to stop the loop if no further improvement can be made double[] average1 = average; totalDist = series.Sum(x => x.Select((y, i) => Math.Pow(y - average1[i], 2)).Sum()); //we get convergence even though there's still work to be done count++; } return average; }
public static double DistanceFunc(IEnumerable<double> vector1, IEnumerable<double> vector2) { Dtw analyser = new Dtw(vector1.ToArray(), vector2.ToArray(), DistanceMeasure.SquaredEuclidean); return analyser.GetCost(); }
private static void TestNDtw() { bool UseBoundaryConstraintStart = true; bool UseBoundaryConstraintEnd = true; Dtw dtw = new Dtw( new[] { 4.0, 4.0, 4.5, 4.5, 5.0, 5.0, 5.0, 4.5, 4.5, 4.0, 4.0, 3.5 }, new[] { 1.0, 1.5, 2.0, 2.5, 3.5, 4.0, 3.0, 2.5, 2.0, 2.0, 2.0, 1.5 }, DistanceMeasure.Euclidean, UseBoundaryConstraintStart, UseBoundaryConstraintEnd, null, null, null); double cost = dtw.GetCost(); Console.Out.WriteLine(String.Format("DTW: {0}", cost)); System.Console.ReadLine(); return; }
private double CalculateCost(SignModel sm1, SignModel sm2) { var seriesVariables = new List<SeriesVariable>(); seriesVariables.Add( new SeriesVariable( sm1.H_horizantal.ToArray(), sm2.H_horizantal.ToArray())); seriesVariables.Add( new SeriesVariable( sm1.H_vertical.ToArray(), sm2.H_vertical.ToArray())); var seriesVariablesArray = seriesVariables.ToArray(); var dtw = new Dtw(seriesVariablesArray); m_dtw = dtw; return dtw.GetCost(); }
/// <summary>Get Distance</summary> /// <seealso cref="">comirva.audio.feature.AudioFeature#GetDistance(comirva.audio.feature.AudioFeature)</seealso> public override double GetDistance(AudioFeature f, AudioFeature.DistanceType t) { if(!(f is MandelEllis)) { new Exception("Can only handle AudioFeatures of type Mandel Ellis, not of: "+f); return -1; } MandelEllis other = (MandelEllis)f; DistanceMeasure distanceMeasure = DistanceMeasure.Euclidean; switch (t) { case AudioFeature.DistanceType.Dtw_Euclidean: distanceMeasure = DistanceMeasure.Euclidean; break; case AudioFeature.DistanceType.Dtw_SquaredEuclidean: distanceMeasure = DistanceMeasure.SquaredEuclidean; break; case AudioFeature.DistanceType.Dtw_Manhattan: distanceMeasure = DistanceMeasure.Manhattan; break; case AudioFeature.DistanceType.Dtw_Maximum: distanceMeasure = DistanceMeasure.Maximum; break; case AudioFeature.DistanceType.KullbackLeiblerDivergence: default: return KullbackLeibler(this.gmmMe, other.gmmMe) + KullbackLeibler(other.gmmMe, this.gmmMe); } Dtw dtw = new Dtw(this.GetArray(), other.GetArray(), distanceMeasure, true, true, null, null, null); return dtw.GetCost(); }