Finding Local Maxima Over a Dynamic Range

前端 未结 6 2118
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 19:17

Working in C#, I need to find all local peaks in a List of doubles and return them as another List doubles. This seems simple enough if I have a set number of values I\'m co

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 19:30

    There are probably more efficient ways but LINQ makes this pretty straightforward

        static IList FindPeaks(IList values, int rangeOfPeaks)
        {
            List peaks = new List();
    
            int checksOnEachSide = rangeOfPeaks / 2;
            for (int i = 0; i < values.Count; i++)
            {
                double current = values[i];
                IEnumerable range = values;
                if( i > checksOnEachSide )
                    range = range.Skip(i - checksOnEachSide);
                range = range.Take(rangeOfPeaks);
                if (current == range.Max())
                    peaks.Add(current);
            }
            return peaks;
        }
    

提交回复
热议问题