Matstat library declarations. More...

Detailed Description

Matstat library declarations.

The Matstat library uses single pass algorithms to compute statistic measures such as mean and variance over many values. The values can be immediately discarded after processing, keeping the memory requirement constant regardless of how many values need to be processed.

The design goal is to provide basic mathematical statistics operations on constrained devices with a "good enough" accuracy to be able to provide some descriptive measures of data. For more accurate measures of statistics, use a fancier library, or copy the data to a PC.

It is important to know that using integer operations will result in lower precision in the computed measures because of truncation.

Author
Joakim NohlgÄrd joaki.nosp@m.m.no.nosp@m.hlgar.nosp@m.d@ei.nosp@m.stec..nosp@m.se

Definition in file matstat.h.

#include <stdint.h>
+ Include dependency graph for matstat.h:

Go to the source code of this file.

Data Structures

struct  matstat_state_t
 Internal state for computing running statistics. More...
 
#define MATSTAT_STATE_INIT
 Empty state initializer. More...
 
void matstat_clear (matstat_state_t *state)
 Reset state. More...
 
void matstat_add (matstat_state_t *state, int32_t value)
 Add a sample to state. More...
 
static int32_t matstat_mean (const matstat_state_t *state)
 Return the computed mean value of all samples so far. More...
 
uint64_t matstat_variance (const matstat_state_t *state)
 Compute the sample variance of all samples so far. More...
 
void matstat_merge (matstat_state_t *dest, const matstat_state_t *src)
 Combine two states. More...
 

Macro Definition Documentation

◆ MATSTAT_STATE_INIT

#define MATSTAT_STATE_INIT
Value:
(const matstat_state_t) { \
.sum = 0, \
.sum_sq = 0, \
.count = 0, \
.mean = 0, \
.min = INT32_MAX, \
.max = INT32_MIN, \
}

Empty state initializer.

Definition at line 56 of file matstat.h.

Function Documentation

◆ matstat_add()

void matstat_add ( matstat_state_t state,
int32_t  value 
)

Add a sample to state.

Parameters
[in]stateState struct to operate on
[in]valueValue to add to the state

◆ matstat_clear()

void matstat_clear ( matstat_state_t state)

Reset state.

Parameters
[in]stateState struct to clear

◆ matstat_mean()

static int32_t matstat_mean ( const matstat_state_t state)
inlinestatic

Return the computed mean value of all samples so far.

Parameters
[in]stateState struct to operate on
Returns
arithmetic mean

Definition at line 87 of file matstat.h.

◆ matstat_merge()

void matstat_merge ( matstat_state_t dest,
const matstat_state_t src 
)

Combine two states.

Add the sums and count of src and dest, take the maximum of the max values and minimum of the min values. The result is written to dest.

Parameters
[in,out]destdestination state struct
[out]srcsource state struct

◆ matstat_variance()

uint64_t matstat_variance ( const matstat_state_t state)

Compute the sample variance of all samples so far.

Parameters
[in]stateState struct to operate on
Returns
sample variance
matstat_state_t
Internal state for computing running statistics.
Definition: matstat.h:44