{
"cells": [
{
"cell_type": "markdown",
"id": "4947d692-812c-4ab1-958e-ac078226707d",
"metadata": {},
"source": [
"# The Colors of Noise\n",
"\n",
"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](https://en.wikipedia.org/wiki/Colors_of_noise).\n",
"\n",
"I am providing a White Noise and Pink Noise Generator. So this is the perfect opportunity to __play around with noise__.\n",
"\n",
"I am using my my [AudioTools](https://github.com/pschatzmann/arduino-audio-tools) 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."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2718c3e1-a7f2-4ea2-a649-2e045165f418",
"metadata": {
"vscode": {
"languageId": "c++"
}
},
"outputs": [],
"source": [
"#pragma cling add_include_path(\"/home/pschatzmann/Arduino/libraries/arduino-audio-tools/src\")\n",
"#include \"AudioTools.h\"\n",
"#include \"AudioLibs/Jupyter.h\""
]
},
{
"cell_type": "markdown",
"id": "55c292df-9cfb-4e74-9b4f-f625bb2f8d38",
"metadata": {},
"source": [
"## Common Constants\n",
"\n",
"To start we define the audio format that will be used throughout this document:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "42fb7892-a018-4db1-98b1-0ed52cc2d1e3",
"metadata": {
"vscode": {
"languageId": "c++"
}
},
"outputs": [],
"source": [
"AudioBaseInfo cfg;\n",
"cfg.channels = 1;\n",
"cfg.sample_rate = 44100;"
]
},
{
"cell_type": "markdown",
"id": "35d46f1a-a2da-4dc2-bf6c-8b4b925c6622",
"metadata": {},
"source": [
"## White Noise\n",
"The generation of white noise is straight forward:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "6cbd928e-61fc-467e-9cf3-ea8e95553c4a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"WhiteNoiseGenerator white(10000); // limit amplitude=volume \n",
"GeneratedSoundStream sound(white);// Stream generated from sine wave\n",
"sound.begin(cfg);\n",
"\n",
"JupyterAudio audio(\"white.wav\", sound, 600, 1024);\n",
"audio"
]
},
{
"cell_type": "markdown",
"id": "98805fb5-6e9f-4871-8327-577072ca03c2",
"metadata": {},
"source": [
"## Pink Noise\n",
"Now, how about some pink noise ?"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "f589d58e-1c76-498e-a25d-c9f23c3c55fe",
"metadata": {
"vscode": {
"languageId": "c++"
}
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PinkNoiseGenerator pink(10000); // limit amplitude=volume \n",
"GeneratedSoundStream sound(pink);// Stream generated from sine wave\n",
"sound.begin(cfg);\n",
"\n",
"JupyterAudio audio(\"pink.wav\", sound, 600, 1024);\n",
"audio"
]
},
{
"cell_type": "markdown",
"id": "fcf21448-4981-4ae7-89d9-8996c633b5b4",
"metadata": {},
"source": [
"## Other Colors\n",
"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.\n",
"\n",
"### Brown Noise\n",
"We generate a lowpass FIR filter with a cut off frequency of 12000 and Transition Bandwidth of 21000"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ceb29108-9f39-4146-a263-16b5f6ea2b62",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float coef[] = { -0.000000000000000001, 0.001688767488474816, -0.019487763557911722, -0.022262562806491287, 0.267832523539628664, 0.544458070672598993, 0.267832523539628720, -0.022262562806491290, -0.019487763557911732, 0.001688767488474817, -0.000000000000000001 };\n",
"\n",
"WhiteNoiseGenerator white(10000); // limit amplitude=volume \n",
"GeneratedSoundStream sound(white);// Stream generated from sine wave\n",
"FilteredStream brown(sound); // Defiles the filter as BaseConverter\n",
"FIR fir(coef);\n",
"brown.begin(cfg);\n",
"brown.setFilter(0, &fir);\n",
"sound.begin(cfg);\n",
"\n",
"JupyterAudio audio(\"brown.wav\", brown, 600, 1024);\n",
"audio"
]
},
{
"cell_type": "markdown",
"id": "19b2616b-e375-4e22-8fdc-d0b617295fee",
"metadata": {},
"source": [
"### Blue Noise\n",
"We generate a highpass FIR filter with a cut off frequency of 12000 and Transition Bandwidth of 21000"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "6d463093-7373-40d5-9351-738f868bd3e7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float coef[] = { 0.000000000000000001, -0.001688767488474816, 0.019487763557911722, 0.022262562806491287, -0.267832523539628664, 0.455541929327401007, -0.267832523539628720, 0.022262562806491290, 0.019487763557911732, -0.001688767488474817, 0.000000000000000001};\n",
"\n",
"WhiteNoiseGenerator white(20000); // limit amplitude=volume \n",
"GeneratedSoundStream sound(white);// Stream generated from sine wave\n",
"FilteredStream blue(sound); // Defiles the filter as BaseConverter\n",
"FIR fir(coef);\n",
"blue.begin(cfg);\n",
"blue.setFilter(0, &fir);\n",
"sound.begin(cfg);\n",
"\n",
"JupyterAudio audio(\"blue.wav\", blue, 600, 1024);\n",
"audio"
]
},
{
"cell_type": "markdown",
"id": "c8ac6375-298e-435d-9bcf-728f7bed19c4",
"metadata": {},
"source": [
"### Gray Noise\n",
"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"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "7be6968a-99dd-43f8-8d37-b02f155eef2c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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 };\n",
"\n",
"WhiteNoiseGenerator white(10000); // limit amplitude=volume \n",
"GeneratedSoundStream sound(white);// Stream generated from sine wave\n",
"FilteredStream gray(sound); // Defiles the filter as BaseConverter\n",
"FIR fir(coef);\n",
"gray.begin(cfg);\n",
"gray.setFilter(0, &fir);\n",
"sound.begin(cfg);\n",
"\n",
"JupyterAudio audio(\"gray.wav\", gray, 600, 1024);\n",
"audio"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9fe9d1fc-dd57-44f3-8dcd-90c715e3bb91",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}