예제 #1
0
        public static double AreaUnderTheCurve(IList <double> xTime, IList <double> yIntensity)
        {
            //MathNet.Numerics.Interpolation.IInterpolation interpole = MathNet.Numerics.Interpolation.Interpolate.LinearBetweenPoints(xTime, yIntensity);

            try
            {
                MathNet.Numerics.Interpolation.IInterpolation interpole = new MathNet.Numerics.Interpolation.Algorithms.AkimaSplineInterpolation(xTime, yIntensity);
                //MathNet.Numerics.Interpolation.IInterpolation interpole = new MathNet.Numerics.Interpolation.Algorithms.CubicSplineInterpolation(xTime, yIntensity);
                double maxTime = 0;
                for (int i = 0; i < xTime.Count; i++)
                {
                    if (xTime[i] > maxTime && yIntensity[i] > 0.0)
                    {
                        maxTime = xTime[i];
                    }
                }
                if (interpole.SupportsIntegration && maxTime > 0)
                {
                    return(interpole.Integrate(maxTime));
                }
            }catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(0);
            //MathNet.Numerics.Interpolation.IInterpolation interpole = MathNet.Numerics.Interpolation.Interpolate.(xTime, yIntensity);

            /*
             * double minTime = double.MaxValue;
             * double maxTime = double.MinValue;
             * foreach (double val in xTime)
             * {
             *  if (val > maxTime)
             *      maxTime = val;
             *  if (val < minTime)
             *      minTime = val;
             * }
             * return interpole.Integrate(maxTime);
             * /*
             * double iterSize = (maxTime - minTime) / 100.0;
             * for(double i = minTime; i <= maxTime; i += iterSize)
             * {
             *  double localIntensity = interpole.Integrate(maxTime);Evaluate.Polynomial(timePoint, coefficients);
             *      if (localIntensity > 0)
             *          cumul += localIntensity * iterSize;
             *      //else
             *      //    break;
             *  }
             * }
             * return cumul;//*/
        }
        public TraceFeatures(ICollection <Touch> touches)
        {
            var           trace      = touches.Where(t => t.FingerId == touches.First().FingerId);
            long          startTime  = trace.First().Time;
            List <double> timePoints = trace.Select(t => (double)(t.Time - startTime)).ToList();
            List <double> xValues    = trace.Select(t => (double)t.X).ToList();
            List <double> yValues    = trace.Select(t => (double)t.Y).ToList();


            var X = new double[trace.Count()];
            var Y = new double[trace.Count()];

            Time = new int[trace.Count()];
            var ind = 0;

            foreach (var touch in trace)
            {
                X[ind]    = (double)touch.X;
                Y[ind]    = (double)touch.Y;
                Time[ind] = (int)(touch.Time - startTime);
                ind++;
            }

            var isNotDistinct = timePoints.GroupBy(n => n).Any(c => c.Count() > 1);

            if (isNotDistinct)
            {
                return;
            }

            //do interpolation stuff
            var splineX = new MathNet.Numerics.Interpolation.Algorithms.AkimaSplineInterpolation(timePoints, xValues);
            var splineY = new MathNet.Numerics.Interpolation.Algorithms.AkimaSplineInterpolation(timePoints, yValues);

            var fft = new MathNet.Numerics.IntegralTransforms.Algorithms.DiscreteFourierTransform();

            //fft.BluesteinForward()

            //var splineX = new MathNet.Numerics.Interpolation.Algorithms.CubicSplineInterpolation(timePoints, xValues);
            //var splineY = new MathNet.Numerics.Interpolation.Algorithms.CubicSplineInterpolation(timePoints, yValues);

            /*
             * int nSteps = (int)timePoints.Last() / 10;
             *
             * var XInt = new double[nSteps];
             * var YInt = new double[nSteps];
             * var vX = new double[nSteps];
             * var vY = new double[nSteps];
             * TimeInterpolated = new int[nSteps];
             * //var aX = new double[nSteps];
             * //var aY = new double[nSteps];
             *
             *
             * for (int i = 0; i < nSteps; i++)
             * {
             *  XInt[i] = splineX.Interpolate(i * 10);
             *  YInt[i] = splineY.Interpolate(i * 10);
             *  vX[i] = splineX.Differentiate(i * 10);
             *  vY[i] = splineY.Differentiate(i * 10);
             *  TimeInterpolated[i] = (int)(i * 10);
             * }
             */

            /*
             * //init arrays
             * int n = touches.Count;
             * var dT = new int?[n];
             * var X = new decimal[n];
             * var Y = new decimal[n];
             * var dX = new decimal?[n];
             * var dY = new decimal?[n];
             * var ddX = new decimal?[n];
             * var ddY = new decimal?[n];
             * var StartDistance = new decimal[n];
             * var StartDirection = new decimal[n];
             * var Direction = new decimal?[n];
             * var Curvature = new decimal?[n];
             * var CurvX = new decimal?[n];
             * var CurvY = new decimal?[n];
             * var DirX = new decimal?[n];
             * var DirY = new decimal?[n];
             * var vX = new decimal?[n];
             * var vY = new decimal?[n];
             * var aX = new decimal?[n];
             * var aY = new decimal?[n];
             *
             * Time = new int[n];
             *
             * var startTime = touches.First().Time;
             *
             * int i = 0;
             * foreach(var t in touches)
             * {
             *  X[i] = t.X;
             *  Y[i] = t.Y;
             *
             *  Time[i] = (int)(t.Time - startTime);
             *
             *  dT[i] = (i == 0) ? null : (int?)(Time[i] - Time[i - 1]);
             *
             *  dX[i] = (i == 0) ? null : (decimal?)(X[i - 1] - X[i]);
             *  dY[i] = (i == 0) ? null : (decimal?)(Y[i - 1] - Y[i]);
             *
             *  ddX[i] = (i > 1) ? (decimal?)(dX[i-1] - dX[i]) : null;
             *  ddY[i] = (i > 1) ? (decimal?)(dY[i-1] - dY[i]) : null;
             *
             *  var dir = (dX[i].HasValue) ? (decimal?)Math.Atan2((double)dY[i], (double)dX[i]) : null;
             *  var curv = (ddX[i].HasValue) ? (decimal?)((double)(dX[i] * ddY[i] - dY[i] * ddX[i]) / ((dX[i] == 0 && dY[i] == 0) ? 0 : Math.Pow((double)(dX[i] * dX[i] + dY[i] * dY[i]), 1.5d))) : null;
             *
             *  var sdX = t.X - X[0];
             *  var sdY = t.Y - Y[0];
             *  var sdis = Math.Sqrt((double)(sdX * sdX + sdY * sdY));
             *  var sdir = Math.Atan2((double)sdY, (double)sdX);
             *
             *  Direction[i] = dir;
             *  Curvature[i] = curv;
             *  StartDistance[i] = (decimal)sdis;
             *  StartDirection[i] = (decimal)sdir;
             *
             *
             *  vX[i] = (i == 0) ? null : (decimal?)(dX[i] / dT[i]);
             *  vY[i] = (i == 0) ? null : (decimal?)(dY[i] / dT[i]);
             *
             *  aX[i] = (i < 2) ? null : (decimal?)(ddX[i] / dT[i]);
             *  aY[i] = (i < 2) ? null : (decimal?)(ddY[i] / dT[i]);
             *
             *  i++;
             * }
             */

            //create features and add to dictionary
            //var fX = new Feature("X", X);
            //var fY = new Feature("Y", Y);
            //var fXInt = new Feature("X interpolated", XInt);
            //var fYInt = new Feature("Y interpolated", YInt);
            //var fVx = new Feature("Velocity X", vX);
            //var fVy = new Feature("Velocity Y", vY);

            /*
             * var fStartDistance = new Feature("StartDistance", StartDistance);
             * var fStartDirection = new Feature("StartDirection", StartDirection);
             * var fDirection = new Feature("Direction", Direction.SkipWhile(d=>!d.HasValue).Cast<decimal>().ToArray());
             * var fCurvature = new Feature("Curvature", Curvature.SkipWhile(d => !d.HasValue).Cast<decimal>().ToArray());
             * var fVx = new Feature("Velocity X", vX.SkipWhile(d => !d.HasValue).Cast<decimal>().ToArray());
             * var fVy = new Feature("Velocity Y", vY.SkipWhile(d => !d.HasValue).Cast<decimal>().ToArray());
             * var fAx = new Feature("Acceleration X", aX.SkipWhile(d => !d.HasValue).Cast<decimal>().ToArray());
             * var fAy = new Feature("Acceleration Y", aY.SkipWhile(d => !d.HasValue).Cast<decimal>().ToArray());
             */

            this["X"] = new Feature("X", splineX);
            this["Y"] = new Feature("Y", splineY);

            //this[fX.Name] = fX;
            //this[fY.Name] = fY;
            //this[fXInt.Name] = fXInt;
            //this[fYInt.Name] = fYInt;
            //this[fVx.Name] = fVx;
            //this[fVy.Name] = fVy;

            /*
             * this[fAx.Name] = fAx;
             * this[fAy.Name] = fAy;
             * this[fStartDistance.Name] = fStartDistance;
             * this[fStartDirection.Name] = fStartDirection;
             * this[fDirection.Name] = fDirection;
             * this[fCurvature.Name] = fCurvature;
             */
        }
 public Feature(string name, MathNet.Numerics.Interpolation.Algorithms.AkimaSplineInterpolation spline)
 {
     Name   = name;
     Spline = spline;
 }