Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello sir/mem
I am developing an application for oscilloscope where i need to calculate average of sample data. In my application after every 10ms new data store in buffer.I have to calculate average of 256 samples it means i can not create 256 array statically so i need to create 256 array at run time and store values in a single array after one time stand.
after 256 time stands I have to calculate sum of all array elements respectively there index values.
i have 8 different cases for 2, 4, 8, 16, 32, 64, 128, 256
I am able to calculate average for two array like this
C#
private void avgcl2(Int64[] bufavg1)
        {
            Int64 ab1 = 0;
            for (int i = 0; i <= bufavg1.Length; i++)
            {
                bufav4[i] = bufavg1[i];
                bufav5[i] = bufavg1[i];
                bufav6[i] = (bufav4[i] + bufav5[i]) / 2;
                ab1 = bufav6[i];
                    //code for draw graph
                list10.Add(c10, ab1);
                c10 += 1;
            }
        }
Posted
Comments
OriginalGriff 19-Feb-11 2:37am    
What is your problem? You don't say what part you are having difficulty with...
NileshKRathod 19-Feb-11 3:08am    
I want to create long type array at run time and fill that array with the values send by oscilloscope and calculate there average.
OriginalGriff 19-Feb-11 3:19am    
Yes, I understand that. What part of that are you having difficulty with?
Your code does not seem to show what problem you are having.
Try explaining the steps you are trying to take, and if they are working. And if not, why not!
NileshKRathod 19-Feb-11 3:23am    
no this part is working properly but I have to create 256 array at run time. these array(in code) are declare statically.
How can i declare and store data in array at run time.

"no this part is working properly but I have to create 256 array at run time. these array(in code) are declare statically.
How can i declare and store data in array at run time."


It is simple enough to declare an array of ints at run time:
int[] myNewArray = new int[256];
Does just that.
Array.Copy(mySourceOfIntegers, myNewArray, Math.Min(mySourceOfIntegers.Length, myNewArray.Length));
Copies data into the new array

You may want to be careful though: if you are allocating this at run time every 10ms, you may get the Garbage Collector coming in and slowing things down after a while - you could miss samples.

What are you actually receiving, and what are you trying to do with it? I assume that you receive 256 integer (or 8/16 bit more likely) samples from an ADC every 10ms, and are looking to store or average those before displaying your trace?
 
Share this answer
 
Comments
NileshKRathod 19-Feb-11 5:05am    
thank you sir
Why don't you create a custom class to represent the dataset for the point being sampled. This way, you can then expand on this for a number of data streams by easily create a collection of datasets.

The dataset is based on a queue with new data pushed in at the top and dropped off at the bottom. In the class you can also add all the statistical calcs you want to do, e.g. Min / Max / Avg

When drawing your current point on the graph you just call read the current value from the dataset.

(Note: When drawing graphs it is quicker to bitblit than try and update a trend by redrawing each value from a seriers, but thats not the topic of this question :) )


Take a look at the sample below i have created;
C#
class DataSet
    {
        //Maximium Size for the DataSet
        const int MAXLENGTH = 256;
        //Private Data to hold the samples
        Queue<Double> _data;
        private Double _maximum;
        private Double _minimum;
        private Double _average;
        public DataSet()
        {
            _data = new Queue<Double>();
            _maximum = new Double();
            _minimum = new Double();
            _average = new Double();
        }
        public readonly Double CurrentValue
        {
            get { return _data.Peek(); }
        }
        public readonly Double Maximum
        {
            get { return _maximum; }
        }
        public readonly Double Minimum
        {
            get { return _minimum; }
        }
        public readonly Double Average
        {
            get { return _average; }
        }
        //Public add a new
        public void AddValue(Double Value)
        {
            if (_data.Count > MAXLENGTH)
            {
                //If data exceeds max length, dump old value
                _data.Dequeue();
            }
            //Add new value to the queue
            _data.Enqueue(Value);
            //Stats
            calcStats();
        }
        //Reset the dataset (e.g. you might hava a start/stop function)
        public void Reset()
        {
            _data = new Queue<Double>();
            _maximum = 0;
            _minimum = 0;
            _average = 0;
        }
        private void calcStats()
        {
            Double max = new Double();
            Double min = new Double();
            Double sum = new Double();
            foreach (Double item in _data)
            {
                if (item > max)
                { max = item; }
                if (item < min)
                { min = item; }
                sum = sum + item;
            }
            _maximum = max;
            _minimum = min;
            _average = sum / _data.Count;
        }
    }
}


To use it you could then do;
C#
DataSet series1 = new DataSet();

series1.AddValue( xx.xxx );

//Read the Average Stats Value;
textbox1.text = series1.Average().toString();

//Current Sample Value
textbox1.text = series1.CurrentValue().toString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900