Recently somebody asked me to provide some pink noise! I knew about white noise, but I did not know that noise can come in different colors.
I am providing a White Noise and Pink Noise Generator. So this is the perfect opportunity to play around with noise.
I am using my my AudioTools library with Jupyterlab, so first we need to define the include Path, then we can add AudioTools.h and AudioLibs/Jupyter.h which provides the functionality that we will use.
#pragma cling add_include_path("/home/pschatzmann/Arduino/libraries/arduino-audio-tools/src")
#include "AudioTools.h"
#include "AudioLibs/Jupyter.h"
To start we define the audio format that will be used throughout this document:
AudioBaseInfo cfg;
cfg.channels = 1;
cfg.sample_rate = 44100;
The generation of white noise is straight forward:
WhiteNoiseGenerator<int16_t> white(10000); // limit amplitude=volume
GeneratedSoundStream<int16_t> sound(white);// Stream generated from sine wave
sound.begin(cfg);
JupyterAudio audio("white.wav", sound, 600, 1024);
audio
Now, how about some pink noise ?
PinkNoiseGenerator<int16_t> pink(10000); // limit amplitude=volume
GeneratedSoundStream<int16_t> sound(pink);// Stream generated from sine wave
sound.begin(cfg);
JupyterAudio audio("pink.wav", sound, 600, 1024);
audio
I do not have any other generators that would provide other colors, but we can start with a white noise and try to use a FIR filter to approximate the desired spectrum. I am using https://fiiir.com/ to generate the filter coefficients.
We generate a lowpass FIR filter with a cut off frequency of 12000 and Transition Bandwidth of 21000
float coef[] = { -0.000000000000000001, 0.001688767488474816, -0.019487763557911722, -0.022262562806491287, 0.267832523539628664, 0.544458070672598993, 0.267832523539628720, -0.022262562806491290, -0.019487763557911732, 0.001688767488474817, -0.000000000000000001 };
WhiteNoiseGenerator<int16_t> white(10000); // limit amplitude=volume
GeneratedSoundStream<int16_t> sound(white);// Stream generated from sine wave
FilteredStream<int16_t, float> brown(sound); // Defiles the filter as BaseConverter
FIR<float> fir(coef);
brown.begin(cfg);
brown.setFilter(0, &fir);
sound.begin(cfg);
JupyterAudio audio("brown.wav", brown, 600, 1024);
audio
We generate a highpass FIR filter with a cut off frequency of 12000 and Transition Bandwidth of 21000
float coef[] = { 0.000000000000000001, -0.001688767488474816, 0.019487763557911722, 0.022262562806491287, -0.267832523539628664, 0.455541929327401007, -0.267832523539628720, 0.022262562806491290, 0.019487763557911732, -0.001688767488474817, 0.000000000000000001};
WhiteNoiseGenerator<int16_t> white(20000); // limit amplitude=volume
GeneratedSoundStream<int16_t> sound(white);// Stream generated from sine wave
FilteredStream<int16_t, float> blue(sound); // Defiles the filter as BaseConverter
FIR<float> fir(coef);
blue.begin(cfg);
blue.setFilter(0, &fir);
sound.begin(cfg);
JupyterAudio audio("blue.wav", blue, 600, 1024);
audio
We generate a band reject FIR filter with a low cut off frequency of 7000 and Transition Bandwidth of 17000 and a high cut off frequency of 17000 and Transition Bandwidth of 17000
float coef[] = { 0.000000000000000001, -0.000948981178239082, -0.005303173067166042, -0.024478435941661606, 0.193646692198087278, 0.058899192865718097, 0.556369410246522644, 0.058899192865718097, 0.193646692198087389, -0.024478435941661613, -0.005303173067166042, -0.000948981178239085, 0.000000000000000001 };
WhiteNoiseGenerator<int16_t> white(10000); // limit amplitude=volume
GeneratedSoundStream<int16_t> sound(white);// Stream generated from sine wave
FilteredStream<int16_t, float> gray(sound); // Defiles the filter as BaseConverter
FIR<float> fir(coef);
gray.begin(cfg);
gray.setFilter(0, &fir);
sound.begin(cfg);
JupyterAudio audio("gray.wav", gray, 600, 1024);
audio