pointybeard/helpers-statistics-slidingaverage

Ring buffer for calculating average of a changing set; helps to reduce jitter.

1.0.2 2019-05-05 10:13 UTC

This package is auto-updated.

Last update: 2024-12-05 23:10:32 UTC


README

Ring buffer for calculating average of a changing set; helps to reduce jitter.

Installation

This library is installed via Composer. To install, use composer require pointybeard/helpers-statistics-slidingaverage or add "pointybeard/helpers-statistics-slidingaverage": "~1.0" to your composer.json file.

And run composer to update your dependencies:

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update

Requirements

There are no particuar requirements for this library other than PHP 5.6 or greater.

To include all the PHP Helpers packages on your project, use composer require pointybeard/helpers or add "pointybeard/helpers": "~1.0" to your composer file.

Usage

Simple example of how to use the SlidingAverage class:

<?php

include __DIR__ . "/vendor/autoload.php";

use pointybeard\Helpers\Statistics\SlidingAverage;

$average = new SlidingAverage\SlidingAverage(20, 0);

do {

    // Do some work here
    $value = rand(0, 100);

    // Push some value into the ring buffer, e.g. time elapsed etc
    $average->push($value);

    echo "\rAverage is: " . $average->sample();
    usleep(70000);

} while(true);

Support

If you believe you have found a bug, please report it using the GitHub issue tracker, or better yet, fork the library and submit a pull request.

Contributing

We encourage you to contribute to this project. Please check out the Contributing documentation for guidelines about how to get involved.

Credits

Credit to Alexey Volynskov's tutorial, and C# implementation, Quick Tip: Use the "Ring Buffer" Data Structure to Smooth Jittery Values. The inspiration for this class comes from that tutorial.

License

"PHP Helpers: Sliding Average" is released under the MIT License.