# Boxcar averaging

## Recommended Posts

Well, it depends on whether you want to log the averaged data or not. If you just want to make your graph look better, use the boxcar() function as the expression for your graph. Boxcar is only designed for use with an array of data that you want reduced to a smaller array.

If you want everything, including logging, to be averaged, you really want the mean() function because you want to take an array of data and get a single averaged point. To use it, you'd have to accumulate some values:

```private m1 = 0
private m2 = 0
private count = 0
while (1)
count++
DigitalOut = 0
read(Eingang0)
m1 += eingang0[0]
if (count == 5)
Motor01.AddValue(m1/count)
m1 = 0
endif
delay (30)

DigitalOut = 1
read(Eingang0)
m2 += eingang0[0]
if (count == 5)
Motor02.AddValue(m2/count)
m2 = 0
endif
delay (30)
endwhile
```

This assumes you still want to alternate readings. If instead you want to read eingang rapidly and then have it put the average in motor01 do something like:

```while (1)
DigitalOut = 0
for (private.c = 0, c &lt; 5, c++)
read(Eingang0)
endfor
Motor01.AddValue(mean(eingang0[0,4]))
delay (30)

DigitalOut = 1
for (private.c = 0, c &lt; 5, c++)
read(Eingang0)
endfor
Motor02.AddValue(mean(eingang0[0,4]))
delay (30)
endwhile
```

##### Share on other sites

• 2 weeks later...

The second choice is a good one. For my programme I used it with 100 counts so that the values and the graph gets smoother.

But my problem is that I have a long time measure for several motors simultaneous. That means that degree of utilization of the CPU is getting higher. Labjack reads the first 100 measures and calculates it. So it reads everything with the highest frequenz, I think its about 50Hz. It is quite too fast for my application. Is there any possibility to reduce the speed of this program so that the degree of utilization also can be reduced?

##### Share on other sites

Certainly, just add a delay inside the for loops:

```   for (private.c = 0, c &lt; 5, c++)
read(Eingang0)
delay(0.1)
endfor
```

#### Archived

This topic is now archived and is closed to further replies.