.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_presets.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_presets.py: ======= Presets ======= This notebook demonstrates how to use the `presets` package to change the default parameters for librosa. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: Python # Code source: Brian McFee # License: ISC .. GENERATED FROM PYTHON SOURCE LINES 15-16 We'll need numpy and matplotlib for this example .. GENERATED FROM PYTHON SOURCE LINES 16-25 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt # Import the Preset class from presets import Preset # To use presets, we'll make a dummy import of librosa import librosa as _librosa .. GENERATED FROM PYTHON SOURCE LINES 26-36 By default, librosa uses the following parameters across all functions: - `sr=22050` (sampling rate) - `hop_length=512` (number of samples between frames) - `n_fft=2048` (number of samples per frame in STFT-like analyses) You may want to change these values to suit your application, but doing so consistently in every function call can be somewhat cumbersome. Presets makes it easy to do this all at once by wrapping the module and all function calls, and overriding default arguments. .. GENERATED FROM PYTHON SOURCE LINES 36-52 .. code-block:: Python # First, we need to set up the preset-wrapped librosa import librosa = Preset(_librosa) # To change the default sampling rate, we can set the `sr` entry: librosa['sr'] = 44100 # and similarly for hop_length and n_fft librosa['hop_length'] = 1024 librosa['n_fft'] = 4096 # In general, when you set `librosa['X']` for any string `X`, anywhere within # librosa where the parameter `X` occurs as a keyword-argument, # its default value will be replaced by whatever value you provide. .. GENERATED FROM PYTHON SOURCE LINES 53-54 Now we can load in a file and do some analysis with the new defaults .. GENERATED FROM PYTHON SOURCE LINES 54-93 .. code-block:: Python filename = librosa.ex('fishin') y, sr = librosa.load(filename, duration=5, offset=35) # Generate a Mel spectrogram: M = librosa.feature.melspectrogram(y=y) # Of course, you can still override the new default manually, e.g.: M_highres = librosa.feature.melspectrogram(y=y, hop_length=512) # And plot the results fig, ax = plt.subplots(nrows=3, sharex=True, sharey=True) librosa.display.specshow(librosa.power_to_db(M, ref=np.max), y_axis='mel', x_axis='time', ax=ax[0]) ax[0].set(title='44100/1024/4096') ax[0].label_outer() librosa.display.specshow(librosa.power_to_db(M_highres, ref=np.max), hop_length=512, y_axis='mel', x_axis='time', ax=ax[1]) ax[1].set(title='44100/512/4096') ax[1].label_outer() # We can repeat the whole process with different defaults, just by # updating the parameter entries librosa['sr'] = 11025 y2, sr2 = librosa.load(filename, duration=5, offset=35) M2 = librosa.feature.melspectrogram(y=y2, sr=sr2) librosa.display.specshow(librosa.power_to_db(M2, ref=np.max), y_axis='mel', x_axis='time', ax=ax[2]) ax[2].set(title='11025/1024/4096') .. image-sg:: /auto_examples/images/sphx_glr_plot_presets_001.png :alt: 44100/1024/4096, 44100/512/4096, 11025/1024/4096 :srcset: /auto_examples/images/sphx_glr_plot_presets_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.519 seconds) .. _sphx_glr_download_auto_examples_plot_presets.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_presets.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_presets.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_