time

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package time provides time-domain statistical metrics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CrestFactor

func CrestFactor(signal []float64) float64

CrestFactor returns the crest factor (peak / RMS) of the signal. Returns 0 if RMS is zero.

func DC

func DC(signal []float64) float64

DC returns the mean (DC offset) of the signal.

func Moments

func Moments(signal []float64) (mean, variance, skewness, kurtosis float64)

Moments returns the mean, population variance, skewness, and excess kurtosis of the signal using Welford's online algorithm for numerical stability.

func Peak

func Peak(signal []float64) float64

Peak returns the peak absolute amplitude of the signal.

func RMS

func RMS(signal []float64) float64

RMS returns the root-mean-square of the signal.

func ZeroCrossings

func ZeroCrossings(signal []float64) int

ZeroCrossings returns the number of zero crossings in the signal. A crossing is counted when consecutive samples have opposite signs.

Types

type Stats

type Stats struct {
	Length         int
	DC             float64 // mean
	DC_dB          float64
	RMS            float64
	RMS_dB         float64
	Max            float64
	MaxPos         int
	Min            float64
	MinPos         int
	Peak           float64 // max(|max|, |min|)
	Peak_dB        float64
	Range          float64 // max - min
	Range_dB       float64
	CrestFactor    float64 // peak / RMS (linear)
	CrestFactor_dB float64
	Energy         float64 // sum of squares
	Power          float64 // energy / length
	ZeroCrossings  int
	Variance       float64
	Skewness       float64
	Kurtosis       float64
}

Stats holds time-domain signal statistics.

func Calculate

func Calculate(signal []float64) Stats

Calculate computes all time-domain statistics in a single pass using Welford's online algorithm for numerical stability on higher-order moments.

Example
package main

import (
	"fmt"

	timestats "github.com/cwbudde/algo-dsp/stats/time"
)

func main() {
	s := timestats.Calculate([]float64{1, -1, 1, -1})
	fmt.Printf("rms=%.1f zc=%d\n", s.RMS, s.ZeroCrossings)

}
Output:
rms=1.0 zc=3

type StreamingStats

type StreamingStats struct {
	// contains filtered or unexported fields
}

StreamingStats accumulates time-domain statistics incrementally across multiple blocks of samples. It processes each sample individually to guarantee bit-for-bit identical results with Calculate.

Example
package main

import (
	"fmt"

	timestats "github.com/cwbudde/algo-dsp/stats/time"
)

func main() {
	s := timestats.NewStreamingStats()
	s.Update([]float64{1, -1})
	s.Update([]float64{1, -1})
	m := s.Result()
	fmt.Printf("len=%d dc=%.1f\n", m.Length, m.DC)

}
Output:
len=4 dc=0.0

func NewStreamingStats

func NewStreamingStats() *StreamingStats

NewStreamingStats creates a new StreamingStats accumulator.

func (*StreamingStats) Reset

func (s *StreamingStats) Reset()

Reset clears all accumulated data, allowing the StreamingStats to be reused.

func (*StreamingStats) Result

func (s *StreamingStats) Result() Stats

Result computes the final statistics from accumulated data.

func (*StreamingStats) Update

func (s *StreamingStats) Update(samples []float64)

Update adds a block of samples to the running statistics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL