Click here to Skip to main content
15,888,461 members
Home / Discussions / C#
   

C#

 
QuestionPreferred connection to database, config or class? Pin
bjay tiamsic4-Feb-18 12:46
bjay tiamsic4-Feb-18 12:46 
AnswerRe: Preferred connection to database, config or class? Pin
OriginalGriff4-Feb-18 19:59
mveOriginalGriff4-Feb-18 19:59 
GeneralRe: Preferred connection to database, config or class? Pin
bjay tiamsic5-Feb-18 19:48
bjay tiamsic5-Feb-18 19:48 
GeneralRe: Preferred connection to database, config or class? Pin
Pete O'Hanlon5-Feb-18 20:06
mvePete O'Hanlon5-Feb-18 20:06 
AnswerRe: Preferred connection to database, config or class? Pin
Eddy Vluggen5-Feb-18 1:18
professionalEddy Vluggen5-Feb-18 1:18 
QuestionPeak Search Algorithm with sliding window and peak excursion Pin
Krellon31-Jan-18 22:46
Krellon31-Jan-18 22:46 
AnswerRe: Peak Search Algorithm with sliding window and peak excursion Pin
Pete O'Hanlon31-Jan-18 22:49
mvePete O'Hanlon31-Jan-18 22:49 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon31-Jan-18 23:13
Krellon31-Jan-18 23:13 
Hi Pete,

Thanks for getting back to me,

I have a modified peak search function that returns peak/troughs.

C#
public static IEnumerable<Tuple<int, double>> LocalPeakMinMax(IEnumerable<double> source, int windowSize)
{
    if (windowSize < 2) windowSize = 2;
    
    windowSize = windowSize - windowSize % 2 + 1;
    int halfWindow = windowSize / 2;
    int index = 0;
    var before = new Queue<double>(Enumerable.Repeat(double.NegativeInfinity, halfWindow));
    var after = new Queue<double>(source.Take(halfWindow + 1));

    foreach (double d in source.Skip(halfWindow + 1).Concat(Enumerable.Repeat(double.NegativeInfinity, halfWindow + 1)))
    {
        double curVal = after.Dequeue();
        if (before.All(x => curVal > x ) && after.All(x => curVal >= x ))
        {
            yield return Tuple.Create(index, curVal);
        }
        else 
        if (before.All(x => curVal <= x) && after.All(x => curVal < x))
        {
            yield return Tuple.Create(index, curVal);
        }

        before.Dequeue();
        before.Enqueue(curVal);
        after.Enqueue(d);
        index++;
        }
    }


The problem is I cannot figure out how best to filter out all the chaff and return the peaks that align with the prescribed logic explained in my original post.

I was going to take the output from this and play with it but it was getting messy. I was hoping the community may be able to come up with a more elegant solution. The code isn't slow at all, quite the opposite. At least with the 625 points i'm currently playing with.

Br
Nigel

modified 1-Feb-18 5:22am.

GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Pete O'Hanlon1-Feb-18 3:32
mvePete O'Hanlon1-Feb-18 3:32 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon1-Feb-18 4:45
Krellon1-Feb-18 4:45 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
OriginalGriff1-Feb-18 4:46
mveOriginalGriff1-Feb-18 4:46 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon1-Feb-18 5:02
Krellon1-Feb-18 5:02 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Pete O'Hanlon1-Feb-18 5:31
mvePete O'Hanlon1-Feb-18 5:31 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon1-Feb-18 6:30
Krellon1-Feb-18 6:30 
QuestionRe: Peak Search Algorithm with sliding window and peak excursion Pin
Eddy Vluggen31-Jan-18 23:06
professionalEddy Vluggen31-Jan-18 23:06 
AnswerRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon31-Jan-18 23:17
Krellon31-Jan-18 23:17 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Eddy Vluggen31-Jan-18 23:26
professionalEddy Vluggen31-Jan-18 23:26 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon1-Feb-18 0:20
Krellon1-Feb-18 0:20 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Eddy Vluggen1-Feb-18 1:37
professionalEddy Vluggen1-Feb-18 1:37 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon1-Feb-18 2:47
Krellon1-Feb-18 2:47 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Eddy Vluggen1-Feb-18 3:21
professionalEddy Vluggen1-Feb-18 3:21 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon1-Feb-18 4:07
Krellon1-Feb-18 4:07 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Eddy Vluggen1-Feb-18 9:25
professionalEddy Vluggen1-Feb-18 9:25 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Krellon1-Feb-18 12:44
Krellon1-Feb-18 12:44 
GeneralRe: Peak Search Algorithm with sliding window and peak excursion Pin
Eddy Vluggen1-Feb-18 13:45
professionalEddy Vluggen1-Feb-18 13:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.