2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.pidcontroller.internal;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * Realizes a first-order FIR low pass filter. To keep code complexity low, it is implemented as moving average (all
19 * FIR coefficients are set to normalized ones).
21 * The exponential decaying function is used for the calculation (see https://en.wikipedia.org/wiki/Time_constant). That
22 * means the output value is approx. 63% of the input value after one time constant and approx. 99% after 5 time
25 * @author Fabian Wolter - Initial contribution
29 public class LowpassFilter {
31 * Executes one low pass filter step.
33 * @param lastOutput the current filter value (result of the last invocation)
34 * @param newValue the just sampled value
35 * @param timeQuotient quotient of the current time and the time constant
36 * @return the new filter value
38 public static double calculate(double lastOutput, double newValue, double timeQuotient) {
39 double output = lastOutput * Math.exp(-timeQuotient);
40 output += newValue * (1 - Math.exp(-timeQuotient));