.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "acoustic/acoustic_spectrogram_aug_tutorial.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_acoustic_acoustic_spectrogram_aug_tutorial.py: Acoustic augmentation.spectrogram_aug Tutorial ============================================================== .. GENERATED FROM PYTHON SOURCE LINES 7-8 !pip install pysensing .. GENERATED FROM PYTHON SOURCE LINES 10-12 In this tutorial, we will be implementing a simple acoustic.augmentation.spectrogram_aug .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python import torch import librosa import numpy as np import torchaudio import matplotlib.pyplot as plt import sys import pysensing.acoustic.augmentation.spectrogram_aug as spec_aug import pysensing.acoustic.preprocessing.transform as transform .. GENERATED FROM PYTHON SOURCE LINES 22-25 Load the audio ----------------------------------- First, the example audio is loaded .. GENERATED FROM PYTHON SOURCE LINES 25-47 .. code-block:: Python # Define the plot function def plot(specs, titles): maxlen = max(spec.shape[-1] for spec in specs) def plot_spec(ax, spec, title): ax.set_title(title) ax.imshow(spec, origin="lower", aspect="auto") ax.set_xlim(0, maxlen) num_specs = len(specs) fig, axes = plt.subplots(num_specs, 1,figsize=(12,8)) if num_specs == 1: axes = [axes] for ax, spec, title in zip(axes, specs, titles): plot_spec(ax, spec[0].float(), title) fig.tight_layout() plt.show() # Load the data waveform, sample_rate = torchaudio.load('example_data/example_audio.wav') spectrogram = transform.spectrogram()(waveform) .. GENERATED FROM PYTHON SOURCE LINES 48-50 1. Timestretch ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 50-60 .. code-block:: Python # Define timestretch with different fixed_rate timestretch_compress = spec_aug.timestretch(fixed_rate=1.1) timestretch_extend = spec_aug.timestretch(fixed_rate=0.9) # Do timestretch to the input spectrogram spectrogram_com = timestretch_compress(spectrogram) spectrogram_ext = timestretch_extend(spectrogram) # Plotting plot([spectrogram,spectrogram_com,spectrogram_ext],['Original','Fixed_rate=1.1','Fixed_rate=1.9']) .. image-sg:: /acoustic/images/sphx_glr_acoustic_spectrogram_aug_tutorial_001.png :alt: Original, Fixed_rate=1.1, Fixed_rate=1.9 :srcset: /acoustic/images/sphx_glr_acoustic_spectrogram_aug_tutorial_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/kemove/yyz/av-gihub/tutorials/acoustic_source/acoustic_spectrogram_aug_tutorial.py:40: UserWarning: Casting complex values to real discards the imaginary part (Triggered internally at ../aten/src/ATen/native/Copy.cpp:299.) plot_spec(ax, spec[0].float(), title) .. GENERATED FROM PYTHON SOURCE LINES 61-63 2. Timemasking ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 63-71 .. code-block:: Python timemasking_trans = spec_aug.timemasking(200) timemasking_random_trans = spec_aug.timemasking(200,p=0.5) timemask_spec = timemasking_trans(spectrogram) timemask_r_spec = timemasking_random_trans(spectrogram) plot([spectrogram,timemask_spec,timemask_r_spec],['Original','Timemasking','Timemasking_random']) .. image-sg:: /acoustic/images/sphx_glr_acoustic_spectrogram_aug_tutorial_002.png :alt: Original, Timemasking, Timemasking_random :srcset: /acoustic/images/sphx_glr_acoustic_spectrogram_aug_tutorial_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-74 3. Frequecymasking ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 74-79 .. code-block:: Python frequencymasking_trans = spec_aug.frequencymasking(400) frequencymask_spec = frequencymasking_trans(spectrogram) plot([spectrogram,frequencymask_spec],['Original','Frequencymasking']) .. image-sg:: /acoustic/images/sphx_glr_acoustic_spectrogram_aug_tutorial_003.png :alt: Original, Frequencymasking :srcset: /acoustic/images/sphx_glr_acoustic_spectrogram_aug_tutorial_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-81 And that's it. We're done with our acoustic augmentation.signal_aug tutorials. Thanks for reading. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.429 seconds) .. _sphx_glr_download_acoustic_acoustic_spectrogram_aug_tutorial.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: acoustic_spectrogram_aug_tutorial.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: acoustic_spectrogram_aug_tutorial.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_