C# UWP average of measurement values from array

Viewed 132

I read an analog temperature into an array.
The measurement is a infinite loop.
For the average I need the last 50 values.
First I fill up the array until all elements are written. Then it kicks out the first element to make space for a new one. Then the average is taken. Unfortunately it let's my program freeze for a second. Seems like that my way of doing it takes lots of resources.
Is there a more neat way?

    UInt64 count_avg = 0;  //global variable

    //Main
    public MainPage()
    {
        this.InitializeComponent();
        while(true)
          {
             double analog_temperature = getData();
             Average(analog_temperature);
          }
    } //Main




        private string Average(double analog_temperature)
    {
        double temp_avg = 0;
        double[] array = new double[50];
        string avg = "";

        count_avg += 1;

        if (count_avg <= 50)
        {
            array[count_avg] = analog_temperature;
            temp_avg = analog_temperature;
        }
        else
        {
            double sum = 0;
            List<double> array_liste = array.OfType<double>().ToList();  //convert array to list
            array_liste.RemoveAt(0); //delete first list item
            array = array_liste.ToArray();  //convert list ot array

            array[array.Length - 1] = analog_temperature; //write current value in last array element

            for (int i = 0; i < array.Length; i++)
            {
                sum = array[i];
            }
            temp_avg = summe / array.Length;

        }

        temp_avg = Math.Round(temp_avg , 1);


        return avg = Convert.ToString(temp_avg );
    }
1 Answers

this program fixes some errors i have seen in your program to calculate the moving average. i have modified the calculate of your average to avoid to build a list and calculate the average of 50 values again and again.. you just do one substraction and one addition.

may be this modification could avoid to use thread:

    public int count_avg = 0;
    public double sum = 0;
    public double temp_avg = 0;
    public int id = 0;
    double[] array = new double[50];

    private string Average(double analog_temperature)
    {
        if (count_avg < array.Length)
        {
            array[count_avg++] = analog_temperature;
            temp_avg = analog_temperature;
            sum += analog_temperature;
            if (count_avg == array.Length)
                temp_avg = sum / array.Length;
        }
        else
        {
            temp_avg = temp_avg - array[id] / array.Length;
            array[id++] = analog_temperature;    // replace the oldest value
            temp_avg = temp_avg + analog_temperature / array.Length;
            if (id == array.Length) id = 0;
        }

        return Math.Round(temp_avg, 1).ToString();
    } 

i am using this method to calculate the moving average:

avg = x0/N + x2/N + ... + Xn-1/N       (X0, X1, ...., Xn-1 are N values)

new value Xn replace x0 so

avg = avg - X0/N               (substract the oldest value of average)

and finally

avg = avg + Xn/N               (add the new value to the average)

then Xn+1 replace X1 and repeat again and again

the double[] array is used as circular array, so i replace the oldest value by the new value (the var id points to the oldest value of the array)

Related