]> git.basschouten.com Git - openhab-addons.git/blob
cd9d0bdab665d20dac066c7cdf49877dd87a2e31
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.pidcontroller.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
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).
20  *
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
23  * constants.
24  *
25  * @author Fabian Wolter - Initial contribution
26  *
27  */
28 @NonNullByDefault
29 public class LowpassFilter {
30     /**
31      * Executes one low pass filter step.
32      *
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
37      */
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));
41
42         return output;
43     }
44 }