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

  • A+
所属分类:.NET技术
摘要

如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

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