# Ladder logic 402: Data manipulation and filtering

## Once values from an analog card 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.

03/29/2016

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).

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.

WILLIAM , CA, United States, 05/04/16 10:15 AM:

Simplest is #6, usually written: xf = w*x + (1-w)xfL, where xf = filtered value, x = current reading, xfL = last xf (at last sample), w= weighting factor. This approximates an analog 1st-order low-pass filter (R-C) and w can be related to a time constant. Simplest because you need store only the last value. Many engineers term "running average", but Statistics terms "exponential averaging". I term #3 "block average" or "moving window average".
The Engineers' Choice Awards highlight some of the best new control, instrumentation and automation products as chosen by...
The System Integrator Giants program lists the top 100 system integrators among companies listed in CFE Media's Global System Integrator Database.
Each year, a panel of Control Engineering and Plant Engineering editors and industry expert judges select the System Integrator of the Year Award winners in three categories.
This eGuide illustrates solutions, applications and benefits of machine vision systems.
Learn how to increase device reliability in harsh environments and decrease unplanned system downtime.
This eGuide contains a series of articles and videos that considers theoretical and practical; immediate needs and a look into the future.
Controller programming; Safety networks; Enclosure design; Power quality; Safety integrity levels; Increasing process efficiency
Additive manufacturing benefits; HMI and sensor tips; System integrator advice; Innovations from the industry
Robotic safety, collaboration, standards; DCS migration tips; IT/OT convergence; 2017 Control Engineering Salary and Career Survey
Featured articles highlight technologies that enable the Industrial Internet of Things, IIoT-related products and strategies to get data more easily to the user.
This article collection contains several articles on how automation and controls are helping human-machine interface (HMI) hardware and software advance.
This digital report will explore several aspects of how IIoT will transform manufacturing in the coming years.
Infrastructure for natural gas expansion; Artificial lift methods; Disruptive technology and fugitive gas emissions
Mobility as the means to offshore innovation; Preventing another Deepwater Horizon; ROVs as subsea robots; SCADA and the radio spectrum
Future of oil and gas projects; Reservoir models; The importance of SCADA to oil and gas
Automation Engineer; Wood Group
System Integrator; Cross Integrated Systems Group
Jose S. Vasquez, Jr.
Fire & Life Safety Engineer; Technip USA Inc.
This course focuses on climate analysis, appropriateness of cooling system selection, and combining cooling systems.
This course will help identify and reveal electrical hazards and identify the solutions to implementing and maintaining a safe work environment.
This course explains how maintaining power and communication systems through emergency power-generation systems is critical.