在计时器的长类型运行时创建数组 [英] creating array at run time of long type at timer

查看:70
本文介绍了在计时器的长类型运行时创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生/女士
我正在开发用于示波器的应用程序,我需要在其中计算样本数据的平均值.在我的应用程序中,每隔10毫秒将新数据存储在缓冲区中之后,我必须计算256个样本的平均值,这意味着我无法静态创建256个数组,因此我需要在运行时创建256个数组并将值在一次站立后存储在单个数组中.
经过256个时间后,我必须分别计算所有数组元素的总和以及索引值.
我有2、4、8、16、32、64、128、256的8种不同情况
我可以像这样计算两个数组的平均值

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

<pre lang="cs">private void avgcl2(Int64[] bufavg1)<br />
        {<br />
            Int64 ab1 = 0;<br />
            for (int i = 0; i <= bufavg1.Length; i++)<br />
            {<br />
                bufav4[i] = bufavg1[i];<br />
                bufav5[i] = bufavg1[i];<br />
                bufav6[i] = (bufav4[i] + bufav5[i]) / 2;<br />
                ab1 = bufav6[i];<br />
                    //code for draw graph<br />
                list10.Add(c10, ab1);<br />
                c10 += 1;<br />
            }<br />
        }</pre><br />

推荐答案

该部分无法正常工作,但我必须在运行时创建256个数组.这些数组(在代码中)是静态声明的.
如何在运行时在数组中声明和存储数据."


在运行时声明一个整数数组很简单:
"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));

将数据复制到新数组中

不过,您可能要小心:如果在运行时每10ms分配一次,则可能会导致垃圾收集器进入并过一会儿使速度变慢-您可能会错过样本.

您实际上收到了什么,您打算如何处理?我假设您每10毫秒从ADC接收256个整数(或者更有可能是8/16位)的采样,并希望在显示轨迹之前存储或取平均值?

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?


为什么不这样做?您不会创建一个自定义类来表示所采样点的数据集.这样,您可以轻松创建数据集,然后在此基础上扩展多个数据流.

数据集基于一个队列,其中新数据推入顶部,而数据放到底部.在课程中,您还可以添加您要执行的所有统计计算,例如最小/最大/平均

在图形上绘制当前点时,只需调用即可从数据集中读取当前值.

(注意:绘制图形时,比尝试通过从Seriers中重绘每个值来更新趋势要快一些,但是那不是这个问题的主题:))


看看下面我创建的示例;
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;
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;

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();


这篇关于在计时器的长类型运行时创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆