.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_rainbowgram.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_auto_examples_plot_rainbowgram.py: ================ Rainbowgrams ================ This notebook demonstrates how to use "Rainbowgrams" to simultaneously visualize amplitude and (unwrapped) phase (differential) as demonstrated in the `NSynth paper `_ [1]_. .. [1] Engel, Jesse, Cinjon Resnick, Adam Roberts, Sander Dieleman, Mohammad Norouzi, Douglas Eck, and Karen Simonyan. "Neural audio synthesis of musical notes with wavenet autoencoders." In International Conference on Machine Learning, pp. 1068-1077. PMLR, 2017. .. GENERATED FROM PYTHON SOURCE LINES 16-19 .. code-block:: Python # Code source: Brian McFee # License: ISC .. GENERATED FROM PYTHON SOURCE LINES 20-21 Standard imports .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt import librosa .. GENERATED FROM PYTHON SOURCE LINES 27-29 We implemented a stft method to visualize the rainbowgram and demonstrated the result with a chirp signal. A chirp signal starts at a low frequency and gradually increases in frequency over time. We then separated the magnitude and phase components of the signal .. GENERATED FROM PYTHON SOURCE LINES 29-34 .. code-block:: Python sr = 22050 y = librosa.chirp(fmin=32, fmax=32 * 2**5, sr=sr, duration=10, linear=True) D = librosa.stft(y) mag, phase = librosa.magphase(D) .. GENERATED FROM PYTHON SOURCE LINES 35-37 We should be visualizing the demodulated phase differential derived by subtracting 2π*f*t from each phase estimate prior to unwrapping, where f and t are the frequency and time. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python freqs = librosa.fft_frequencies() times = librosa.times_like(D) phase_exp = 2*np.pi*np.multiply.outer(freqs,times) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Plot the spectrum. .. GENERATED FROM PYTHON SOURCE LINES 43-55 .. code-block:: Python fig, ax = plt.subplots() img = librosa.display.specshow(np.diff(np.unwrap(np.angle(phase)-phase_exp, axis=1), axis=1, prepend=0), cmap='hsv', alpha=librosa.amplitude_to_db(mag, ref=np.max)/80 + 1, ax=ax, y_axis='log', x_axis='time') ax.set_facecolor('#000') cbar = fig.colorbar(img, ticks=[-np.pi, -np.pi/2, 0, np.pi/2, np.pi]) cbar.ax.set(yticklabels=['-π', '-π/2', "0", 'π/2', 'π']); plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_rainbowgram_001.png :alt: plot rainbowgram :srcset: /auto_examples/images/sphx_glr_plot_rainbowgram_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-59 The above uses HSV colormap for phase fading to a black background. The twilight colormap can also work here, with the caveat that it uses black to code the extremes of the map (ie 0). We can sidestep this by using a neutral axis facecolor: .. GENERATED FROM PYTHON SOURCE LINES 59-69 .. code-block:: Python fig, ax = plt.subplots() img = librosa.display.specshow(np.diff(np.unwrap(np.angle(phase)-phase_exp, axis=1), axis=1, prepend=0), cmap='twilight', alpha=librosa.amplitude_to_db(mag, ref=np.max)/80 + 1, ax=ax, y_axis='log', x_axis='time') ax.set_facecolor('#888') cbar = fig.colorbar(img, ticks=[-np.pi, -np.pi/2, 0, np.pi/2, np.pi]) cbar.ax.set(yticklabels=['-π', '-π/2', 0, 'π/2', 'π']) .. image-sg:: /auto_examples/images/sphx_glr_plot_rainbowgram_002.png :alt: plot rainbowgram :srcset: /auto_examples/images/sphx_glr_plot_rainbowgram_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.655 seconds) .. _sphx_glr_download_auto_examples_plot_rainbowgram.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rainbowgram.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rainbowgram.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_