#region variance&置信度计算

时间:2011-03-30 13:07:04

标签: c# variable-assignment variance

// 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!!
        }
    }
}

1 个答案:

答案 0 :(得分:1)

#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

您对哪些置信区间感兴趣?