Dan King
Dan King

Reputation: 51

I need to find all the peaks and valleys in a list of data points using c#

This is my entire program. I have been working on this project for a week now and I am simply stuck. I have managed to read the CSV file and put the information in two lists that I then converted into arrays. Now I don't HAVE to do it like this. It's just simply what I managed to write and work and make sense in my head. This is where I've managed to get to and where I'm stuck. All I need is to be able to parse out ALL the peaks, or maximums, and valleys, or minimums, from the data points, and display them. If I can get that to happen then I can finally be done with this program. Any help and suggestions, code, anything like that would greatly appreciate. Like I said if you have a better formula that I can plug in and use without hard-coding points (because there's over 2000 of them) and display the outcome in a listbox or something then that would make my day. Thank y, everyone,ne in advance

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        double firstX, firstY, secondX, secondY, thirdX, thirdY, rw, cl = 0.0;

        int count = 0;

        string x = "";
        string y = "";

        byte MinMax = 0; //0 equals rising, 1 equals falling

        double Max = new Array[];
        //double Min = new Array[];

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var reader = new StreamReader(@"D:\data.csv"))
            {
                List<string> listA = new List<string>();
                List<string> listB = new List<string>();

                while (!reader.EndOfStream)
                {

                    var line = reader.ReadLine();
                    var values = line.Split(',');

                    listA.Add(values[0]);
                    listB.Add(values[1]);

                    string[] xPoints = listA.ToArray();
                    string[] yPoints = listB.ToArray();

                    while (yPoints.Last() != null)
                    {
                        if (Convert.ToDouble(yPoints.First()) == 0.0)
                        {
                            firstY = Convert.ToDouble(yPoints.First());
                        }

                        if (Convert.ToDouble(yPoints.First()) != 0.0 && secondY == 0.0)
                        {
                            secondY = Convert.ToDouble(yPoints.Last());

                            if (secondY > firstY)
                            {
                                MinMax = 0;
                            }
                            else
                            {
                                MinMax = 1;
                            }
                        }

                        if (secondY != 0.0 && thirdY == 0.0)
                        {
                            thirdY = Convert.ToDouble(yPoints.Last());

                            if (MinMax == 0 && thirdY > secondY)
                            {
                                firstY = secondY;
                                secondY = thirdY;
                            }
                            else
                            {
                                Max[count,0] = secondY;
                            }
                        }
                    }

                    listBox1.Items.Add(values[0]);
                    listBox2.Items.Add(values[1]);
                }
            }
        }
    }
}

Upvotes: 4

Views: 2493

Answers (1)

babu646
babu646

Reputation: 1000

This snippet will get valleys and peaks on an array of y values, ignoring [0]

public void GetValleysAndPeaks(double[] yValues, out List<int> peakIndexes, out List<int> valleyIndexes)
{
    peakIndexes = new List<int>();
    valleyIndexes = new List<int>();

    bool directionUp = yValues[0] <= yValues[1];
    for(int i = 1; i < yValues.Length - 1; i++)
    {
        if(directionUp && yValues[i + 1] < yValues[i])
        {
            peakIndexes.Add(i);
            directionUp = false;
        }
        else if(!directionUp && yValues[i + 1] > yValues[i])
        {
            valleyIndexes.Add(i);
            directionUp = true;
        }
    }
}

Upvotes: 2

Related Questions