1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "movingaverage_p.h" |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | namespace Qt3DInput { |
9 | namespace Input { |
10 | |
11 | MovingAverage::MovingAverage(unsigned int samples) |
12 | : m_maxSampleCount(samples) |
13 | , m_sampleCount(0) |
14 | , m_currentSample(0) |
15 | , m_total(0.0f) |
16 | , m_samples(samples) |
17 | { |
18 | } |
19 | |
20 | void MovingAverage::addSample(float sample) |
21 | { |
22 | if (m_sampleCount == m_maxSampleCount) |
23 | m_total -= m_samples[m_currentSample]; |
24 | else |
25 | ++m_sampleCount; |
26 | |
27 | m_samples[m_currentSample] = sample; |
28 | m_total += sample; |
29 | ++m_currentSample; |
30 | if (m_currentSample >= m_maxSampleCount) |
31 | m_currentSample = 0; |
32 | } |
33 | |
34 | float MovingAverage::average() const |
35 | { |
36 | return m_sampleCount ? m_total / static_cast<float>(m_sampleCount) : 0.0f; |
37 | } |
38 | |
39 | } // Input |
40 | } // Qt3DInput |
41 | |
42 | QT_END_NAMESPACE |
43 |