Calculating average in a high priority thread
-
Hi Guys!, I'm developing a simple tracking game using two haptic devices as controllers. Haptic rendering is executed in a high priority thread. During the experiment, force experienced will depends on the position of each player, it's calculated as follows:
coupledForce1 = cMul(Kp, (newPosition2-newPosition1))-(cMul(Kb, (linearVelocity1)));
coupledForce2 = cMul(Kp, (newPosition1-newPosition2))-(cMul(Kb, (linearVelocity2)));
I have 100 trials of tracking tasks and I have to calculate the average interaction force during each trial. How it can be done? my issue is: this manipulation have to be done in a thread with execution rate of around 8KHz Thanks!
-
Hi Guys!, I'm developing a simple tracking game using two haptic devices as controllers. Haptic rendering is executed in a high priority thread. During the experiment, force experienced will depends on the position of each player, it's calculated as follows:
coupledForce1 = cMul(Kp, (newPosition2-newPosition1))-(cMul(Kb, (linearVelocity1)));
coupledForce2 = cMul(Kp, (newPosition1-newPosition2))-(cMul(Kb, (linearVelocity2)));
I have 100 trials of tracking tasks and I have to calculate the average interaction force during each trial. How it can be done? my issue is: this manipulation have to be done in a thread with execution rate of around 8KHz Thanks!
Please don't repost questions. You have already asked this here Calculating average in a thread[^].
-
Please don't repost questions. You have already asked this here Calculating average in a thread[^].
my bad!, just removed
-
my bad!, just removed
Thank you. The problem with duplicate questions is that you may get answers from different users to both questions. A clarification question to yours: Do you need code to calculate the average? If so, you may search for "moving average" or "rolling average".
-
Thank you. The problem with duplicate questions is that you may get answers from different users to both questions. A clarification question to yours: Do you need code to calculate the average? If so, you may search for "moving average" or "rolling average".
Thank you!. I have already seen some implementation of moving average. But I'm not sure about how to implement for my application. Since it's in a thread which is executing at 8Khz, I'm wondering how to store forces trial by trial and to take avareage?
-
Thank you!. I have already seen some implementation of moving average. But I'm not sure about how to implement for my application. Since it's in a thread which is executing at 8Khz, I'm wondering how to store forces trial by trial and to take avareage?
See this Stackoverflow thread: http://stackoverflow.com/questions/10990618/calculate-rolling-moving-average-in-c-or-c[^]. The fastest solution would be a ring buffer and a total sum variable (untested):
int items = 0;
int ndx = 0;
int buffer[buffer_Size];
// May need floating point here when max. value * buffer_size >= INT_MAX
int sum = 0;int get_av(int val)
{
if (items >= buffer_size)
sum -= buffer[ndx];
else
++items;
sum += val;
buffer[ndx] = val;
if (++ndx >= buffer_size)
ndx = 0;
return sum / items;
}This should be fast enough for your requirements