1.0.67 • Published 5 days ago

@thi.ng/colored-noise v1.0.67

Weekly downloads
34
License
Apache-2.0
Repository
github
Last release
5 days ago

colored-noise

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Customizable O(1) ES6 generators for colored noise.

Colored noise is useful for a wide variety of use cases where a more controlled approach to random number generation is required, from DSP/synthesis to rendering, geometry generation, animation etc.

The following noise colors are available and illustrated via their power spectrums / histograms (averaged 1000 runs @ 256 samples each). Each noise gen is configurable in terms of number of internal RNG states, value range (default: [-1..1) interval, always centered around 0) and the actual backing PRNG implementation (default: Math.random, see @thi.ng/random for alternatives).

Code ported from Java, C & Clojure implementations and other references/literature (see links in doc strings). Furthermore, all generators have been refactored to be O(1).

In alphabetical order:

Blue noise

High-pass filtered noise (opposite of red) - Source

blue2
                               ▁▁▁▂▂▂▃▄▄▄▅▅▅▅▅▆▆▆▆▇▇▇▇▇▇▇█▇▇███▇
                  ▁▁▂▃▄▄▅▆▇▆████████████████████████████████████
            ▂▃▄▅▇███████████████████████████████████████████████
        ▃▆▇█████████████████████████████████████████████████████
    ▁▅▇█████████████████████████████████████████████████████████
  ▂▆████████████████████████████████████████████████████████████
▂▇██████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

blue4
                                                 ▁▁▂▄▄▄▆▆▇█▇▆██▇
                                           ▁▃▄▇▇████████████████
                                       ▁▄▆██████████████████████
        ▁▄▅▆▇▇███████▇▇▆▄▃▁          ▄▇█████████████████████████
     ▃▅████████████████████▇▅      ▄████████████████████████████
   ▄██████████████████████████▄  ▁██████████████████████████████
 ▂██████████████████████████████▇███████████████████████████████
▃███████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

blue16

                                                             ▃▅▇
                                                           ▂████
                                                          ▄█████
                                                   ▂▅▅▃  ▂██████
                                           ▂▅▄▁   ▆████▅▁███████
                    ▂      ▁▄▃    ▁▅▇▇▃   ▆████▂ ▅██████████████
  ▂▆█▆▂   ▂▆█▆▃   ▄███▅   ▆███▇  ▁█████▄ ▆██████▅███████████████
 ▃█████▄ ▃█████▄ ▅█████▆ ▇██████▁███████▇███████████████████████
 ███████▅███████▆███████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

blue32

                                                               ▆
                                                              ██
                                                             ▅██
                                                          ▅▃ ███
                                                      ▃  ▆██████
                                              ▂   ▇▃ ▆██▃███████
                                  ▂   ▅  ▂▇▃ ▅█▆ ███▄███████████
  ▃   ▃   ▄   ▄   ▄  ▁▇▂ ▂█▃ ▄█▄ ▆█▇ ███▁███▄███▇███████████████
 ▇██ ▇██ ███ ███ ███▁███▂███▄███▅███▇███████████████████████████
 ███████▇███████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

Green noise

Band-pass filtered noise (interleaved blue noise, opposite of violet) - Source

green4

                         ▂▃▅▆▇▇█▇▇▇▆▅▄▃▂
                      ▃▅█████████████████▆▃
                    ▄███████████████████████▄          ▁▁
    ▂▅▇█████▅▁    ▂███████████████████████████▁    ▂▅▇████▇▅▂
  ▁▆██████████▅  ▂█████████████████████████████▂  ▅██████████▆▁
 ▂██████████████▅███████████████████████████████▆██████████████▂
▁███████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
green16

                               ▅▆▅
                              ████▇
                             ▅█████▆
                          ▄▃ ███████ ▃▄
                      ▄▁ ▆██████████▇██▆ ▁▄
          ▁   ▃  ▁▇▄ ▇██▄███████████████▄██▇ ▃█▁  ▄   ▁
 ▂▇▃ ▃█▄ ▅█▆ ▇██ ███▅███████████████████████▅███▁███ ▅█▅ ▄█▄ ▄▇▂
 ███▃███▄███▆███████████████████████████████████████▆███▄███▃███
▅███████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
green32

                                ▆
                               ███
                               ███
                             ▅▆███▆▄
                           ▅ ███████ ▄
                       ▂ ▇▂███████████▂▆ ▁
                 ▁ ▄ ▇▁█▆███████████████▆█▂▇ ▄ ▂
 ▄ ▃ ▄ ▄ ▅ ▆ ▇▁█▃█▅█▇███████████████████████▇█▄█▃█▁▇ ▆ ▅ ▄ ▃ ▃ ▃
 █▅█▅█▅█▆█▇███████████████████████████████████████████▇█▇█▅█▅█▄█
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

Pink noise

Exponential decay power curve (1/f) - Source

pink4
 ▅▄▂
▇████▇▅▃▂
████████████▇▆▄▃▁▁▁▁▂
████████████████████████▇▆▅▄▃▂▂     ▁
████████████████████████████████▇████████████▇▆▅▄▄▄▄▂▃▁▁
█████████████████████████████████████████████████████████▇▆▅▄▁
██████████████████████████████████████████████████████████████▇▃
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

pink16
█
█
██
███▆▁▁
██████▇▅▁▂▁
████████████▇▆▆▄▂▂▂▂▁▁
███████████████████████▇▇▆▅▅▄▃▂      ▁
████████████████████████████████▄███████████▇▆▅▄▃▃▄▃▂▂▁
████████████████████████████████████████████████████████▆▇▆▅▂▁
██████████████████████████████████████████████████████████████▆▁
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

pink32
▄
█
█
█▄
██▄▁
████▅▄▂
████████▆▇▆▄▂▁
████████████████▅▇▆▆▆▅▄▃▂▁▂
██████████████████████████████▆▅ ▄▄▅▃▅▄▄▂▃▄▃▂▂
████████████████████████████████████████████████▇▇▇▇▇▇▆▅▂▂▂
████████████████████████████████████████████████████████████▅▄▁
███████████████████████████████████████████████████████████████▅

Red / brown noise

Low-pass filtered noise (opposite of blue) - Source

red4

 █▇▇▇▆▇▆▅▅▅▄▃▃▂▁
█████████████████▇▆▅▃▁
███████████████████████▆▃
███████████████████████████▄          ▁▃▄▆▇█████████▇▆▅▃▁
█████████████████████████████▄     ▁▄▇████████████████████▆▃
██████████████████████████████▇▁  ▄██████████████████████████▄
████████████████████████████████▇██████████████████████████████▁
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

red16

 ▇▅▃
█████▃
██████▃
███████▂  ▂▅▅▂
████████ ▅████▅    ▄▄▂
███████████████▅ ▂████▆   ▃▇█▆▁    ▃▃▂      ▁
████████████████▅██████▇ ▄█████▁ ▁▇███▆   ▄███▄   ▃▇█▇▂   ▂▆█▆▂
████████████████████████▇███████▂██████▆ ▆█████▅ ▄█████▃ ▃█████▃
████████████████████████████████████████████████▆███████▅███████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

red32

 ▅
██▇
███▅
████ ▃▅
████▇██▆ ▁▃
████████▂██▇ ▂▇▁  ▁
████████████▄███ ▆█▅ ▃▆▂  ▄   ▁
████████████████████▄███▁███ ▇█▅ ▄█▄ ▃▇▂ ▁▆▁ ▁▅   ▄   ▄   ▃   ▃
████████████████████████████▇███▄███▃███▁███ ███ ███ ███ ▇██ ▇█▇
████████████████████████████████████████████████▇███▇███▇███▇███
████████████████████████████████████████████████████████████████

Violet noise

Band-stop filtered noise (interleaved red noise, opposite of green) - Source

violet2

 ██▇▇█▆▇▆▆▅▅▃▃▃▂▁                               ▁▂▃▄▄▅▆▆▇▆▆▇▇█▇█
███████████████████▇▅▄▂                  ▁▂▄▅▇▇█████████████████
█████████████████████████▅▃           ▃▅████████████████████████
████████████████████████████▄       ▄███████████████████████████
██████████████████████████████▄   ▄█████████████████████████████
████████████████████████████████▆███████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

violet4

▂▇▇▇▆▄▂▂                                                 ▂▂▄▆▇▇▇
█████████▆▃                                           ▃▆████████
████████████▄                            ▁          ▄███████████
██████████████▁    ▂▅▇████▇▅▂       ▂▄▇████▇▅▁    ▁▇████████████
███████████████▂  ▅██████████▆▁   ▂▇██████████▅  ▁██████████████
████████████████▅██████████████▃ ▃██████████████▅███████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

violet16

 ▅                                                             ▅
███                                                           ██
███▆                                                         ▅██
████ ▃▅                                                   ▅▃ ███
███████▆ ▁▅                                           ▃▂ ▆██████
████████▃██▇ ▄▇▂  ▄   ▂                   ▁   ▄  ▂█▄ ▇██▃███████
████████████▅███▁██▇ ▆█▅ ▄█▄ ▂▇▃ ▃█▂ ▄█▃ ▅█▆ ▇██ ███▅███████████
████████████████████▆███▅███▄███▃███▃███▄███▆███████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

violet32

██                                                             █
██                                                             █
██▄▄                                                         ▄▆█
████ ▃                                                     ▃ ███
██████▂▆ ▁                                             ▂ ▆▂█████
████████▅█▂▇ ▄ ▂                                 ▂ ▄ ▇▁█▅███████
████████████▇█▅█▂█▁▇ ▇ ▅ ▄ ▃ ▃ ▃ ▃ ▃ ▄ ▄ ▅ ▆ ▇▁█▃█▄█▇███████████
██████████████████████▇█▇█▆█▆█▅█▅█▅█▅█▆█▆███████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

White noise

Uniform distribution (unfiltered output from source PRNG) - Source

white

▁█████████▇█████████▇█████▇▇▇█▇██▇▇█▇████████▇█▇███▇▇█▇█████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████
████████████████████████████████████████████████████████████████

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/dsp - Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils
  • @thi.ng/lowdisc - n-dimensional low-discrepancy sequence generators/iterators
  • @thi.ng/random - Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation

Installation

yarn add @thi.ng/colored-noise

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/colored-noise"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const coloredNoise = await import("@thi.ng/colored-noise");

Package sizes (gzipped, pre-treeshake): ESM: 506 bytes

Dependencies

API

Generated API docs

TODO

See /tools for usage examples...

import { red } from "@thi.ng/colored-noise";
import { take } from "@thi.ng/transducers";

// default config
[...take(20, red())]
// [0.03108920908982049, 0.5411110045940151, 0.26064470417452945, ...]

// custom config
import { XsAdd } from "@thi.ng/random";

[...take(20, red(16, 1, new XsAdd(0xdecafbad)))]
// [ -0.17761799097704192, -0.10240132532836904, -0.1103387340810939, ...]

Use as IGen in @thi.ng/dsp

import { green } from "@thi.ng/colored-noise";
import { adsr, iterable, product } from "@thi.ng/dsp";
import { wavByteArray } from "@thi.ng/dsp-io-wav";
import { writeFileSync } from "fs";

const FS = 44100;

const signal = product(
    // wrap green noise as IGen
    iterable(green(16), 0),
    // apply gain envelope
    adsr({ a: 0.005 * FS, d: 0.2 * FS, s: 0 })
);

// output as 16bit mono WAV file
writeFileSync(
    "export/hihat.wav",
    wavByteArray(
        { bits: 16, channels: 1, length: 0.25 * FS, sampleRate: FS },
        signal
    )
);

Result waveform with spectrum

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-colored-noise,
  title = "@thi.ng/colored-noise",
  author = "Karsten Schmidt",
  note = "https://thi.ng/colored-noise",
  year = 2015
}

License

© 2015 - 2021 Karsten Schmidt // Apache Software License 2.0

1.0.67

5 days ago

1.0.66

8 days ago

1.0.65

20 days ago

1.0.64

26 days ago

1.0.63

30 days ago

1.0.62

1 month ago

1.0.61

1 month ago

1.0.60

1 month ago

1.0.59

1 month ago

1.0.58

2 months ago

1.0.55

2 months ago

1.0.57

2 months ago

1.0.56

2 months ago

1.0.54

2 months ago

1.0.53

2 months ago

1.0.52

2 months ago

1.0.49

3 months ago

1.0.51

3 months ago

1.0.50

3 months ago

1.0.47

3 months ago

1.0.46

3 months ago

1.0.44

4 months ago

1.0.45

4 months ago

1.0.43

4 months ago

1.0.42

4 months ago

1.0.41

4 months ago

1.0.40

4 months ago

1.0.39

4 months ago

1.0.38

5 months ago

1.0.19

9 months ago

1.0.18

9 months ago

1.0.22

8 months ago

1.0.21

8 months ago

1.0.26

8 months ago

1.0.25

8 months ago

1.0.24

8 months ago

1.0.23

8 months ago

1.0.29

6 months ago

1.0.28

6 months ago

1.0.27

7 months ago

1.0.33

6 months ago

1.0.32

6 months ago

1.0.31

6 months ago

1.0.30

6 months ago

1.0.37

5 months ago

1.0.36

5 months ago

1.0.34

5 months ago

1.0.17

10 months ago

1.0.16

11 months ago

1.0.15

12 months ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.3.20

1 year ago

0.3.23

1 year ago

0.3.22

1 year ago

0.3.21

1 year ago

0.3.19

2 years ago

0.3.18

2 years ago

0.3.17

2 years ago

0.3.16

2 years ago

0.3.15

2 years ago

0.3.14

2 years ago

0.3.13

2 years ago

0.3.12

2 years ago

0.3.11

2 years ago

0.3.10

2 years ago

0.3.6

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.9

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.3

2 years ago

0.2.9

2 years ago

0.2.7

2 years ago

0.2.8

2 years ago

0.2.6

2 years ago

0.2.4

2 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.3

3 years ago

0.1.47

3 years ago

0.1.45

3 years ago

0.1.46

3 years ago

0.1.44

3 years ago

0.1.43

3 years ago

0.1.41

3 years ago

0.1.42

3 years ago

0.1.40

3 years ago

0.1.39

3 years ago

0.1.37

3 years ago

0.1.38

3 years ago

0.1.36

3 years ago

0.1.35

3 years ago

0.1.34

3 years ago

0.1.33

3 years ago

0.1.32

3 years ago

0.1.31

3 years ago

0.1.30

3 years ago

0.1.29

3 years ago

0.1.28

3 years ago

0.1.27

3 years ago

0.1.26

3 years ago

0.1.25

3 years ago

0.1.23

3 years ago

0.1.24

3 years ago

0.1.22

3 years ago

0.1.21

3 years ago

0.1.20

3 years ago

0.1.17

3 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.1.15

3 years ago

0.1.14

3 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago