Generator Module
The generator module provides signal generators.
The following functions calculate N samples and return an array containing the samples.
For indefinitely long iteration over the samples, consider using the output of these functions
in itertools.cycle.
Noise
Different types of noise are available. The following table lists the color of noise and how the power and power density change per octave:
| Color | Power | Power density |
|---|---|---|
| White | +3 dB | 0 dB |
| Pink | 0 dB | -3 dB |
| Blue | +6 dB | +3 dB |
| Brown | -3 dB | -6 dB |
| Violet | +9 dB | +6 dB |
The colored noise is created by generating pseudo-random numbers using
np.random.randn and then multiplying these with a curve typical for the color.
Afterwards, an inverse DFT is performed using np.fft.irfft.
Finally, the noise is normalized using acoustic_toolbox.signal.normalize.
All colors
| FUNCTION | DESCRIPTION |
|---|---|
noise |
Generate noise of a specified color. |
noise_generator |
Generate |
Per color
| FUNCTION | DESCRIPTION |
|---|---|
white |
Generate white noise with constant power density and flat narrowband spectrum. |
pink |
Generate pink noise with equal power in proportionally wide bands. |
blue |
Generate blue noise with power increasing 6 dB per octave. |
brown |
Generate brown noise with power decreasing -3 dB per octave. |
violet |
Generate violet noise with power increasing +9 dB per octave. |
heaviside |
Returns the value 0 for |
See Also
For related functions, check scipy.signal.
Functions
noise
noise(
N: int,
color: str = "white",
state: RandomState | None = None,
) -> ndarray
Noise generator.
| PARAMETER | DESCRIPTION |
|---|---|
N
|
Amount of samples.
TYPE:
|
color
|
Color of noise.
TYPE:
|
state
|
State of PRNG.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of noise samples. |
Source code in acoustic_toolbox/generator.py
white
white(N: int, state: RandomState | None = None) -> ndarray
White noise.
White noise has a constant power density. Its narrowband spectrum is therefore flat. The power in white noise will increase by a factor of two for each octave band, and therefore increases with 3 dB per octave.
| PARAMETER | DESCRIPTION |
|---|---|
N
|
Amount of samples.
TYPE:
|
state
|
State of PRNG.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of white noise samples. |
Source code in acoustic_toolbox/generator.py
pink
pink(N: int, state: RandomState | None = None) -> ndarray
Pink noise.
Pink noise has equal power in bands that are proportionally wide. Power density decreases with 3 dB per octave.
Note
This method uses the filter with the following coefficients. $$ B = [0.049922035, -0.095993537, 0.050612699, -0.004408786] $$ $$ A = [1, -2.494956002, 2.017265875, -0.522189400] $$
The filter is applied using scipy.signal.lfilter:
from scipy.signal import lfilter
b = np.array([0.049922035, -0.095993537, 0.050612699, -0.004408786])
a = np.array([1, -2.494956002, 2.017265875, -0.522189400])
return lfilter(b, a, np.random.randn(N))
Another way would be using the FFT:
| PARAMETER | DESCRIPTION |
|---|---|
N
|
Amount of samples.
TYPE:
|
state
|
State of PRNG.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of pink noise samples. |
Source code in acoustic_toolbox/generator.py
blue
blue(N: int, state: RandomState | None = None) -> ndarray
Blue noise.
Power increases with 6 dB per octave. Power density increases with 3 dB per octave.
| PARAMETER | DESCRIPTION |
|---|---|
N
|
Amount of samples.
TYPE:
|
state
|
State of PRNG.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of blue noise samples. |
Source code in acoustic_toolbox/generator.py
brown
brown(N: int, state: RandomState | None = None) -> ndarray
Brown noise.
Power decreases with -3 dB per octave. Power density decreases with 6 dB per octave.
| PARAMETER | DESCRIPTION |
|---|---|
N
|
Amount of samples.
TYPE:
|
state
|
State of PRNG.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of violet noise samples. |
Source code in acoustic_toolbox/generator.py
violet
violet(N: int, state: RandomState | None = None) -> ndarray
Violet noise.
Power increases with +9 dB per octave. Power density increases with +6 dB per octave.
| PARAMETER | DESCRIPTION |
|---|---|
N
|
Amount of samples.
TYPE:
|
state
|
State of PRNG.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of violet noise samples. |
Source code in acoustic_toolbox/generator.py
noise_generator
noise_generator(
N: int = 44100,
color: str = "white",
state: RandomState | None = None,
) -> Generator[float, None, None]
Noise generator.
Generate N amount of unique samples and cycle over these samples.
| PARAMETER | DESCRIPTION |
|---|---|
N
|
Amount of unique samples to generate.
TYPE:
|
color
|
Color of noise.
TYPE:
|
state
|
State of PRNG.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
Generator of noise samples. |
Source code in acoustic_toolbox/generator.py
:::