<commit_before>package dsp_test

import (
	"fmt"

	"github.com/akualab/dsp"
	"github.com/akualab/dsp/wav"

	narray "github.com/akualab/narray/na64"
)

func ExampleSpectrum() {

	app := dsp.NewApp("Example App")

	// Read a waveform.
	path := "data/wav1.json.gz"
	wavSource, err := wav.NewSourceProc(path, wav.Fs(8000))
	if err != nil {
		panic(err)
	}

	// Add the source processor responsible for reading and supplying waveform samples.
	wav := app.Add("wav", wavSource)

	// Use a windowing processor to segment the waveform into frames of 80 samples
	// and apply a Hamming window of size 205. The last arg is to instruct the processor
	// to center the frame in the middle of the window.
	window := app.Add("window", dsp.NewWindowProc(80, 205, dsp.Hamming, true))

	// Compute the FFT of the windowed frame. The FFT size is 2**8.
	spectrum := app.Add("spectrum", dsp.SpectralEnergy(8))

	// Connect the processors.
	// wav -> window -> spectrum
	app.Connect(window, wav)
	app.Connect(spectrum, window)

	// Get features using this object.
	out := spectrum

	// Get the next waveform. (This processor is designed to read a list of
	// files. The Next() method loads the next waveform in the list.)
	wavSource.Next()

	// Get the spectrum for frame #10.
	v, e := out.Get(10)
	if e != nil {
		panic(e)
	}

	// Print first element of the FFT for frame #10.
	fmt.Println(v.Data[0])
	// Output:
	// 5.123420120893221e-05
}

// Calculate Fibonacci values.
func ExampleFibonacci() {
	app := dsp.NewApp("Fibonacci")
	fibo := app.Add("Fibo", Fibo(11))

	// Self loop.
	app.Connect(fibo, fibo)

	f10, e := fibo.Get(10)
	if e != nil {
		panic(e)
	}
	fmt.Println(f10.Data[0])
	// Output:
	// 89
}

// Subtract a value "fm" from a Fibonacci series so the sum adds up to zero.
func ExampleZMFibonacci() {
	app := dsp.NewApp("Fibonacci")
	fibo := app.Add("Fibo", Fibo(11))

	// Computes the mean of the series.
	fiboMean := app.Add("Fibo Mean", dsp.Mean())

	// Subtracts mean from fibo values.
	fiboZM := app.Add("ZM Fibo", dsp.Sub())

	// Make connections. Note that fiboMean is of tyep OneValuer and fibo is a Framer.
	// The reulting fiboZM is of type Framer. Values are computed only once and saved
	// in the processor cache.
	app.Connect(fibo, fibo)
	app.Connect(fiboMean, fibo)
	app.Connect(fiboZM, fibo, fiboMean)

	f10, e := fiboZM.Get(10)
	if e != nil {
		panic(e)
	}
	fmt.Println(f10.Data[0])
	// Output:
	// 67.9090909090909
}

// Fibo is a processor that returns the Fibonacci value for index.
// N is the length of the series.
func Fibo(N int) dsp.Processer {
	return dsp.NewProc(20, func(idx int, in ...dsp.Processer) (dsp.Value, error) {
		if idx == 0 || idx == 1 {
			na := narray.New(1)<commit_msg>Change example names using convention.<commit_after>package dsp_test

import (
	"fmt"

	"github.com/akualab/dsp"
	"github.com/akualab/dsp/wav"

	narray "github.com/akualab/narray/na64"
)

func ExampleApp_Spectrum() {

	app := dsp.NewApp("Example App")

	// Read a waveform.
	path := "data/wav1.json.gz"
	wavSource, err := wav.NewSourceProc(path, wav.Fs(8000))
	if err != nil {
		panic(err)
	}

	// Add the source processor responsible for reading and supplying waveform samples.
	wav := app.Add("wav", wavSource)

	// Use a windowing processor to segment the waveform into frames of 80 samples
	// and apply a Hamming window of size 205. The last arg is to instruct the processor
	// to center the frame in the middle of the window.
	window := app.Add("window", dsp.NewWindowProc(80, 205, dsp.Hamming, true))

	// Compute the FFT of the windowed frame. The FFT size is 2**8.
	spectrum := app.Add("spectrum", dsp.SpectralEnergy(8))

	// Connect the processors.
	// wav -> window -> spectrum
	app.Connect(window, wav)
	app.Connect(spectrum, window)

	// Get features using this object.
	out := spectrum

	// Get the next waveform. (This processor is designed to read a list of
	// files. The Next() method loads the next waveform in the list.)
	wavSource.Next()

	// Get the spectrum for frame #10.
	v, e := out.Get(10)
	if e != nil {
		panic(e)
	}

	// Print first element of the FFT for frame #10.
	fmt.Println(v.Data[0])
	// Output:
	// 5.123420120893221e-05
}

// Calculate Fibonacci values.
func ExampleApp_Fibonacci() {
	app := dsp.NewApp("Fibonacci")
	fibo := app.Add("Fibo", Fibo(11))

	// Self loop.
	app.Connect(fibo, fibo)

	f10, e := fibo.Get(10)
	if e != nil {
		panic(e)
	}
	fmt.Println(f10.Data[0])
	// Output:
	// 89
}

// Subtract a value "fm" from a Fibonacci series so the sum adds up to zero.
func ExampleOneValuer_ZMFibonacci() {
	app := dsp.NewApp("Fibonacci")
	fibo := app.Add("Fibo", Fibo(11))

	// Computes the mean of the series.
	fiboMean := app.Add("Fibo Mean", dsp.Mean())

	// Subtracts mean from fibo values.
	fiboZM := app.Add("ZM Fibo", dsp.Sub())

	// Make connections. Note that fiboMean is of tyep OneValuer and fibo is a Framer.
	// The reulting fiboZM is of type Framer. Values are computed only once and saved
	// in the processor cache.
	app.Connect(fibo, fibo)
	app.Connect(fiboMean, fibo)
	app.Connect(fiboZM, fibo, fiboMean)

	f10, e := fiboZM.Get(10)
	if e != nil {
		panic(e)
	}
	fmt.Println(f10.Data[0])
	// Output:
	// 67.9090909090909
}

// Fibo is a processor that returns the Fibonacci value for index.
// N is the length of the series.
func Fibo(N int) dsp.Processer {
	return dsp.NewProc(20, func(idx int, in ...dsp.Processer) (dsp.Value, error) {
		if idx == 0 || idx == 1 {
			na := narray.New(1)
{'fraction_non_alphanumeric': 0.13698881349715752, 'fraction_numerical': 0.031175499724922062, 'mean_word_length': 3.2877358490566038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1495368', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2023, 'n_words': 639}