以下代码，请提出优化建议int forceInt = 0;//用于记录上次的力
            //1.算好100个电压值。0到8V
            int numPoints = 20;
            double[] sineWave = new double[numPoints];

            for (int i = 0; i < numPoints; i++)
            {
                double angle = 2 * Math.PI * i / numPoints; // 0 to 2*PI in numPoints steps
                sineWave[i] = Math.Round(4 * (1 + Math.Sin(angle - Math.PI / 2)), 3);
                //sineWave[i] = 4 * (1 + Math.Sin(angle - Math.PI / 2)); // Scale sine wave from -1 to 1 to 0 to 8V
            }

            // Print the sine wave values
            for (int i = 0; i < numPoints; i++)
            {
                Console.WriteLine("Point:" + i + "," + sineWave[i] + ",V");
            }
            dataPoints.Clear();
            //2.记录当前的上传感器和下传感器的值
            //numbersUp.Clear();
            await Task.Delay(11 * 1000);
            double upperResolution = Math.Round(rawSignalUp.Average(), 3);
            

            //double upperResolution = Convert.ToDouble(textBox3.Text);
            double lowerResolution = Math.Round(rawSignalDown.Average(), 3);

            List<double> numbers = new List<double>();

            int initial = (int)numericUpDown11.Value;
            int increment = (int)numericUpDown8.Value;
            int target = (int)numericUpDown9.Value;
            for (int j = initial; j < target; j += increment)
            {
                
                xValue = 0.0;
                yValue = 0.0;
                textBox11.Text = "0";
                textBox12.Text = "0";
                textBox15.Text = "0";
                textBox16.Text = "0";
                bool isbandstopFirst = true;//用于记录是否初始化算法
                
                //dataPoints.Clear();
                textBox14.Text = (j).ToString("F0");
                //3.发第一个电压值1，等待10000ms
                for (int i = 0; i < numPoints; i++)
                {
                    if (!isTest) return;
                    Data_Output("当前序号：" + i);
                    ApplyVoltage(sineWave[i] * 25);
                    await Task.Delay(11 * 1000);
                    //4.计算当前的位移，F= k * x ,计算出F发给发力电机
                    double upperResolution1 = Math.Round(rawSignalUp.Average(), 3);
                    double lowerResolution1 = Math.Round(rawSignalDown.Average(), 3);
                    float displacement = (float)((upperResolution - upperResolution1) + (lowerResolution - lowerResolution1));

                    stiffness = j;
                    //stiffness = (int)numericUpDown13.Value;
                    int force = (int)(displacement * stiffness) + forceValue;

                    // 增加电压以后，从现在的力每次加一，计算是否大于计算的力，如果符合条件 退出。减少电压相反
                    bool isIncreasing = i < numPoints / 2; // 判断是增加还是减少 forceInt
                    int delay = 13 * 1000; // 延迟时间
                    //double upperResolution1, lowerResolution1;
                    //float displacement;
                    //int stiffness = j;
                    //int force;

                    while ((isIncreasing && forceInt < force) || (!isIncreasing && forceInt > force))
                    {

                        // 计算 forceInt 和 force 之间的差值
                        int difference = Math.Abs(force - forceInt);

                        // 定义最小步进值，防止步进值过小导致无限循环
                        int minStep = 1;

                        // 计算步进值，步进值可以是差值的一半，或者其他合适的比例
                        int step = Math.Max(difference / 2, minStep);

                        // 根据 isIncreasing 增加或减少 forceInt
                        forceInt += isIncreasing ? step : -step;

                        //forceInt += isIncreasing ? 10 : -10; // 根据 isIncreasing 增加或减少 forceInt

                        // 确保 forceInt 不小于 forceValue
                        if (forceInt < forceValue)
                        {
                            int randomNumber = rand.Next(1, 21);
                            forceInt = forceValue + 1;
                        }
                            

                        Force_Set(forceInt); // 设置力值
                        await Task.Delay(delay); // 延迟

                        List<double> list = new List<double> { };
                        double[] data = rawSignalUp.ToArray();
                        if (isbandstopFirst)
                        {
                            for (int k = 0; k < 100; k++)
                            {
                                bandstop.bandStop(data[k]);
                            }
                            isbandstopFirst = false;
                        }


                        for (int k = 0; k < 200; k++)
                        {
                            list.Add(bandstop.bandStop(data[k]));
                        }

                        List<double> list1 = new List<double> { };
                        double[] data1 = rawSignalDown.ToArray();

                        for (int k = 0; k < 200; k++)
                        {
                            list1.Add(bandstop.bandStop(data1[k]));
                        }


                        // 读取分辨率值
                        upperResolution1 = Math.Round(list.Average(), 3);

                        lowerResolution1 = Math.Round(list1.Average(), 3);

                        // 计算位移
                        displacement = (float)((upperResolution - upperResolution1) + (lowerResolution - lowerResolution1));

                        // 计算力值
                        force = (int)(displacement * stiffness) + forceValue;
                    }

                    //if (force < forceValue)
                    //    force = forceValue;

                    //Force_Set(force);
                    Console.WriteLine("加电压到" + sineWave[i] * 25 + "V, 后....位移: " + Math.Round(displacement, 2) + "阻挡力：" + textBox5.Text);
                    //检查力是否到位，如果不到位记录一下。
                    int maxChecks = 100;
                    int interval = 200; // milliseconds
                   int tolerance = 10; // tolerance range
                   int currentValue;
                   for (int k = 0; k < maxChecks; k++)
                   {
                       // Wait for the specified interval
                       await Task.Delay(interval);
                       if (int.TryParse(textBox5.Text, out currentValue))
                       {
                           // Check if the value is within the tolerance range
                           if (Math.Abs(currentValue - (force - forceValue)) <= tolerance)
                           {
                               break;
                           }
                       }
                       if (k == maxChecks - 1)
                       {
                           Console.WriteLine("没有到达目标力");
                       }
                       
                   }

                   //保存数据 Math.Round(4 * (1 + Math.Sin(angle - Math.PI / 2)), 3)
                   dataLines.Add(string.Format("{0},{1},{2}", textBox5.Text, Math.Round(displacement, 2), stiffness));
                    // 添加数据点
                   dataPoints.Add(Math.Round(displacement, 2), (forceInt - forceValue));
                    if (isIncreasing)
                    {
                        double value = Math.Round(displacement, 2);
                        if (value < 0) value = 0;
                        numbers.Add(value);
                        if (numbers.Count > numPoints / 2 - 1)
                        {
                            double max = numbers.Max();
                            double average = numbers.Average();

                            if (this.textBox11.InvokeRequired)
                            {
                            }
                            else
                            {
                                textBox11.Text = (max).ToString("F2");
                            }

                            if (this.textBox12.InvokeRequired)
                            {
                            }
                            else
                            {
                                textBox12.Text = (average).ToString("F2");
                            }
                            double forceMax = max * int.Parse(textBox14.Text);
                            double forceAve = average * int.Parse(textBox14.Text);
                            if (this.textBox15.InvokeRequired)
                            {
                            }
                            else
                            {
                                textBox15.Text = (forceMax).ToString("F0");
                            }
                            if (this.textBox16.InvokeRequired)
                            {
                            }
                            else
                            {
                                textBox16.Text = (forceAve).ToString("F0");
                            }
                            numbers.Clear();

                        }
                    }
                    // 更新 ZedGraph
                    //zedGraphControl1.GraphPane.CurveList.Clear();
                    //LineItem curve = zedGraphControl1.GraphPane.AddCurve("", dataPoints, Color.Red, SymbolType.None);
                    //zedGraphControl1.AxisChange();
                    //zedGraphControl1.Invalidate();

                    // 更新 ZedGraph
                    zedGraphControl1.Invoke((Action)(() =>
                    {
                        zedGraphControl1.GraphPane.CurveList.Clear();
                        LineItem curve = zedGraphControl1.GraphPane.AddCurve("", dataPoints, Color.Red, SymbolType.None);
                        zedGraphControl1.AxisChange();
                        zedGraphControl1.Invalidate();
                    }));
                }
            }
            
            Data_Output("比例测试法测试完成");

            // 获取当前时间并生成文件名
            string timestamp = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            string directoryPath = txtDirectoryPath.Text;
            string fileName = timestamp + ".csv";
            string filePath = Path.Combine(directoryPath, fileName);

            // 确保目录存在
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            // 写入数据到CSV文件
            //dataLines.Add(string.Format("{0},{1},{2}", 2, 3, 10));
            //dataLines.Add(string.Format("{0},{1},{2}", 21, 33, 10));
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                writer.WriteLine("Force (N),Deformation (um), Stiffness(N/um)");
                foreach (string line in dataLines)
                {
                    writer.WriteLine(line);
                }
                //foreach (PointPair point in dataPoints)
                //{
                //    writer.WriteLine(string.Format("{0},{1},{2}", point.Y, point.X, stiffness));
                //}
            }
            Data_Output("数据导出成功");