//should be analogeus to matlab interp1 with method 'linear'
        public static void ResampleRBA <RealType>(IAlgebraReal <RealType> algebra, RealType[] old_sample_times, RealType[] old_values, RealType[] new_sample_times, RealType[] new_values)
        {
            int old_sample_count = old_sample_times.Length;
            int new_sample_count = new_sample_times.Length;

            int old_sample_index = 0;

            for (int new_sample_index = 0; new_sample_index < new_sample_count; new_sample_index++)
            {
                if (algebra.Compare(new_sample_times[new_sample_index], old_sample_times[0]) != 1)
                {
                    //extrapolate_before
                    new_values[new_sample_index] = old_values[0];
                }
                else
                {
                    if (algebra.Compare(old_sample_times[old_sample_count - 1], new_sample_times[new_sample_index]) != 1)
                    {
                        //extrapolate_before after
                        new_values[new_sample_index] = old_values[old_sample_count - 1];
                    }
                    else
                    {
                        //We are stricktly between samples
                        while (algebra.Compare(old_sample_times[old_sample_index], new_sample_times[new_sample_index]) == -1)
                        {
                            old_sample_index++;
                        }

                        new_values[new_sample_index] = Interpolation1DLinear(algebra, old_sample_times[old_sample_index - 1], old_values[old_sample_index - 1], old_sample_times[old_sample_index], old_values[old_sample_index], new_sample_times[new_sample_index]);
                    }
                }
            }
        }
        public static List <Tuple <int, int> > FindPeakIntervals <ValueType>(IAlgebraReal <ValueType> algebra, IFunction <int, ValueType> function, int min_index, int max_index, ValueType peak_rate)
        {
            int temp_index  = min_index - 1;
            int start_index = min_index - 1;
            int index       = min_index - 1;
            List <Tuple <int, int> > intervals = new List <Tuple <int, int> >();

            while (index < max_index)
            {
                if ((algebra.Compare(function.Compute(index + 1), algebra.Multiply(peak_rate, function.Compute(start_index))) == -1) &&
                    (algebra.Compare(function.Compute(temp_index), (algebra.Multiply(peak_rate, function.Compute(start_index)))) == 1))
                {
                    int peak_index = start_index;
                    while (algebra.Compare(function.Compute(peak_index - 1), (algebra.Multiply(peak_rate, function.Compute(start_index)))) != -1)
                    {
                        peak_index = peak_index - 1;
                    }
                    intervals.Add(new Tuple <int, int>(peak_index, index));
                }

                if ((algebra.Compare(function.Compute(index + 1), function.Compute(temp_index)) == -1) ||
                    (algebra.Compare(function.Compute(index + 1), algebra.Multiply(peak_rate, function.Compute(start_index))) == -1))
                {
                    temp_index  = index + 1;
                    start_index = index + 1;
                    index       = index + 1;
                }
                else if (algebra.Compare(function.Compute(index + 1), function.Compute(start_index)) == -1)
                {
                    start_index = index + 1;
                    index       = index + 1;
                }
                else
                {
                    index = index + 1;
                }
            }

            return(intervals);
        }