寻找局部最大值在一个动态范围 [英] Finding Local Maxima Over a Dynamic Range

查看:140
本文介绍了寻找局部最大值在一个动态范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作在C#中,我需要找到双打列表中的所有局部峰值,并返回它们作为另一个列表双打。这似乎很简单,如果我有价值观,我在比较值的任何给定的窗口的一组数字,但我需要能够真正通过这个窗口大小到函数本身。这可能会造成混淆,但基本上我需要的是这样的:

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 comparing in any given 'window' of values, but I need to be able to actually pass this window size into the function itself. That may be confusing, but basically I need something like this:

public List<double> FindPeaks(List<double> values, double rangeOfPeaks)

,其中,如果'rangeOfPeaks'是5,当前值将比较2值在它每一侧,以确定它是否是一个峰或没有。如果'rangeOfPeaks'是11,当前值将与5比较值在每一侧。我认为这是一个pretty的基本算法,但是,我已经成功找到什么好的方法来检测这样的高峰。有没有人这样做过?任何帮助都将是AP preciated。在此先感谢!

where if 'rangeOfPeaks' was 5, the 'current' value would be compared to 2 values on each side of it to determine if it was a peak or not. If 'rangeOfPeaks' was 11, the current value would be compared to 5 values on each side. I'd think this was a pretty basic algorithm, however, I've been unsuccessful in finding any good methods for detecting a peak like this. Has anyone ever done this before? Any help at all would be appreciated. Thanks in advance!

推荐答案

有可能是更有效的方式,但LINQ使这pretty的直观

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

    static IList<double> FindPeaks(IList<double> values, int rangeOfPeaks)
    {
        List<double> peaks = new List<double>();

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

这篇关于寻找局部最大值在一个动态范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆