public override void Calculate()
        {
            Times = Generate.LinearSpaced(base.Samples, base.Start, base.Start + base.Duration);

            PolyFit.FitAndGetPoints(UserTimes, UserValues, Order, Samples, Times, out double[] caculatedVariables);
            values = caculatedVariables;
        }
Exemplo n.º 2
0
 public DosingInterval(List <float> time, List <float> conc, PKCalculationOptions options)
 {
     _options       = options;
     _time          = time;
     _concentration = conc;
     _timeSteps     = ArrayHelper.TimeStepsFrom(time);
     _polyFit       = new PolyFit();
 }
Exemplo n.º 3
0
 public PKInterval(List <float> time, List <float> concentration, PKCalculationOptions options, double?drugMassPerBodyWeight = null, double?concentrationThreshold = null)
 {
     _options = options;
     DrugMassPerBodyWeight = drugMassPerBodyWeight;
     _time                   = time;
     _concentration          = concentration;
     _concentrationThreshold = concentrationThreshold;
     _timeSteps              = ArrayHelper.TimeStepsFrom(time);
     _polyFit                = new PolyFit();
 }
Exemplo n.º 4
0
    /// <summary> Use LU Decomposition to approximate arc length and inverse arc length functions for faster computation. They will be used in the XML navigation file. </summary>
    /// <returns>double[nbLanes,nbAnchors-1,2,3] with 2 being (arcLength,invArcLength) and 3 being(x^3,x^2,x)</returns>
    public static double[,,,] ArcLengthApproximations(QuadraticPolynomial[,] lanePolynoms)
    {
        var arcLengthApproximations = new double[lanePolynoms.GetLength(0), lanePolynoms.GetLength(1), 2, 3];
        var steps  = new double[11];
        var values = new double[11];

        for (int j = 0; j < lanePolynoms.GetLength(0); j++)
        {
            for (int k = 0; k < lanePolynoms.GetLength(1); k++)
            {
                for (var i = 0; i < 11; i++)
                {
                    steps[i]  = i / 10d;
                    values[i] = lanePolynoms[j, k].ArcLength(0.0, steps[i]);
                }

                if (lanePolynoms[j, k]._coeffs[0, 0] < 0.0001 && lanePolynoms[j, k]._coeffs[0, 1] < 0.0001 && lanePolynoms[j, k]._coeffs[0, 2] < 0.0001)
                {
                    arcLengthApproximations[j, k, 0, 0] = 0;
                    arcLengthApproximations[j, k, 1, 0] = 0;
                    arcLengthApproximations[j, k, 2, 0] = lanePolynoms[j, k].ArcLength(0.0, 1.0);

                    arcLengthApproximations[j, k, 0, 1] = 0;
                    arcLengthApproximations[j, k, 1, 1] = 0;
                    arcLengthApproximations[j, k, 2, 1] = 1 / arcLengthApproximations[j, k, 0, 2];
                }
                else
                {
                    var aLen = new PolyFit(steps, values, 3);
                    arcLengthApproximations[j, k, 0, 0] = aLen.Coeff[3];
                    arcLengthApproximations[j, k, 1, 0] = aLen.Coeff[2];
                    arcLengthApproximations[j, k, 2, 0] = aLen.Coeff[1];

                    var invaLen = new PolyFit(values, steps, 3);
                    arcLengthApproximations[j, k, 0, 1] = invaLen.Coeff[3];
                    arcLengthApproximations[j, k, 1, 1] = invaLen.Coeff[2];
                    arcLengthApproximations[j, k, 2, 1] = invaLen.Coeff[1];
                }
            }
        }

        return(arcLengthApproximations);
    }
Exemplo n.º 5
0
        public void BodePlot(ref List <List <float> > FrqDataList)
        {
            List <float>         FrqList      = new List <float>();
            List <float>         Ftest        = new List <float>();
            List <List <float> > BodeDataList = new List <List <float> >();

            BodeDataList.Add(new List <float>());
            BodeDataList.Add(new List <float>());
            BodeDataList.Add(new List <float>());
            //   int Index = 0;

            while (true)
            {
                if (FrqDataList[1][0] == 0)
                {
                    Ftest.Add(FrqDataList[0][0]);
                    for (int i = 0; i < 3; i++)
                    {
                        FrqDataList[i].RemoveAt(0);
                    }
                }
                else
                {
                    break;
                }
            }
            while (true)
            {
                int last = FrqDataList[1].Count - 1;
                if (FrqDataList[1][last] == 0)
                {
                    Ftest.Add(FrqDataList[0][last]);
                    for (int i = 0; i < 3; i++)
                    {
                        FrqDataList[i].RemoveAt(last);
                    }
                }
                else
                {
                    break;
                }
            }
            //float b = FrqDataList[2].Average();
            float b      = -0.175022362F;
            float DisAvg = Ftest.Average();
            //float K = -0.142118886F;
            int   DataLength = FrqDataList[0].Count;
            float DisturbanceMax = FrqDataList[1].Max(), DisturbanceMin = FrqDataList[1].Min();

            for (int i = 0; i < DataLength - 1; i++)
            {
                FrqList.Add(FrqDataList[0][i]);
                if (FrqDataList[2][i + 1] != FrqDataList[2][i] || i == DataLength - 1)
                {
                    double[] xx = new double[FrqList.Count];
                    double[] yy = new double[FrqList.Count];
                    for (int j = 0; j < FrqList.Count; j++)
                    {
                        xx[j] = j;
                        yy[j] = FrqList[j];
                    }
                    //FrqDataList[2].GetRange(PhaseStart, (int)PeriodTimes).CopyTo(yy);
                    var polyfit = new PolyFit(xx, yy, 5);
                    var fitted  = polyfit.Fit(xx);

                    BodeDataList[0].Add(FrqDataList[2][i]);
                    BodeDataList[1].Add((float)(20 * Math.Log10((fitted.Max() - fitted.Min()) / (DisturbanceMax - DisturbanceMin))));
                    FrqList.Clear();
                }
            }

            double PeriodTimes = 0, PhaseDelay = 0;
            int    TCMDMaxIndex = 0, TCMDMinIndex = 0, FRTCMMaxIndex = 0, FRTCMMinIndex, PhaseStart = 0, PhaseEnd = 0, cctest = 0;
            //Index = 0;
            List <float> TCMDList  = new List <float>();
            List <float> FRTCMList = new List <float>();

            for (int i = 0; i < DataLength - 1; i++)
            {
                if (FrqDataList[2][i + 1] != FrqDataList[2][i] || i == DataLength - 1)
                {
                    PeriodTimes = (1 / FrqDataList[2][i]) * 1000 * 4;
                    PhaseStart  = (i - (int)PeriodTimes + 1);
                    float    Amp = (FrqDataList[0].GetRange(PhaseStart, (int)PeriodTimes).Max() - FrqDataList[0].GetRange(PhaseStart, (int)PeriodTimes).Min()) / 2;
                    double[] xx  = new double[(int)PeriodTimes];
                    double[] yy  = new double[(int)PeriodTimes];
                    for (int j = 0; j < (int)PeriodTimes; j++)
                    {
                        xx[j] = j;
                        yy[j] = FrqDataList[0][j + PhaseStart];
                    }
                    //FrqDataList[2].GetRange(PhaseStart, (int)PeriodTimes).CopyTo(yy);
                    var polyfit = new PolyFit(xx, yy, 4);
                    var fitted  = polyfit.Fit(xx);

                    if ((FrqDataList[1][PhaseStart] >= 0 && fitted[0] >= 0) || (FrqDataList[1][PhaseStart] <= 0 && fitted[0] <= 0))
                    {
                        if (FrqDataList[1][PhaseStart] >= 0)
                        {
                            for (int j = PhaseStart; j < i + 1; j++)
                            {
                                if (FrqDataList[1][j] < 0)
                                {
                                    FRTCMMaxIndex = j;
                                    break;
                                }
                            }
                            for (int j = 0; j < (int)PeriodTimes; j++)
                            {
                                if (fitted[j] < DisAvg)
                                {
                                    TCMDMaxIndex = j + PhaseStart;
                                    break;
                                }
                            }
                        }
                        else if (FrqDataList[1][PhaseStart] <= 0)
                        {
                            for (int j = PhaseStart; j < i + 1; j++)
                            {
                                if (FrqDataList[1][j] > 0)
                                {
                                    FRTCMMaxIndex = j;
                                    break;
                                }
                            }
                            for (int j = 0; j < (int)PeriodTimes; j++)
                            {
                                if (fitted[j] > DisAvg)
                                {
                                    TCMDMaxIndex = j + PhaseStart;
                                    break;
                                }
                            }
                        }
                        PhaseDelay = (double)(FRTCMMaxIndex - TCMDMaxIndex) / PeriodTimes * 360;
                    }
                    else
                    {
                        if (FrqDataList[1][PhaseStart] >= 0)
                        {
                            for (int j = PhaseStart; j < i + 1; j++)
                            {
                                if (FrqDataList[1][j] < 0)
                                {
                                    FRTCMMaxIndex = j;
                                    break;
                                }
                            }
                            for (int j = 0; j < (int)PeriodTimes; j++)
                            {
                                if (fitted[j] > DisAvg)
                                {
                                    TCMDMaxIndex = j + PhaseStart;
                                    break;
                                }
                            }
                        }
                        else if (FrqDataList[1][PhaseStart] <= 0)
                        {
                            for (int j = PhaseStart; j < i + 1; j++)
                            {
                                if (FrqDataList[1][j] > 0)
                                {
                                    FRTCMMaxIndex = j;
                                    break;
                                }
                            }
                            for (int j = 0; j < (int)PeriodTimes; j++)
                            {
                                if (fitted[j] < DisAvg)
                                {
                                    TCMDMaxIndex = j + PhaseStart;
                                    break;
                                }
                            }
                        }
                        PhaseDelay = (double)(FRTCMMaxIndex - TCMDMaxIndex) / PeriodTimes * 360 - 180;
                    }
                    while (PhaseDelay < -330)
                    {
                        PhaseDelay += 360;
                    }
                    while (PhaseDelay > 30)
                    {
                        PhaseDelay += -360;
                    }
                    BodeDataList[2].Add((float)PhaseDelay);
                }
            }
            //for (int i = 0; i < DataLength - 1; i++)
            //{
            //    if (FrqDataList[2][i + 1] != FrqDataList[2][i] || i == DataLength - 1)
            //    {
            //        PeriodTimes = (1 / FrqDataList[2][i]) * 1000 * 4;

            //        double[] xx = new double[(int)PeriodTimes];
            //        double[] yy = new double[(int)PeriodTimes];
            //        for (int j = 0; j < (int)PeriodTimes; j++)
            //        {
            //            xx[j] = j;
            //            yy[j] = FrqDataList[0][j + PhaseStart];
            //        }

            //        var polyfit = new PolyFit(xx, yy, 10);
            //        var fitted = polyfit.Fit(xx);


            //        FRTCMMaxIndex = FrqDataList[1].GetRange(i, (int)PeriodTimes).IndexOf(FrqDataList[1].GetRange(i, (int)PeriodTimes).Max());
            //        FRTCMMinIndex = FrqDataList[1].GetRange(i, (int)PeriodTimes).IndexOf(FrqDataList[1].GetRange(i, (int)PeriodTimes).Min());
            //        TCMDMaxIndex = FrqDataList[2].GetRange(i, (int)PeriodTimes).IndexOf(FrqDataList[2].GetRange(i, (int)PeriodTimes).Max());
            //        TCMDMinIndex = FrqDataList[2].GetRange(i, (int)PeriodTimes).IndexOf(FrqDataList[2].GetRange(i, (int)PeriodTimes).Min());
            //        PhaseDelay = -(TCMDMaxIndex - FRTCMMaxIndex) / PeriodTimes * 360;
            //        if (PhaseDelay < -350)
            //            PhaseDelay += 360;
            //        else if (PhaseDelay > 10)
            //            PhaseDelay += -360;
            //        BodeDataList[2].Add((float)PhaseDelay);
            //    }
            //}

            FrqDataList.Clear();
            FrqDataList = BodeDataList;
        }