.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/separation/plot_hpss.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_gallery_separation_plot_hpss.py: Harmonic and percussive separation ================================== Median-filtering HPSS: sustained tones go to the harmonic part and clicks to the percussive part. ``harmonic_percussive`` returns one ``AudioSamples`` for each. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import numpy as np import audio_samples as aus from audio_samples import effects, mpl .. GENERATED FROM PYTHON SOURCE LINES 15-16 A sustained tone mixed with clicks. .. GENERATED FROM PYTHON SOURCE LINES 16-22 .. code-block:: Python sr = 22050 tone = 0.4 * np.sin(2 * np.pi * 440.0 * np.arange(2 * sr) / sr) clicks = effects.clicks(times=np.arange(0.25, 2.0, 0.25), sr=sr, length=2 * sr) audio = aus.AudioSamples.new_mono((tone + 0.5 * clicks).astype(np.float32), sr) audio .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.8375 (-1.5 dBFS)
RMS0.2884 (-10.8 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: Python harmonic, percussive = audio.harmonic_percussive() fig, axes = mpl.plot_waveform_comparison( [audio, harmonic, percussive], ["Mix", "Harmonic", "Percussive"], title="HPSS" ) .. image-sg:: /gallery/separation/images/sphx_glr_plot_hpss_001.png :alt: HPSS :srcset: /gallery/separation/images/sphx_glr_plot_hpss_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 29-30 The harmonic part sounds like the tone. .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python harmonic .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.4326 (-7.3 dBFS)
RMS0.2821 (-11.0 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 33-34 The percussive part sounds like the clicks. .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python percussive .. raw:: html
AudioSamples
dtypef32
channels1 (mono)
sample rate22050 Hz
duration2.000 s (44100 samples per channel)
peak0.4773 (-6.4 dBFS)
RMS0.0548 (-25.2 dBFS)


.. GENERATED FROM PYTHON SOURCE LINES 37-38 The two parts add back up to the mix. .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python residual = audio.to_numpy() - (harmonic + percussive).to_numpy() print(f"largest residual: {np.max(np.abs(residual)):.2e}") assert np.max(np.abs(residual)) < 1e-3 .. rst-class:: sphx-glr-script-out .. code-block:: none largest residual: 2.09e-07 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.351 seconds) .. _sphx_glr_download_gallery_separation_plot_hpss.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_hpss.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_hpss.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_hpss.zip `