我需要使用c#

时间:2018-04-25 08:19:44

标签: c#

这是我的整个计划。我已经在这个项目上工作了一个星期,我只是陷入困境。我已设法读取CSV文件并将信息放入两个列表中,然后我将其转换为数组。现在我不必像这样做。这只是我设法编写和工作并在我脑海中有意义的原因。这是我设法到达的地方和我被困的地方。我所需要的只是能够从数据点中解析出所有峰值,最大值,谷值或最小值,并显示它们。如果我能做到这一点,那么我终于可以完成这个程序了。任何帮助和建议,代码,任何类似的东西都会非常感激。就像我说的,如果你有一个更好的公式,我可以插入并使用没有硬编码点(因为它有超过2000个),并以listbox或其他东西显示结果,那将是我的一天。感谢y,大家,提前了

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]);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

此片段将在y值数组上获得valleys和peak,忽略[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;
        }
    }
}