ClearCore Library
Loading...
Searching...
No Matches
IirFilter.h
1/*
2 * Copyright (c) 2020 Teknic, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#ifndef __IIRFILTER_H__
24#define __IIRFILTER_H__
25
26#include <math.h>
27#include "SysTiming.h"
28
29#ifndef HIDE_FROM_DOXYGEN
30namespace ClearCore {
31
32//*****************************************************************************
33// NAME *
34// Iir16 class
35//
36// DESCRIPTION
45//
46class Iir16 {
47public:
48 Iir16(void) : m_tc(0), m_z(0) {};
49
53 void Update(uint16_t input) {
54 m_z = ((static_cast<int64_t>(m_z) * m_tc) >> 15) -
55 ((static_cast<int32_t>(input) * m_tc) << 1) +
56 (static_cast<int32_t>(input) << 16);
57 }
61 uint16_t LastOutput() {
62 return (m_z >> 16);
63 };
64
68 void Tc(uint16_t newTc) {
69 m_tc = newTc;
70 };
71
75 uint16_t Tc() {
76 return m_tc;
77 };
78
79 void TcSamples(uint16_t riseSamples99pct) {
80 float tcTemp = powf(.01, 1. / riseSamples99pct) * 32768 + 0.5;
81 m_tc = (tcTemp < INT16_MAX) ? tcTemp : INT16_MAX;
82 }
83
84 uint16_t TcSamples() {
85 return logf(0.01) / logf(m_tc / 32768.);
86 }
87
88 uint16_t Tc_ms() {
89 return TcSamples() / MS_TO_SAMPLES;
90 }
91
92 void Tc_ms(uint16_t riseMs99pct) {
93 TcSamples(riseMs99pct * MS_TO_SAMPLES);
94 }
95
96 // Reset the filter to this level
97 void Reset(uint16_t newSetting) {
98 m_z = (newSetting << 16);
99 }
100
101private:
102 uint16_t m_tc; // Filter time constant (positive)
103 int32_t m_z; // "Z" output/accumulator
104};
105
106} // ClearCore namespace
107#endif // HIDE_FROM_DOXYGEN
108#endif // #ifndef __IIRFILTER_H__
109// *
110//*****************************************************************************
ClearCore timing profiling utility functions.
#define MS_TO_SAMPLES
Definition SysTiming.h:49
Namespace to encompass the ClearCore board API.
Definition AdcManager.h:36