FlowManager Documentation Beta

Structure Ring​Average

public struct RingAverage

Implement a reusable Circular Buffer logic for calculating average value on a Ring flow of data.

A generic Circular Buffer (Ring) data structure. It enables to construct dinamically sized buffer and to calculate average value of the buffer for each new sample data added. Average function is a completely generic function ([Double]) -> Double passed to the init method

Example:

var ring = RingAverage(size: 5, averageFunc: { $0.reduce(0, +) / Double($0.count) }) let average = ring.addSample(1.0)

Nested Type Aliases

Average​Function​Definition

public typealias AverageFunctionDefinition = ([Double]) -> Double

The closures / function for dynamically calculating the average with a dynamic algorithm.

Initializers

init(size:​average​Func:​)

public init(size: Int = 50, averageFunc: @escaping AverageFunctionDefinition = RingAverage.simpleAverageFunction)

Initializes a RingAverage Circular Buffer.

Parameters

size Int

The size of the Circular Buffer.

average​Func @escaping Average​Function​Definition

The clousure / function for dynamically calculating the average value.

Methods

ssdn​Average​Function(_:​)

public static func ssdnAverageFunction(_ samples: [Double]) -> Double

Return the HRV SSDN average value on a list of BPM samples. Circular Buffer average function to be used with the RingAverage struct.

SDNN formula from https://www.kubios.com/about-hrv/

simple​Average​Function(_:​)

public static func simpleAverageFunction(_ samples: [Double]) -> Double

Return the mean average value on a list of samples. Circular Buffer average function to be used with the RingAverage struct.

reset()

public mutating func reset()

Reset the Circular Buffer.

add​Sample(_:​)

public mutating func addSample(_ value: Double) -> Double

Return the new average value of the Circular Buffer while adding a new sample value to.

Parameters

value Double

A new value to be added to the Circular Buffer.