C# 求方差和平均值并保存

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            List<byte[]> Zhendata = new List<byte[]>();
            byte[] byt1 = { 0x01, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16, 0x15 };
            byte[] byt2 = { 0x02, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b };
            byte[] byt3 = { 0x03, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
            Zhendata.Add(byt1);
            Zhendata.Add(byt2);
            Zhendata.Add(byt3);
            adDataCaculate(Zhendata);




        }
        public static void adDataCaculate(List<byte[]> Zhendata)
        {
            byte[] byteData = new byte[30];
            int count1 = 0, count2 = 0;
            StringBuilder sNeed = new StringBuilder();


            foreach (Byte[] Package in Zhendata)
            {
                count1 = 0;
                foreach (byte byt in Package)
                {
                    if (count1 >=1)   //跳过第一个数
                    {


                        byteData[count2] = byt;
                        count2++;
                        // Console.Write("--" + byt);
                    }
                 count1++; 
                }
            }
           // Console.ReadKey();
            double[] channel = new double[5];
            double variance = 0.0, average = 0.0;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    channel[j] = byteData[j * 6 + i*2]  + byteData[j * 6 + i*2+1];
                    sNeed.Append(channel[j] + ",");


                }
                variance = Var(channel);
                sNeed.Append(variance + "--");
                average = channel.Average();
                sNeed.Append(average);
                strWrite(sNeed.ToString(), Environment.CurrentDirectory + "\\channelData", "channelData.txt");
                sNeed.Clear();
            }


        }
        public static double Var(double[] v)
        {
            double sum1 = 0;
            for (int i = 0; i < v.Length; i++)
            {
                double temp = v[i] * v[i];
                sum1 = sum1 + temp;
            }


            double sum = 0;
            foreach (double d in v)
            {
                sum = sum + d;
            }
            double var = sum1 / v.Length - (sum / v.Length) * (sum / v.Length);
            return var;
        }
        private  static int rowCount = 0;
        private static void strWrite(string str, string filePath, string fileName)
        {


            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            if (!File.Exists(filePath + "\\" + fileName))
                File.Create(filePath + "\\" + fileName).Close(); //.Close 很关键,不然会有问题
            if (rowCount < 3600)
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true);//true 追加数据
                sw.WriteLine(str);
                sw.Close();
                rowCount++;
            }
            else
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, false);//true 追加数据
                sw.WriteLine(str);
                sw.Close();
                rowCount = 0;
            }
        }
    }
}


追加了温度数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThzData;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Drawing;


using ThzDataProcess;
using System.Windows.Forms;




namespace BLL
{
    class DataCalculate
    {
       
        public  void tempartureData_zyr(List<byte[]> Zhendata)
        {
            StringBuilder sNeed = new StringBuilder();
            foreach (Byte[] match in Zhendata)
                sNeed.Append(BitConverter.ToString(match).Replace("-", "").Substring(20).ToUpper());
            double[] temparture = GetTemparture_zyr(sNeed.ToString());
            sNeed.Clear();
            foreach (var tem in temparture)
                sNeed.Append(tem + "***");
            strWrite_zyr(sNeed.ToString(), Environment.CurrentDirectory + "\\bin", "tempartureData.txt");
            sNeed.Clear();




        }


        public double[] GetTemparture_zyr(string str)
        {
            string tepStr = str.Substring(20 * 2, 8 * 2);
            double[] Temparture = new double[4];
            if (tepStr == null)
                return Temparture;
            byte[] Wen = DataProcess.strToToHexByte(tepStr);
            if ((Wen[0] & 0xf0) >> 4 == 15)
                Temparture[0] = -(~Wen[1] + 1 + 256.0 * ~Wen[0]) / 16;
            else
                Temparture[0] = (Wen[1] + 256.0 * Wen[0]) / 16;


            if ((Wen[2] & 0xf0) >> 4 == 15)
                Temparture[1] = -(~Wen[3] + 1 + 256.0 * ~Wen[2]) / 16;
            else
                Temparture[1] = (Wen[3] + 256.0 * Wen[2]) / 16;


            if ((Wen[4] & 0xf0) >> 4 == 15)
                Temparture[2] = -(~Wen[5] + 1 + 256.0 * ~Wen[4]) / 16;
            else
                Temparture[2] = (Wen[5] + 256.0 * Wen[4]) / 16;


            if ((Wen[6] & 0xf0) >> 4 == 15)
                Temparture[3] = -(~Wen[7] + 1 + 256.0 * ~Wen[6]) / 16;
            else
                Temparture[3] = (Wen[7] + 256.0 * Wen[6]) / 16;
            return Temparture;
        }
        public void adDataCaculate_zyr(List<byte[]> Zhendata)
        {
            byte[] byteData = new byte[35250];//1410*25
            int count1 = 0, count2 = 0;
            StringBuilder sNeed = new StringBuilder();


            foreach (Byte[] Package in Zhendata)
            {
                count1 = 0;
                foreach (byte byt in Package)
                {
                    if (count1 >= 20)   //跳过第一个数
                    {


                        byteData[count2] = byt;
                        count2++;


                    }
                    count1++;
                }
            }
            // Console.ReadKey();
            double[] channel = new double[473];
            double variance = 0.0, average = 0.0;
            for (int i = 0; i < 36; i++)
            {
                for (int j = 0; j < 473; j++)
                {
                    byte bigByte = Convert.ToByte(byteData[j * 74 + i * 2].ToString("X"), 16);
                    if ((bigByte & 0xf0) >> 4 == 15)
                        channel[j] = -(~byteData[j * 74 + i * 2 + 30] + 1 + 256.0 * ~byteData[j * 74 + i * 2 + 31]) / 16;
                    else
                        channel[j] = (byteData[j * 74 + i * 2 + 30] + 256.0 * ~byteData[j * 74 + i * 2 + 31]) / 16;
                    sNeed.Append(channel[j] + ",");
                }
                variance = Var_zyr(channel);
                sNeed.Append("***variance:" + variance + "***average:");
                average = channel.Average();
                sNeed.Append(average);
                strWrite_zyr(sNeed.ToString(), Environment.CurrentDirectory + "\\bin", "channelData.txt");
                sNeed.Clear();
            }


        }


        public double Var_zyr(double[] v)
        {
            double sum1 = 0;
            for (int i = 0; i < v.Length; i++)
            {
                double temp = v[i] * v[i];
                sum1 = sum1 + temp;
            }


            double sum = 0;
            foreach (double d in v)
            {
                sum = sum + d;
            }
            double var = sum1 / v.Length - (sum / v.Length) * (sum / v.Length);
            return var;
        }
        private int rowCount = 0;
        private void strWrite_zyr(string str, string filePath, string fileName)
        {


            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            if (!File.Exists(filePath + "\\" + fileName))
                File.Create(filePath + "\\" + fileName).Close(); //.Close 很关键,不然会有问题
            if (rowCount < 3600)
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true);//true 追加数据
                sw.WriteLine(str);
                sw.Close();
                rowCount++;
            }
            else
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, false);
                sw.WriteLine(str);
                sw.Close();
                rowCount = 0;
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值