.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/tutorials/20_colored_noise.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_tutorials_20_colored_noise.py: ============= Colored noise ============= A noise signal is produced by a stochastic process. The color of noise, also called the noise spectrum, refers to the power spectrum of a noise signal. The practice of naming kinds of noise after colors started with white noise, a signal whose spectrum has equal power within any equal interval of frequencies. That name was given by analogy with white light, which was (incorrectly) assumed to have such a flat power spectrum over the visible range. Other color names, such as ``pink``, ``red``, and ``blue`` were then given to noise with other spectral profiles, often (but not always) in reference to the color of light with similar spectra. Some of those names have standard definitions in certain disciplines, while others are very informal and poorly defined. Source: `Wikipedia `_ .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python import numpy as np from matplotlib import pyplot as plt from stimuli.audio import Noise .. GENERATED FROM PYTHON SOURCE LINES 30-34 In this tutorial, we will create and plot the power spectrum of different noise colors. ``stimuli`` implements several noise color in :class:`~stimuli.audio.Noise`. Refer to the documentation of the ``color`` argument for available colors. .. GENERATED FROM PYTHON SOURCE LINES 34-41 .. code-block:: Python colors = ("white", "pink", "blue", "violet", "brown") sounds = dict() for color in colors: # identical volume on all audio channels sounds[color] = Noise(color=color, volume=10, duration=1) .. GENERATED FROM PYTHON SOURCE LINES 42-44 We can listen to each individual noise by playing each sound with :meth:`~stimuli.audio.Noise.play`. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: Python for sound in sounds.values(): sound.play(blocking=True) .. GENERATED FROM PYTHON SOURCE LINES 49-52 The underlying signal is stored in the :py:attr:`stimuli.audio.Noise.signal` property, a numpy :class:`~numpy.ndarray` of shape ``(n_samples, n_channels)``. In this case, the sound was set to mono so the signal has shape ``(n_samples, 1)``. .. GENERATED FROM PYTHON SOURCE LINES 52-64 .. code-block:: Python plt.figure(layout="constrained") for color in colors: signal = sounds[color].signal.squeeze() # compute the one-dimensional discrete fourier transform frequencies = np.fft.rfftfreq(signal.size) dft = np.abs(np.fft.rfft(signal)) # plot with log scaling on both X and Y axis plt.loglog(frequencies, dft) plt.legend(colors) plt.ylim([1e-3, None]) plt.show() .. image-sg:: /generated/tutorials/images/sphx_glr_20_colored_noise_001.png :alt: 20 colored noise :srcset: /generated/tutorials/images/sphx_glr_20_colored_noise_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 21.943 seconds) **Estimated memory usage:** 158 MB .. _sphx_glr_download_generated_tutorials_20_colored_noise.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 20_colored_noise.ipynb <20_colored_noise.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 20_colored_noise.py <20_colored_noise.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 20_colored_noise.zip <20_colored_noise.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_