From 63eb126261cd3d433f1e301044262b6b35c7a1ca Mon Sep 17 00:00:00 2001
From: "Sven (Sbte)" <svenb.linux@gmail.com>
Date: Sun, 24 Feb 2013 17:23:31 +0100
Subject: Implement an averaged frequency counter to get a more stable FPS rate
---
source/lib/frequency_filter.cpp | 24 +++++++++++++++++++-----
1 file changed, 19 insertions(+), 5 deletions(-)
diff --git a/source/lib/frequency_filter.cpp b/source/lib/frequency_filter.cpp
index 4ae9905..dfe875c 100644
a
|
b
|
|
23 | 23 | #include "precompiled.h" |
24 | 24 | #include "lib/frequency_filter.h" |
25 | 25 | |
26 | | static const double errorTolerance = 0.05f; |
| 26 | static const double errorTolerance = 0.25; |
27 | 27 | static const double sensitivity = 0.10; |
| 28 | static const double sampleSeconds = 2.0; |
28 | 29 | |
29 | 30 | /** |
30 | 31 | * variable-width window for frequency determination |
… |
… |
class FrequencyFilter : public IFrequencyFilter
|
192 | 193 | public: |
193 | 194 | FrequencyFilter(double resolution, double expectedFrequency) |
194 | 195 | : m_frequencyEstimator(resolution), m_controller(expectedFrequency), m_iirFilter(sensitivity, expectedFrequency) |
195 | | , m_stableFrequency((int)expectedFrequency), m_smoothedFrequency(expectedFrequency) |
| 196 | , m_stableFrequency((int)expectedFrequency), m_smoothedFrequency(expectedFrequency), m_averagedFrequency(expectedFrequency) |
| 197 | , m_sampleAmount((int)(sampleSeconds * expectedFrequency)) |
196 | 198 | { |
197 | 199 | } |
198 | 200 | |
… |
… |
public:
|
205 | 207 | const int bias = m_controller.ComputeBias(m_smoothedFrequency, frequency); |
206 | 208 | m_smoothedFrequency = m_iirFilter(frequency, bias); |
207 | 209 | |
| 210 | // Keep an moving average of the frequency over the last two seconds |
| 211 | // If there is a spike of more than 25% (for example after the menu) |
| 212 | // then reset the moving average and reset the amount of samples needed |
| 213 | const double difference = fabs(m_smoothedFrequency - m_averagedFrequency); |
| 214 | if (difference > errorTolerance*m_averagedFrequency) |
| 215 | { |
| 216 | m_averagedFrequency = m_smoothedFrequency; |
| 217 | m_sampleAmount = (int)(m_averagedFrequency * sampleSeconds) + 1; |
| 218 | } |
| 219 | else |
| 220 | m_averagedFrequency = ((m_sampleAmount - 1.0) * m_averagedFrequency + m_smoothedFrequency) / (float)m_sampleAmount; |
| 221 | |
208 | 222 | // allow the smoothed FPS to free-run until it is no longer near the |
209 | 223 | // previous stable FPS value. round up because values are more often |
210 | 224 | // too low than too high. |
211 | | const double difference = fabs(m_smoothedFrequency - m_stableFrequency); |
212 | | if(difference > errorTolerance*m_stableFrequency) |
213 | | m_stableFrequency = (int)(m_smoothedFrequency + 0.99); |
| 225 | m_stableFrequency = (int)(m_averagedFrequency + 0.99); |
214 | 226 | } |
215 | 227 | |
216 | 228 | virtual double SmoothedFrequency() const |
… |
… |
private:
|
230 | 242 | |
231 | 243 | int m_stableFrequency; |
232 | 244 | double m_smoothedFrequency; |
| 245 | double m_averagedFrequency; |
| 246 | int m_sampleAmount; |
233 | 247 | }; |
234 | 248 | |
235 | 249 | |