# Moving average

Hello,
I’m not too familiar with the pac control instruction set so before I head down a particular path, I’d like to know from the community if there is an easier way.

I am using an R2 controller with a 2 analog input; one represent voltage, the other current and I need to monitor a moving average of the resistance (v/i). Calculating the resistance is done, but I’m not sure about creating the average. This is what it looks like I need to do… (this is not a question about how to implement a moving average, its about how to implement one in the controller)

Create a table variable and an index
Add a resistance value to the table using the Move To numeric Table Element instruction
Increment the index and reset to 0 if the table is full
Get the table total by looping through each value the divide the total by the number of elements in the table.

Again, I am not very familiar with the instruction set so am wondering if there are any built instruction to do what I am trying the do, or do I need to handle everything ‘manually’?
kevin

Hi Kevin,

Your method sounds okay to me. There is no “built-in” average method.

However, you might check out the totalizing feature, starting with the “Set Analog Totalizer Rate” command.

I’m wondering if you couldn’t skip the table and “total by looping” part you mention above by configuring totalizing for your 2 inputs, then regularly doing a “Get & Clear Analog Totalizer Value” command. You’d divide the one returned total by the other for the average over that time period.

Does that make sense?

-OptoMary

Hi OptoMary,
I am looking for an easy way to get an hourly average of an analog input, such as a furnace temperature. Can I use the commands “SetAnalogTotalizerRate” and “GetAnalogTotalizerValue” for this purpose. I know that I can just dump values into a float table and average it, but I am looking for a better way. If yes, how would I do this? thanks,

Welcome to OptoForums!

Yes! I’ve attached a chart you can import into a 9.3 PAC Control Basic (or Pro) strategy. You’d just change the initial value for fSecontsToTotalize from the 10 seconds I used to demonstrate here to your 3600 second requirement.

Just set the totalize rate for your temperature to that value (block 1 here), then in a loop do the Get & Clear Totalize Value when that amount of time has passed (I used an up timer to test that in the condition block).

In this example, I also added an unnecessary GetAnalogTotalizerValue for fun so you could see how fCurrentValue accumulates the appropriate fractional values over time.

Hope that makes sense, and you can see this little YouTube clip of the code in action!

//youtu.be/JEsqFQYrmi8

thank you, that works great!