Ticket #1016: 0002-Implement-an-averaged-frequency-counter-to-get-a-mor.2.patch

File 0002-Implement-an-averaged-frequency-counter-to-get-a-mor.2.patch, 2.7 KB (added by sbte, 11 years ago)

Patch to take an average over the last 2 seconds

  • source/lib/frequency_filter.cpp

    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  
    2323#include "precompiled.h"
    2424#include "lib/frequency_filter.h"
    2525
    26 static const double errorTolerance = 0.05f;
     26static const double errorTolerance = 0.25;
    2727static const double sensitivity = 0.10;
     28static const double sampleSeconds = 2.0;
    2829
    2930/**
    3031 * variable-width window for frequency determination
    class FrequencyFilter : public IFrequencyFilter  
    192193public:
    193194    FrequencyFilter(double resolution, double expectedFrequency)
    194195        : 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))
    196198    {
    197199    }
    198200
    public:  
    205207        const int bias = m_controller.ComputeBias(m_smoothedFrequency, frequency);
    206208        m_smoothedFrequency = m_iirFilter(frequency, bias);
    207209
     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
    208222        // allow the smoothed FPS to free-run until it is no longer near the
    209223        // previous stable FPS value. round up because values are more often
    210224        // 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);
    214226    }
    215227
    216228    virtual double SmoothedFrequency() const
    private:  
    230242
    231243    int m_stableFrequency;
    232244    double m_smoothedFrequency;
     245    double m_averagedFrequency;
     246    int m_sampleAmount;
    233247};
    234248
    235249