Moving average

I would like to develop a moving average function of an analog input variable - eg averaging over the most recent 1-hour period - to avoid sudden spikes or dips. Has anyone done this already?

One approach may be to sample instantaneous analog input every 1 minute, average these over (say) 6 minutes, then average the most recent 10 of the 6 minute-averages.

Any tips appreciated - thanks

You may need to program in T3000 to smooth the collected analog input variables. So I collected the data on IN1 once a minute and averaged it every six minutes as you said. I made an example. I don’t know if it is the reference you want.

10 IF INTERVAL(0:1:0) THEN VAR1 = IN1
20 VAR1_TOTAL = VAR1_TOTAL + VAR1
30 VAR1_COUNT = VAR1_COUNT + 1
40 IF VAR1_COUNT = 6 THEN AVG_VAR1 = VAR1_TOTAL / 6
50 VAR1_TOTAL = 0
60 VAR1_COUNT = 0

Lijun, could you show us the filter parameter, you can set that to a large value and will get what you need.

Maurice is right, you can also adjust the smoothness in the T3000 using the filter parameter. It is located at:

Thank you Lijun & Maurice.
I have tried changing filter parameter in the past but could not make sense of its affect on input value. Can you please explain what the filter parameter = 5 actually means? Is this related to filter time-constant? What value should I use to achieve a filter time-constant of say 60sec?

Could you tell me which Temco product you are using? The input filter in some products may not be supported.

The filter parameter is used for Filter function. Filter function is used to filter the input signal to reduce noise and fluctuations. The purpose of the filter is to make the input signal more stable and reliable by smoothing it.

int16 Filter(uint8 channel, signed input) {

int32 xdata siResult;
uint8 xdata I;
int32 xdata siTemp;

I = channel; /* Assign the channel to I */
/* Assign the input value to siTemp */
siTemp = input;

/* Check if the channel is 10 (assumed to be the temperature channel) */
if (I == 10) {
    /* If the power-up timer is less than 5, initialize old_temperature */
    if (power_up_timer < 5)
        old_temperature = siTemp;
    /* Apply a weighted average filter to the temperature value */
    siResult = (old_temperature * EEP_Filter + siTemp) / (EEP_Filter + 1);
    /* Update old_temperature with the filtered result */
    old_temperature = siResult;
} else {
    /* Apply a weighted average filter to the analog input value */
    siResult = (pre_mul_analog_input[I] * InputFilter(I) + siTemp) * 10 / (InputFilter(I) + 1);
    /* Apply rounding if necessary */
    if (siResult % 10 >= 5)
        siResult += 10;
    /* Update pre_mul_analog_input with the filtered result */
    pre_mul_analog_input[I] = siResult / 10;
    /* Divide the result by 10 to get the final filtered value */
    siResult /= 10;
}

/* Return the filtered result */
return siResult;

}

Bigger number means more samples are averaged, the signal will be smoother but slower. A smaller number means fewer saples are averaged, the signal will be more responsive but its a ‘noisier’ signal, more jagged response. I just grabbed this image from the 'net but it gives you the general idea.

I am using the T3-BB controller.

The filter function seems to be nearly what I am looking for provided I can learn how to drive it, ie what value to enter to achieve “small” and “large” response? Any hints?

I notice there is an offset between the “large” and “small” traces - can this be fixed?

As I understand, the listed code is not user-configurable, but is built-in to the controller - right?
It effectively replaces the “measured-input” value with a weighted average value.

Is “EEP_Filter” the value entered in the input table Filter column?

You can leave it at the default for the most part which I beleive is ‘5’. To be frank its not that critical, temperatures and HVAC processes don’t change very quickly. Set up a trend log and experiment with the settings if you think the system is not stable.

The example image I clipped there is just a filter example from the internet, completely unrelated to the T3000 software. There will be no offset when you adjust the filter.

The code Lijun sent you is the source code of the T3 controller so you can understand the filter logic. Basically its just averaging several samples before it sends out the value to the real world.

Thanks again.

So regarding a sensible value for Inputs table, “Filter” column…

Default (unfiltered) value is 5.
Is there a maximum permissible value? what is it?

To get a gradual response over say 1 hour, should I increase 5 to 6? to 10? to 50? to 500? to 5000??? I can experiment, but it would be helpful to know a rough starting point.

Cheers

Max Value: I just tried typing a large number and this message pops up:

Gradual Response: The best filter setting is the default value of 5, it gives a good balance between speed of response and smooth readings. I would set up a trend log if you are interested to learn more about the filter setting.

Thanks. I will do some experimentation with filter parameter and see if I can get a suitable result