• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - Automation
  • MovingAverage
  • 5.0.0

MovingAverage Class

(Automation::MovingAverage)

MovingAverage operator that averages input values over given Time to produce output. More...

Header: #include <MovingAverage>
Inherits: CDPOperator<T>
  • List of all members, including inherited members

Public Functions

MovingAverage(const CDPPropertyBase &in)
virtual ~MovingAverage()

Reimplemented Public Functions

virtual void Configure(XMLPrimitive *operatorXML) override
virtual void Create(const char *shortName, CDPBaseObject *pParent) override
virtual unsigned int Process() override
  • 18 public functions inherited from CDPOperator
  • 17 public functions inherited from CDPOperatorBase
  • 49 public functions inherited from CDPBaseObject
  • 27 public functions inherited from CDP::StudioAPI::CDPNode
  • 22 public functions inherited from CDP::StudioAPI::ICDPNode

Additional Inherited Members

  • 1 public variable inherited from CDPOperatorBase
  • 1 static public member inherited from CDPBaseObject
  • 1 protected function inherited from CDP::StudioAPI::CDPNode
  • 8 protected variables inherited from CDPOperator
  • 5 protected variables inherited from CDPOperatorBase
  • 10 protected variables inherited from CDPBaseObject

Detailed Description

MovingAverage operator that averages input values over given Time to produce output.

This operator can be used to calculate average (mean) of a value. Operator observes input values values in configurable amount of Time and calculates average value output based on input values in that "observation window".

Arguments

NameDescription
InThe default input value.
OutThe default output value.
TimeTime in seconds to take values into account while calculation average.

Note: If Time is zero the operator will do no averaging (operator output will be the same as input).

Note: If Time is negative the operator will not calculate output and alarm will be reported to parent component.

Properties

PropertyDescription
AverageTypeThree calculation types to choose from:
  • Simple - unweighted mean of the history window data. Simple average can also be used as simple Finite Impulse Response (FIR) filter.
  • Median - median average of history window data - value that has the same number of history window values below and above
  • Weighted - linearly weighted average of history window data - recent values have more effect in average than oldest
  • RMS - Root Mean Square of the history window data (square root of the average of squares of the history values)
  • Cumulative - unweighted mean of all data since system start.

    Note: HistoryWindowTime argument is not affecting this mode.

ResetClears average calculation. When toggled from 0 to 1 calculation "observation window" is cleared and average is set to input value.

When operator is used inside a signal its default input is automatically tied to signal's InternalValue or previous operator's output. Its default output is automatically tied to next operator's input or to signal's Value. See also CDP Operator Usage In CDP Signals.

Note: This operator can't be used in I/O Servers / CDPChannels because it requires to be run with a constant period. I/O Servers can't always guarantee periodic running since they may be in Offline state, or they can send/receive on change.

Examples

Simple Moving Average Example

Below is an example plot with the MovingAverage(HistoryWindowTime=10, AverageType="Simple") (orange) of the Sine(Frequency=0.1 Hz) signal (cyan) in input. Note, that as the HistoryTimeWindow is exactly the same as sine cycle (10 s) then value will be averaged to 0 after one sine cycle.

Median Moving Average Example

Below is an example plot with the MovingAverage(HistoryWindowTime=0.5, AverageType="Median") (orange) of the noisy Sine(Frequency=0.1 Hz) signal (cyan) in input. Note, that the Median average can successfully be used to filter out random noise.

Weighted Moving Average Example

Below is an example plot with the MovingAverage(HistoryWindowTime=10, AverageType="Weighted") (orange) of the Sine(Frequency=0.1 Hz) signal (cyan) in input. Note, that in that case output will never be averaged to 0 because of the HistoryWindowTime youngest values contribute more than the oldest values.

Cumulative Average Example

Below is an example plot with the MovingAverage(HistoryWindowTime=10, AverageType="Cumulative") (orange) of the Sine(Frequency=0.1 Hz) signal (cyan) in input. Note, that in that case the average will approach 0 very slowly and continuously because all sine values are taken into account (not only those that were evaluated during HistoryWindowTime).

RMS Average Example

Below is an example plot with the MovingAverage(HistoryWindowTime=10, AverageType="RMS") applied, shown as orange, while the input signal, Sine(Frequency=0.1 Hz, Amplitude=1), is shown as cyan. Note, that since the HistoryTimeWindow is exactly the same as the sine cycle (10 s), then the value will be approximately 0.707, which is the constant for RMS / amplitude for any sine wave.

Actual Processing Code of the MovingAverage

unsigned int MovingAverage<T>::Process()
{
  auto windowSize = d->valueHistory->GetWindowTime();
  if (windowSize < 0.0)
  {
    GetParent()->ReportConfigurationFault(this, "Negative window Time");
    d->faultReported = true;
    return STATUS_SIGNAL_FAULT;
  }
  if (d->valueHistory->GetReset())
    d->ResetCalculation();

  d->valueHistory->PushValue((T)m_input);

  m_output = d->CalculateAverage((T)m_input);

  if (d->faultReported)
  {
    GetParent()->ClearConfigurationFault(this);
    d->faultReported = false;
  }

  return STATUS_OK;
}

See also Argument.

Member Function Documentation

MovingAverage::MovingAverage(const CDPPropertyBase &in)

Constructs a MovingAverage with input in.

[virtual] MovingAverage::~MovingAverage()

Destructs the MovingAverage operator

[override virtual] void MovingAverage::Configure(XMLPrimitive *operatorXML)

Reimplemented from CDPBaseObject::Configure().

[override virtual] void MovingAverage::Create(const char *shortName, CDPBaseObject *pParent)

Reimplemented from CDPBaseObject::Create().

[override virtual] unsigned int MovingAverage::Process()

Reimplemented from CDPOperatorBase::Process().

Returns STATUS_OK.

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top