## Ladder logic 402: Data manipulation and filtering

The prior post on ladder logic discussed using a programmable logic controller (PLC) to capture values from an analog card. Once those values have been captured, there are methods to filter the data and methods to simulate a filter using Microsoft Excel before it is coded into a PLC. There are several filtering algorithms that can be found on the Internet currently being used on a PLC (see links below). For the formulas, the same variable names were used even though they differed extensively in the various posts:

N = New value calculated and substituted for the input value. In other words, the calculated filtered value.

L = The previous captured input value.

F = The previous calculated filtered value.

k = The manipulated factor for all calculations (sometimes known as Alpha).

i = The current analog input value.

Avg = A running average over x number of previous samples, (3 for x was used in this example.)

The formula for a first order filter is New Filtered = Last Filtered + Factor * (Input – Last Filtered).

This equates to N=F+k(i-F) according to the variables listed above. The actual formula uses Alpha instead of k. Results from this formula are listed as Formula 6 below in the simulation.

To simulate a signal analog values were listed in one column on an Excel spreadsheet. These values are typical for a 13-bit signed analog signal from a PLC card; notice that they increment or decrement by a factor of eight. This would mean that on a 0 to 10v or 0 to 20mA signal with a range of 0-32,767, there would be 4096 possible values for the signal. The other half of the range, -32768 to -1, is not obtainable from the field device.

The first column shown above are the input values. The value of 20168 simulates a big noise spike that the user might want to filter for. The following columns show results after using the various filtering formulas found. The reason the input column has earlier values in it is to ptovide a running average of up to five values in the formulas. Formula 6 (shown below) also needs to use the previous filtered value, so that’s why it has an extra value in it also.

This is a key with the variables listed and the formulas. The k values are linked to cells used in the table.

Formula 1 actually looks like it makes the signal worse. Formula 2 and Formula 4 did nearly the same things; in the instructions Formula 2 was "k=1.0, no filtering, k=0, output freezes." Formula 4â€²s instructions said "k=1.0, no filtering, 2.0, 4.0 and 8.0 give increasing amounts of filtering with more dampening, but more delay."

Formula 3 is a straight running average, and seems to actually work pretty well. The current and previous values were used for an average of three; when using more there was more lag. Formula 5 also uses a running average. Three samples were used in the average also even though the intent was to use four or five. When four or five were used, the delayed reaction (lag) got worse.

The formal definition of a first order filter is represented by Formula 6. Sometimes this is known as a first derivative filter; if the points are equated to position, this would represent a velocity based filter. A second order filter would then represent Accel/Decel and a third order filter would represent "jerk."

The filters would work off of the delta between the current and previous values. Simulating formulas can be done very easily in Microsoft Excel, but the user may want to check things out before coding it into the PLC as illustrated by Formula 1.

Putting a formula into PLC form will vary a lot between platforms. In ladder logic, a "calculate" type command lets the user type the formula in directly, though you may still need to use first in, first out (FIFOs) instructions, especially when averaging.

**Frank Lamb **is the founder of Automation Consulting Services Inc. This article originally appeared on the Automation Primer blog. Automation Primer is a CFE Media content partner. Edited by Chris Vavra, production editor, *Control Engineering*, CFE Media, cvavra@cfemedia.com.

**ONLINE extra**

For more articles on PLCs and programming, see related articles below.