wavegen

package
v0.0.0-...-9f956ec Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package wavegen is used for generating synthetic test data for the Dropbear LSTM project.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sample

type Sample struct {

	// The time component of the sample
	T float64

	// The value component of the sample
	S float64
}

Sample represents a single sample from a Signal

type SampleList

type SampleList []Sample

func (SampleList) Len

func (l SampleList) Len() int

Len implements gonum's plotter.XYer interface

func (SampleList) ToSignal

func (l SampleList) ToSignal() *Signal

ToSignal converts a list of samaples to a Signal object, and sets the sample rate thereof to their average sample rate.

func (SampleList) XY

func (l SampleList) XY(i int) (float64, float64)

XY implements gonum's plotter.XYer interface

type Signal

type Signal struct {
	// signal values
	S []float64 `json:"samples"`

	T []float64 `json:"times"`

	// sample rate in Hz, note that this is set by the caller, this library
	// cannot guarantee the accuracy of this value
	SampleRate float64
}

Signal represents a time-series signal

When generating or modifying a signal, you must guarantee that the T and S values are sorted by T (e.g. earlier points in time have lower indices), and that T ans S are the same size.

func (*Signal) AverageSampleRate

func (s *Signal) AverageSampleRate() float64

AverageSampleRate calculates the average sample rate in Hz of the signal. This can be useful when dealing with real signals, rather than those generated using wavegen.

func (*Signal) Duration

func (s *Signal) Duration() float64

Duration returns the number seconds of data in the signal.

func (*Signal) Index

func (s *Signal) Index(i int) (Sample, error)

Index retrieves the ith sample value.

func (*Signal) Interpolate

func (s *Signal) Interpolate(times ...float64) []Sample

Interpolate can be used to perform linear interpolation. It will compute a separate linear interpolation for each point in times, and return an appropriate sample for each.

func (*Signal) Len

func (s *Signal) Len() int

Len implements gonum's plotter.XYer interface

func (*Signal) MustIndex

func (s *Signal) MustIndex(i int) Sample

MustIndex works identically to Index(), but calls panic() if an error occurs

func (*Signal) NearestIndex

func (s *Signal) NearestIndex(time float64, overshoot bool) int

NearestIndex will return the index within a signal which has a time value as close as possible to the specified time argument. It will return an index with a greater or equal value if overshoot is true, and a lesser or equal value if overshoot is false. It will return 0 if the time value is before the beginning of the signal, and s.Size()-1 if the time value is after the end of the signal.

Note that time includes the offset, so time=0 when the offset is 1 second will return 0, since it is before the beginning of the signal data.

func (*Signal) Size

func (s *Signal) Size() int

Size returns the number of samples which a Signal contains.

func (*Signal) Summarize

func (s *Signal) Summarize() (string, error)

func (*Signal) ValidateIndex

func (s *Signal) ValidateIndex(i int) error

ValidateIndex ensures that the specified index can be retrieved from the signal, and generates an error if not.

func (*Signal) XY

func (s *Signal) XY(i int) (float64, float64)

XY implements gonum's plotter.XYer interface

type WaveFile

type WaveFile struct {
	// Version should be the version specifier, currently 0.
	Version int

	// Parameters represents the WaveParameters.
	Parameters *WaveParameters `json:",omitempty"`

	// Signal represents the wave data.
	Signal *Signal `json:",omitempty"`
}

WaveFile represents all of the data that could be encoded in a WaveGen JSON file.

func FromJSON

func FromJSON(data []byte) (*WaveFile, error)

FromJSON loads the data stored in a JSON representation of a wavegen file.

func ReadJSON

func ReadJSON(path string) (*WaveFile, error)

ReadJSON loads the data stored in a JSON file on disk to a wavegen file object.

func (*WaveFile) ToJSON

func (wf *WaveFile) ToJSON() ([]byte, error)

ToJSON converts a WaveFile to an in-memory JSON representation and returns it.

func (*WaveFile) WriteJSON

func (wf *WaveFile) WriteJSON(path string) error

WriteJSON generates a JSON representation using ToJSON(), then writes it to disk.

type WaveParameters

type WaveParameters struct {
	// SampleRate is the sample rate at which the wave should be generated,
	// in Hz
	SampleRate float64

	// Offset is the time at which samples should begin being collected, in
	// seconds
	Offset float64

	// Duration is the number of seconds of samples that should be generated
	Duration float64

	// Frequencies is the list of frequencies of Sin wave that should be
	// generated
	Frequencies []float64

	// Phases is the list of phases of the Sin waves that should be
	// generated
	Phases []float64

	// Amplitudes is the list of amplitudes of Sin that should be generated
	Amplitudes []float64

	// Noises stores a list of noise functions which are applied on a
	// per-signal basis.  if this field is left empty, then no noise will
	// be generated for the given signal. These noise functions are
	// understood:
	//
	// * "pseudo" (rand.Float64)
	// * "none" (no noise)
	// * "" (no noise)
	Noises []string

	// NoiseMagnitues is a list of coefficients to the given noise function
	// for a particular signal.  If empty, it is assumed that all
	// magnitudes are 1.0.
	NoiseMagnitudes []float64

	// GlobalNoise accepts the same values as Noises, but are applied
	// globally rather than to a specific signal.
	GlobalNoise string

	// GlobalNoiseMagnitude works similarly to NoiseMagnitudes, but applies
	// to the global noise.
	GlobalNoiseMagnitude float64
}

WaveParameters is used to store the parameters that generate a particular wave. See GenerateSyntheticData().

func (*WaveParameters) GenerateSyntheticData

func (w *WaveParameters) GenerateSyntheticData() (*Signal, error)

GenerateSyntheticData generates a signal which is a composition of several Sin functions of the given frequencies, phases, and amplitudes, with noise optionally applied to each signal, and optionally applied to the data overall.

func (*WaveParameters) Noise

func (w *WaveParameters) Noise(index int) (float64, error)

Noise generates a randomized noise value for the index-th component of the wave parameter. An index of -1 indicates that the global noise should be generated instead.

func (*WaveParameters) Summarize

func (w *WaveParameters) Summarize() (string, error)

func (*WaveParameters) ValidateParameters

func (w *WaveParameters) ValidateParameters() error

ValidateParameters will ensure that the parameters are valid.

If the noises or noise magnitudes are empty, then they will be filled to an appropriate length with default values.

Jump to

Keyboard shortcuts

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