完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
你好!我现在有一个大脑障碍,希望有人能帮助我。我有一个数组,它有一个可调整的元素数量,每一个连续的元素都用一个动态值(“计数”)更新,直到它达到数组的限制,然后从一开始就开始。现在,我想做什么?将所有元素加在一起并按数组限制进行划分,但因为它不是固定长度,所以不知道将所有元素添加到“AlayLimeLimes”的语法,然后用“AlayLimeLimes”分隔。
以上来自于百度翻译 以下为原文 hello! I am having a brain block at the moment, hope somebody can help. I have an array with an adjustable number of elements, every second each successive element gets updated with a dynamic value ("count") until it has reached its array limit and then starts back from the beginning. Now, what I want to do is add all the elements together and divide by the array limit, but because its not a fixed length I don't know the syntax to add all the elements to "arraylimit" and then divide by "arraylimit". char i; countarray = count; if (i < arraylimit){ i++;} else {i = 0;} countav = countarray[0] + ... /arraylimit; ???? |
|
相关推荐
17个回答
|
|
|
MMMM,将它们全部合并到ARARYAULL,然后用ARALYEAL…
以上来自于百度翻译 以下为原文 Mmmm, sum them all together up to arraylimit, and then divide by arraylimit... |
|
|
|
|
|
在输入时添加值,而不是等到完成为止。LMT控制数组大小。
以上来自于百度翻译 以下为原文 Add the values as they are entered rather than wait until it is complete. #define MAX_LMT int idx=0,lmt=10,avg,cnt=0,x[MAX_LMT]; x[idx++]=val; cnt+=val; if(idx==lmt){idx=0;avg=cnt/lmt;cnt=0;} lmt controls the array size |
|
|
|
|
|
如果这是一个固定的限制,这将是很容易的,但是什么是语法来表示“和数组限制”?当一个新的计数被添加到它时,数组平均每秒,代码的平均部分在一个ISR中,每秒钟重复一次。你能注释你的代码并解释每个部分正在发生什么吗?非常感谢大家的帮助,谢谢。
以上来自于百度翻译 以下为原文 That would be easy if it was a fixed limit, but what is the syntax to say "sum to the array limit"? The array is to be averaged every second when a new count has been added to it, the averaging part of the code is in an ISR that repeats every second. Could you comment your code and explain what is happening at each part please? All help is greatly appreciated, thank you. |
|
|
|
|
|
我能做的最好。如果您的限制更改确保它小于Max。LMT。
以上来自于百度翻译 以下为原文 #define MAX_LMT 50 // MAXIMUM SIZE FOR MEMORY, (MISSED OUT 50 ON 1ST POST)int idx=0,lmt=10,avg,sum=0,array[MAX_LMT]; // GLOBAL VARIABLESvoid __attribute__((interrupt, no_auto_psv)) _T5Interrupt(void){ array[idx++]=count; // STORE NEW VALUE INTO ARRAY AND INCREMENT INDEX sum+=val; // SUM OF VALUES RECEIVED, RUNNING TOTAL if(idx==lmt){ // LIMIT REACHED OF HOW MANY ELEMENTS YOU NEED idx=0; // RESET INDEX avg=sum/lmt; // AVERAGE IS SUM / LIMIT sum=0; // RESET SUM } IFS1bits.T5IF=0; // CLEAR IRQ } Best I could do. If your limit changes make sure that it is smaller than MAX_LMT. Renamed cnt to sum, x to array and value to count that you had |
|
|
|
|
|
如果你理解Gort的建议和它所做的,那么就用它。对我来说,你的问题描述仍然相当模糊,所以我不知道你的问题是什么,或者你需要什么。详细地说,我确实知道当你真正需要平均(这意味着当你被允许重置计数器和总和)。基于计数?基于时间?仅仅通过数组大小强制?你可能需要一个“移动平均线”吗?我认为你的申请应该告诉你什么是必要的。
以上来自于百度翻译 以下为原文 If you understand Gort's proposal and what it does then -> use it. For me your problem description still is rather vague, so I have no idea what your problem is or what you need. In detail I did understand WHEN you actually need to average (which implies when you are allowed to reset your counter and sum). Based on count ? Based on time ? Just forced by array size ? May be you need a 'moving average' ? I think your application should tell you what is necessary. |
|
|
|
|
|
|
|
|
|
|
|
谢谢,我看到你在那里做了什么。问题是,你是在达到极限后重合极限,然后我要做的是移动平均值,当一个新的值进入数组时,最旧的被丢弃(重写),取平均值,然后再计算。就像我前面提到的,如果我有一个固定的数组长度,它很容易……但是我需要10的值(在if语句和除法中)是一个变量。这也意味着我需要将数组与相同变量的值相加。我需要一个函数来对数组求和,而不考虑它存储了多少元素。
以上来自于百度翻译 以下为原文 Thanks, I see what you did there. The problem with that is that you are summing to the limit and then resetting the sum after the limit is reached. What I am trying to do is a moving average, when a new value enters the array the oldest is discarded (re-written over), the average is taken and then displayed. Like I previously mentioned, if I had a fixed array length its quite easy... char i; countarray = count; if (i < 10){ i++;} else {i = 0;} countav = countarray[0] + countarray[1] + countarray[2] + countarray[3] + countarray[4] + countarray[5] + countarray[6] + countarray[7] + countarray[8] + countarray[9]/ 10; but I need the value of 10 (in the if statement and the division) to be a variable. This also means I need to sum the array to the value of the same variable. I need a function that sums the array regardless of how many elements it stores. |
|
|
|
|
|
a)在任何时候,您需要在数组中的当前有效计数数。为此需要一个计数器变量。从0开始(您的总数也从0开始),然后为每个中断增加1。一旦达到最大值,计数器值将不再改变。例如在你的例子中,一旦达到10,它将保持10。不要将此计数器值与数组索引值混合,这是两个不同的变量。b)在循环中建立和,最大值是计数器变量。一旦达到最大值(10),您将冗余地执行9个元素的求和,尽管这已经在之前完成。对于更大的数组和/或在中断中进行计算,这是一个更大的问题。对于这个(性能)原因,您可以优化它,只需从您的和(减)中删除最旧的元素,从而用新的值替换它(Add)。我的印象是,在B之前,你不应该尝试这样做。因为10只是一个例子,用数组的CouthSosil替换这个例子。
以上来自于百度翻译 以下为原文 a) at any time you need the current valid number of counts in your array. You need a counter variable for that. This starts with 0 (your sum also starts with 0 ) and then increments +1 for each of your interrupts. Once the maximum is reached, the counter value will not change anymore. E.g. in your example, once the 10 is reached, it will stay 10. Do not mix up this counter value with your array index value , that's two different variables. b) build the sum in a loop, the maximum is your counter variable. Divide by the counter variable. c) Once the max (10) is reached, you will redundantly perform the summation for 9 of the elements, although this has already been done before. This is more a problem for larger arrays and/or if you are doing your calculations in the interrupt. For this (performance) reason you might optimize that and just remove the oldest element from your sum (subtract) and thus replace it (add) with your new value. My impression is, that you should not try that before b) works. As the 10 is only an example, replace this with the choosen size of your array. |
|
|
|
|
|
你可以用MeMeTime[1,2,3,4,5 ] 15/5移动[2,3,4,5,6 ] 20/5 [3,4,5,6,7] 25/5 [4,5,6,7,8] 30 /5项=5MeMeTimes(数组,数组+1,ITEMs-1)数组[ITEMS1]=CurtSoS-=数组[0 ]和+ = CuTavvg=和/项
以上来自于百度翻译 以下为原文 You can move the array values with memmove [1,2,3,4,5] 15/5 [2,3,4,5,6] 20/5 [3,4,5,6,7] 25/5 [4,5,6,7,8] 30/5 items=5 memmove(array, array+1, items-1) array[items-1]=count sum-=array[0] sum+=count avg=sum/items |
|
|
|
|
|
因为它每秒只发生一次,所以时间不会是问题。我也会把这个代码从中断中移开。在ISR中设置一些标志,或者等待IRQ标志被设置并中断中断。
以上来自于百度翻译 以下为原文 Since it only happens once per second, time shoudn't be a problem. I'd also move that code out of the interrupt. Set some flag in the isr or just wait for the irq flag to be set and ditch the interrupt. |
|
|
|
|
|
我还是不清楚这一点。这个变量在达到10时是否保持在10,或者这个变量可以随时改变?
以上来自于百度翻译 以下为原文 I'm still not understanding this clearly. Does this variable stays at 10 when it reaches 10, or this variable can change any time? |
|
|
|
|
|
我还是不清楚这一点。这个变量在达到10时是否保持在10,或者这个变量可以随时改变?这个例子中的数字10是我需要平均多少个数据点或元素。我制作的设备是一个辐射探测器。盖革Mull管的脉冲在一秒钟内计数,记录在阵列中并复位。阵列的长度是平均(秒)的结果。10秒(或10元件阵列)周期将提供一致的结果(辐射发射是不均匀的)。然而,一个较长的平均时间将减少仪器的响应,它有时可能是有用的,以减少平均周期(和阵列长度)到3秒的按钮按下。它需要是一个移动平均线中的最后一个元素被丢弃,我相信Rodims和GOT2015E解决了Gunn问题。
以上来自于百度翻译 以下为原文 I'm still not understanding this clearly. Does this variable stays at 10 when it reaches 10, or this variable can change any time? the number 10 in this example is how many data points or elements in the array I need to average. The device I am making is a radiation detector. Pulses from a geiger-muller tube are counted over a one second period, recorded (in the array) and reset. The length of the array is how long (in seconds) the results are averaged. A 10 second (or 10 element array) period will provide a consistent result (radiation emission is non uniform). However, a long average time will reduce the instruments response and it sometimes may be useful to reduce the average period (and array length) to 3 seconds with the push of a button. It needs to be a moving average where the last element in the array is discarded, I believe Rodims and Gort2015 have solved the issue. Regards Gunn |
|
|
|
|
|
|
|
|
|
|
|
我相信你想要如下:你有一个固定大小的数组(比如说10个元素),你想用它来计算移动平均值。当第一次填充数组时,只有几个值,那么平均值应该只在这些值上计算。在第十个值之后,平均值总是被计算大约10个元素,因为从第十一个值开始,你重写最老的值。你不熟悉使用循环吗?例如:SimulalZeMixByoFixFox元素0。注意,数组限制是数组中的最高索引,而AARYSIZE用于声明您的CONTARTRAIL。因此,AARYLIMECTOR= ALARYSIZE - 1。看起来你混淆了这两个数字。我试着靠近你的代码,帮助你理解它,虽然这不是最佳的。此外,这对于移动平均来说并不是很有效。有效率取决于你的需求。回答以下问题:1)你需要什么时候更新?您是否需要在每次添加新的值后直接更新?还是只是偶尔?2)当数组第一次被填充时,你的数组的唯一动态是什么?3)数组中使用的数据类型是什么?整数还是浮点?4)你的数组有多大?10或100或1000或…既然你用的是“char”作为索引类型,它就不多了。5)平均值需要多少精度?6)启动条件对你来说真的是个问题吗?如果前9个值的错误平均值(如果数组的大小为10),那会是坏的吗?7)这个代码需要有很高的性能,而且必须是真的,真的,真的很快吗?如果你有一个整数数据类型,那么保持总和通常是最好的解决方案。如果输入值,则将其添加到总和中,并将元素的数量增加一个。如果移除一个值,则从该值中减去它,并将元素的数量减少一个。如果重写一个值,则与移除和添加元素相同。若要得到平均值,只需将元素和的数量除以。如果有浮点数据类型,则不能使用整数的方法,因为错误开始加起来。有许多不错的其他算法来阻止它。但这取决于你。
以上来自于百度翻译 以下为原文 I believe you want the following: You have a fixed size array (let's say 10 elements) which you want to use for calculating a moving average. When the array is filled for the first time, and you have only a few values then the average should be computed only over those values. After the 10th value the average will be always computed about 10 elements, because starting with the 11th value you are overwriting the oldest value. Are you not familiar with using a loop? For example like this: char i; char number_of_elements; // insert new element (might overwrite old one) countarray = count; // increase index if (i < arraylimit){i++;} else {i = 0;} // increase number of elements if (number_of_elements < arraylimit+1){number_of_elements++;} // calculate the average (sum up from countarray[0] to countarray[number_of_elements-1] and then divide) fill_in_type_which_suits_your_needs_here countsum = 0; char j; for (j=0;j } countav = countsum / number_of_elements; Initialize number_of_elements with 0. Note, that arraylimit is the highest index in your array, and arraysize is used for the declaration of your countarray. Therefore arraylimit = arraysize - 1. It seems you are confusing those two numbers. I tried to stay near to your code, to help you understand it although this is not optimal. Furthermore, that is not really efficient for a moving average. What is efficient depends on your needs. Answer the following questions: 1) When do you need the average updated? Do you need it updated every time directly after you have added a new value? Or only occasionally? 2) Is the only thing what is dynamic about your array the start situation, when the array gets filled for the first time? 3) What is your datatype used for the array? Integer or Float? 4) How big is your array (roughly)? 10 or 100 or 1000 or ... ? Since you are using "char" as index type, it can not be much. 5) Which accuracy do you need for the average? 6) Is the starting condition really an issue for you? Would it be bad if you get a wrong average for the first 9 values (if your array is of size 10)? 7) Is this code required to have a high performance and must be really, really, really fast? If you have an integer datatype then keeping the sum is typically the best solution. If you enter a value, then you add it to the sum and increase the number of elements by one. If you remove a value then subtract it from the sum and decrease the number of elements by one. If you overwrite a value, than that is the same as removing and then adding an element. To get the average you just divide the sum by the number of elements. If you have an float datatype, then you can not use the approach for integers, because the errors start adding up. There are quite a number of nice other algorithms to prevent it. But which fits for you, depends. |
|
|
|
|
|
你好,莫泽,谢谢你的回复。1)每秒(当一个新的值被添加到数组中)2)不,在一个按钮的推动下,数组需要缩短。我没有考虑开始的条件,这将是一个问题!3)单个元素将是整数(你不能从GM管得到半个脉冲),但平均值将是一个浮点。4)数组最多是10个元素。5)到至少1个小数点。6)见点2)。7,不,只要它能保持1Hz。谢谢你的帮助。
以上来自于百度翻译 以下为原文 hello Moser, thank you for your reply. 1) Every second (when a new value has been added to the array) 2) No, at a push of a button the array needs to be shortened. I didn't think about the starting condition, it would be an issue! 3) The individual elements would be integers ( you can't get half a pulse from a GM tube) but the average will be a float. 4) The array would be a maximum of 10 elements. 5) To at least 1 decimal place. 6) See point 2). 7) No, as long as it can be keep up with 1Hz. Thank you for your help. |
|
|
|
|
|
使用平滑比平均化更容易:x= a*v+b*x,其中(a+b)=1,v是新值。当a=2/n时,平滑近似等于平均值,因此在你的情况下a=2/10=0.2。这样可以避免使用数组,也可以为用户产生更好的结果。例如,当一个大的10秒的值被删除时,他不会看到10秒的平均变化。这里的代码:如果你坚持平均化:
以上来自于百度翻译 以下为原文 It is easier to use smoothing than averaging: x = a*v + b*x, where (a + b) = 1, v is the new value. Smoothing is approximately equivalent to averaging when a = 2/N, so in your case a = 2/10 = 0.2. This lets you avoid using arrays and also produces better results for the user - for example, he doesn't see the 10s average suddently change when a big 10-second old value gets removed from the acarage. Here's the code: uint32_t rolling_sum; // to add a value (every 1s). // here's how the formula gets converted to the code: // rolling_sum is x*5. x = 0.2*v + 0.8*x ===> rolling_sum = v + 4*x ===> // rolling_sum = v + 4*rolling_sum/5 ===> rolling_sum = rolling_sum + v - rolling_sum/5 rolling_sum += new_value - ((rolling_sum + 2)/5); // to get the 10s average (any time) average = (rolling_sum + 2)/5; If you insist on averageing: uint32_t rolling_sum; uint32_t past_values[10]; int index; // points to the next value to be updated // to add a value (every 1s). if (++index > 9) index = 0; // move the index to the next slot rolling_sum += new_value - past_values[index]; // replace the count from 10 seconds ago with the current count past_values[index] = new_value; // remember the value we used so that we could subtract it 10 cycles from now // to get the 10s average (any time) average = (rolling_sum + 5)/10; |
|
|
|
|
|
附加的问题:2a,6a)“如果你的前9个值得到错误的平均值(如果你的数组的大小是10),那会是坏的吗?”…如果你在前3个值(缩短的长度)得到了错误的平均值,那会是坏的吗?如果不是,数组元素可以被零或填充在开始时读取的第一个值。这将使它变得更简单——只需平均最近的10或3个值,而不是平均超过1, 2, 3,…,在起始点3a,5a上高达10,避免浮点数学。把你的总和乘以10,然后除以10或3。这将给出一个小数点,即一个人的位数是十进制数字。为了显示,你只要把小数点放在正确的位置。
以上来自于百度翻译 以下为原文 Additional questions: 2a, 6a) "Would it be bad if you get a wrong average for the first 9 values (if your array is of size 10)?" ... And would it be bad if you get the wrong average for the first 3 values (reduced length)? If not, the array elements can be zeroed or filled with the first value read at the start. This will make it simpler -- just average the most recent 10 or 3 values; instead of averaging over 1, 2, 3, ..., up to 10 at the start. 3a, 5a) Avoid floating point math. Multiply your sum by 10 before dividing by 10 or 3. That will give one decimal place; i.e. the one's digit is the decimal digit. For displaying you just place the decimal point at the correct position. |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
475 浏览 0 评论
5795 浏览 9 评论
2334 浏览 8 评论
2224 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3530 浏览 3 评论
1126浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1098浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
873浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
475浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-3 11:28 , Processed in 1.039154 second(s), Total 104, Slave 87 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
3302