C# #地区差异及;置信度计算 //还没有。。。 使用制度; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms

C# #地区差异及;置信度计算 //还没有。。。 使用制度; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms,c#,variable-assignment,variance,C#,Variable Assignment,Variance,#地区差异及;置信度计算 //还没有。。。 使用制度; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 使用USBkit//这是用于Costomized dll包装器的。 使用System.IO//此选项适用于二进制文件操作。 使用LBR统计//这是统计图书馆的。 名称空间USBTL { 公共部分类Form1:Form { int i

#地区差异及;置信度计算
//还没有。。。
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用USBkit//这是用于Costomized dll包装器的。
使用System.IO//此选项适用于二进制文件操作。
使用LBR统计//这是统计图书馆的。
名称空间USBTL
{
公共部分类Form1:Form
{    
int iUSBDevice;//这是USB枚举器。
bool-bUSBDeviceStatus=false;//USB设备的状态。
//bool bWriteFileSet=false;//是否设置了文件路径。
//bool bWriteTofile=false;//正在写入文件。
bool bflasher=false;
float[,]fTemperatureData;//温度数据数组。
const int iTemperatureDataSize=128;
const int iNumberOfChannels=2;
float[]fMeanValue;//此数组用于平均值。
float[]fStdDev;//此数组用于标准偏差。
float[]fVariance;//此数组用于方差计算。
float[]fConfidence;//此数组用于置信度计算。
float[]fMaxTemperature=new float[2]{30,30};//这是温度转换常数。
float[]fMaxInputVoltage=new float[2]{255*4/5,255*4/5};//这是输入范围覆盖率常量。
enum ChanID{Chan1,Chan2};//只是模拟输入通道的枚举。
//类Form1构造函数。
公共表格1()
{
初始化组件();
}
//Form1_Load打开USB设备并初始化数据缓冲区。
私有void Form1\u加载(对象发送方、事件参数e)
{
Console.WriteLine(“应用程序正在启动!!”);//只是输出窗口上的一条消息
fMeanValue=新浮点[iNumberOfChannels];
fStdDev=新浮点[iNumberOfChannels];
fVariance=新浮点[iNumberOfChannels];
fConfidence=新浮动[iNumberOfChannels];
对于(int y=0;y// None yet...  
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Windows.Forms;  
using USBkit;                       //This is for the costomised dll wrapper.
using System.IO;                    //This if for binary file operations. 
using lbrStatistics;                //This is for the statistics library.



namespace USBTL  
{
    public partial class Form1 : Form    
    {    
        int iUSBDevice;                                                  //This is the USB enuminator.
        bool bUSBDeviceStatus = false;                                  //Status of USB device.  
        //bool bWriteFileSet = false;                                    //File pathway is set or not.  
        //bool bWriteTofile = false;                                    //Writting to file.
        bool bflasher = false;
        float[,] fTemperatureData;                                      //Temperature Data array.
        const int iTemperatureDataSize = 128;
        const int iNumberOfChannels = 2;
        float [] fMeanValue;                                            //This array is for the Mean Value.
        float [] fStdDev;                                               //This array is for the standard Deviation.
        float[] fVariance;                                              //This array is for the Variance calculations.
        float[] fConfidence;                                            //This array is for the Confidence calculations.

        float[] fMaxTemperature= new float [2] {30, 30};                //This is the Temperature conversion constand.
        float[] fMaxInputVoltage= new float [2] {255*4/5, 255*4/5};     //This is the Input Range coverage constant.

        enum ChanID {Chan1, Chan2};                                     //Just an enumeration for the Analogue Input Channels.

//Class Form1 constructor.        
        public Form1()
        {
            InitializeComponent();
        }

// Form1_Load opens up the USB Device and initialises the data buffer.
        private void Form1_Load(object sender, EventArgs e)
        {

            Console.WriteLine("Application is starting!!");         //Just a message on the output window
            fMeanValue = new float [iNumberOfChannels];
            fStdDev = new float [iNumberOfChannels];
            fVariance = new float[iNumberOfChannels];
            fConfidence = new float[iNumberOfChannels];
            for (int y = 0; y < iNumberOfChannels; y++)
            {
                fMeanValue[y] = 0;                                  //Initialise MeanValue
                fStdDev[y] = 0;                                     //Initialise Std Deviation
                fVariance[y] = 0;                                   //Initialise Variance
                fConfidence[y] = 0;                                 //Initialise Confidence
            }


        #region USB interogation code
            //Interogate and find the address of the USB kit.
            for(int Device=0; Device<4; Device++)
            {
                iUSBDevice = USB.OpenDevice(Device);
                if (iUSBDevice != Device)
                {
                    bUSBDeviceStatus = false;
                    stxtUSBStatus.ForeColor = Color.Red;
                    stxtUSBStatus.Text = "USB NOT FOUND";
                    Console.WriteLine("USB Device " + Convert.ToString(Device)+ " not found");
                }
                else
                {
                    bUSBDeviceStatus = true;
                    Console.WriteLine("USB Device " + Convert.ToString(Device) +" Found");
                    stxtUSBStatus.ForeColor = Color.Green;
                    stxtUSBStatus.Text = "USB "+Convert.ToString(Device)+" OPEN";
                    break;
                } 
            }

         #endregion

        #region Array initialisation Code
            fTemperatureData = new float[iTemperatureDataSize,iNumberOfChannels];
            for(int y=0; y<iNumberOfChannels; y++)
                for (int i = 0; i < fTemperatureData.GetLength(y); i++)
                    fTemperatureData[i,y] = 0; 
        #endregion

        #region If USB device found Enable the timer
            if (bUSBDeviceStatus == true)
            {
                tmrNS.Interval = 200;                           //Timer is running 5 times a second.
                tmrNS.Enabled = true;
            } 
        #endregion

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
             Console.WriteLine("Application Terminated");
        }

        private void tmrNS_Tick(object sender, EventArgs e)
        {
            #region flasher
            if (bflasher == false)
            {
                USB.SetAnalogChannel(1);
                bflasher = true;
            }
            else
            {
                USB.ClearAnalogChannel(1);
                bflasher = false;
            }
            #endregion

            #region Reading analogue channels.

            int [] iAnalogueInput = new int [2] {1, 2};

            USB.ReadAllAnalog(ref iAnalogueInput[0], ref iAnalogueInput[1]);
            pltData1.data = (int) ((float) iAnalogueInput[(int)ChanID.Chan1] * fMaxTemperature[(int)ChanID.Chan1]/fMaxInputVoltage[(int)ChanID.Chan1]);
            pltData2.data = (int) ((float) iAnalogueInput[(int)ChanID.Chan2] * fMaxTemperature[(int)ChanID.Chan2]/fMaxInputVoltage[(int)ChanID.Chan2]);
            pbTemp.Value = iAnalogueInput[(int)ChanID.Chan1];
            #endregion

            #region Setup the digital LEDs
            int Channel = (int)ChanID.Chan1;

            int iBitShift =(int) Math.Round(Math.Log(iAnalogueInput[Channel], 2), 0)-1;
            USB.WriteAllDigital(1 << iBitShift);


           #endregion

            #region Array updates.
            for(int y=iNumberOfChannels-1; y>0;y--)
                for (int i = fTemperatureData.GetLength(y)-1; i > 0; i--)
                    fTemperatureData[i,y] = fTemperatureData[i - 1,y];
            for(int y=iNumberOfChannels-1; y>0; y--)
                fTemperatureData[0,y] = (float) iAnalogueInput[y]*fMaxTemperature[y]/fMaxInputVoltage[y];                        
            #endregion

            #region Mean Value Calculation
            for (int y = 0; y < iNumberOfChannels; y++)
            {
                for (int i = 0; i < fTemperatureData.GetLength(y); i++)
                    fMeanValue[y] += fTemperatureData[i, y];

                fMeanValue[y] =(float) Math.Round(fMeanValue[y]/fTemperatureData.GetLength(y),2);
            }

            #endregion

            #region Standard Deviation Calculation
            for (int y = 0; y < iNumberOfChannels; y++)
            {
                for (int i = 0; i < fTemperatureData.GetLength(y); i++)
                    fStdDev[y] += (float)Math.Pow((double)(fTemperatureData[i, y] - fMeanValue[y]), 2.0);
                fStdDev[y] = (float) Math.Round((float)Math.Sqrt(fStdDev[y] / (fTemperatureData.GetLength(y) - 1)),2);
            }
            #endregion


            #region Variance Calculation
            //Add here code to calculate the variance
            //fVariance[0]=???
            #endregion

            #region Confidence Calculation
            //Add here code to calculate the Confidence
            //fConfidence[0]=???
            #endregion

            //Add here code to write the Mean value, Standard Deviation, Variance and Confidence in textboxs. Hint use labels to indicate which one is which.
            //Also add code here to call the statistic library functions 

        }

        private void pbTemp_Click(object sender, EventArgs e)
        {
            //Nothing implemented for this function. 
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tmrNS.Enabled == true)                              //Stop Timer.
                tmrNS.Enabled = false;
                USB.ClearAllAnalog();
                USB.ClearAllDigital();
                USB.CloseDevice();                                      //Close USB.
                GC.Collect();

        }

        private void pltData1_Load(object sender, EventArgs e)
        {
            //Nothing implemented for this event. 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //This is just an example of using the statistics library......
            //These are are all local variables!!!
            float [] fAverageValue = new float [iNumberOfChannels];
            float [] fStandardDeviation = new float [iNumberOfChannels];
            float [] fVariance = new float [iNumberOfChannels];
            float [] fConfidence = new float [iNumberOfChannels];
            for (int y = 0; y < iNumberOfChannels; y++)
            {
                for (int i = 0; i < fTemperatureData.GetLength(y); i++)                 //This is an example for filling up the data Array
                fTemperatureData[i, y] = 100 + 2 / ((float)i + 1);                      //Generate dummy data
                float[] fSingleDimArray= new float [iTemperatureDataSize];              //A temporary array for converting multidimentional arrays to single arrays.
                                                                                                                            // Array.Copy(fTemperatureData, (int)fTemperatureData.GetLength(128) * y, fSingleDimArray, 0, fTemperatureData.GetLength(y));
                for (int i = 0; i < fTemperatureData.GetLength(y); i++)
                    fSingleDimArray[i] = fTemperatureData[i, y];

                fAverageValue[y] = (float) Math.Round(Statistb.Average(ref fSingleDimArray),1);              //Calculating Average Value
                fStandardDeviation[y] =(float) Math.Round(Statistb.StdDev(ref fSingleDimArray),3);           //Calculating Standard Deviation
                fVariance[y] = (float) Math.Round(Statistb.Variance(ref fSingleDimArray),1);                 //Calculating Variance
                fConfidence[y] = (float) Math.Round(Statistb.Confidence90(ref fSingleDimArray),3);           //Calculating Confidence
                //This is for reporting the calculations to the output window.
                Console.WriteLine("Average"+y.ToString()+"=" + fAverageValue[y].ToString());
                Console.WriteLine("SD"+y.ToString()+"=" + fStandardDeviation[y].ToString());
                Console.WriteLine("Var"+y.ToString()+"=" + fVariance[y].ToString());
                Console.WriteLine("Conf"+y.ToString()+"=" + fConfidence[y].ToString());
            }
        }

        private void pltData2_Load(object sender, EventArgs e)
        {
            //Nothing implemented for this event!!
        }
    }
}
#region Variance Calculation
for (int y = 0; y < iNumberOfChannels; y++)
{
    for (int i = 0; i < fTemperatureData.GetLength(y); i++)
        fVariance[y] += (float)Math.Pow((double)(fTemperatureData[i, y] - fMeanValue[y]), 2.0);
    fVariance[y] = (float)(fVariance[y] / (fTemperatureData.GetLength(y) - 1));
}
#endregion