.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery_1d/plot_real_signal.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_1d_plot_real_signal.py: Compute the scattering transform of a speech recording ====================================================== This script loads a speech signal from the free spoken digit dataset (FSDD) of a man pronouncing the word "zero," computes its scattering transform, and displays the zeroth-, first-, and second-order scattering coefficients. .. GENERATED FROM PYTHON SOURCE LINES 10-15 Preliminaries ------------- ############################################################################## To handle audio file I/O, we import `os` and `scipy.io.wavfile`. .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: default import numpy as np import os import scipy.io.wavfile .. GENERATED FROM PYTHON SOURCE LINES 21-22 We import `matplotlib` to plot the calculated scattering coefficients. .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: default import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 26-30 Finally, we import the `Scattering1D` class from the `scattering` package and the `fetch_fsdd` function from `scattering.datasets`. The `Scattering1D` class is what lets us calculate the scattering transform, while the `fetch_fsdd` function downloads the FSDD, if needed. .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: default from kymatio.numpy import Scattering1D from kymatio.datasets import fetch_fsdd .. GENERATED FROM PYTHON SOURCE LINES 35-39 Scattering setup ---------------- First, we download the FSDD (if not already downloaded) and read in the recording `0_jackson_0.wav` of a man pronouncing the word "zero". .. GENERATED FROM PYTHON SOURCE LINES 39-45 .. code-block:: default info_dataset = fetch_fsdd(verbose=True) file_path = os.path.join(info_dataset['path_dataset'], sorted(info_dataset['files'])[0]) _, x = scipy.io.wavfile.read(file_path) .. rst-class:: sphx-glr-script-out .. code-block:: none Cloning git repository at https://github.com/Jakobovski/free-spoken-digit-dataset.git .. GENERATED FROM PYTHON SOURCE LINES 46-47 Once the recording is in memory, we normalize it. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: default x = x / np.max(np.abs(x)) .. GENERATED FROM PYTHON SOURCE LINES 51-57 We are now ready to set up the parameters for the scattering transform. First, the number of samples, `T`, is given by the size of our input `x`. The averaging scale is specified as a power of two, `2**J`. Here, we set `J = 6` to get an averaging, or maximum, scattering scale of `2**6 = 64` samples. Finally, we set the number of wavelets per octave, `Q`, to `16`. This lets us resolve frequencies at a resolution of `1/16` octaves. .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: default T = x.shape[-1] J = 6 Q = 16 .. GENERATED FROM PYTHON SOURCE LINES 63-65 Finally, we are able to create the object which computes our scattering transform, `scattering`. .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: default scattering = Scattering1D(J, T, Q) .. GENERATED FROM PYTHON SOURCE LINES 69-77 Compute and display the scattering coefficients ----------------------------------------------- Computing the scattering transform of a signal is achieved using the `__call__` method of the `Scattering1D` class. The output is an array of shape `(C, T)`. Here, `C` is the number of scattering coefficient outputs, and `T` is the number of samples along the time axis. This is typically much smaller than the number of input samples since the scattering transform performs an average in time and subsamples the result to save memory. .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: default Sx = scattering(x) .. GENERATED FROM PYTHON SOURCE LINES 81-84 To display the scattering coefficients, we must first identify which belong to each order (zeroth, first, or second). We do this by extracting the `meta` information from the scattering object and constructing masks for each order. .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. code-block:: default meta = scattering.meta() order0 = np.where(meta['order'] == 0) order1 = np.where(meta['order'] == 1) order2 = np.where(meta['order'] == 2) .. GENERATED FROM PYTHON SOURCE LINES 91-92 First, we plot the original signal `x`. .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: default plt.figure(figsize=(8, 2)) plt.plot(x) plt.title('Original signal') .. image-sg:: /gallery_1d/images/sphx_glr_plot_real_signal_001.png :alt: Original signal :srcset: /gallery_1d/images/sphx_glr_plot_real_signal_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Original signal') .. GENERATED FROM PYTHON SOURCE LINES 98-100 We now plot the zeroth-order scattering coefficient, which is simply an average of the original signal at the scale `2**J`. .. GENERATED FROM PYTHON SOURCE LINES 100-106 .. code-block:: default plt.figure(figsize=(8, 8)) plt.subplot(3, 1, 1) plt.plot(Sx[order0][0]) plt.title('Zeroth-order scattering') .. image-sg:: /gallery_1d/images/sphx_glr_plot_real_signal_002.png :alt: Zeroth-order scattering :srcset: /gallery_1d/images/sphx_glr_plot_real_signal_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Zeroth-order scattering') .. GENERATED FROM PYTHON SOURCE LINES 107-109 We then plot the first-order coefficients, which are arranged along time and log-frequency. .. GENERATED FROM PYTHON SOURCE LINES 109-114 .. code-block:: default plt.subplot(3, 1, 2) plt.imshow(Sx[order1], aspect='auto') plt.title('First-order scattering') .. image-sg:: /gallery_1d/images/sphx_glr_plot_real_signal_003.png :alt: First-order scattering :srcset: /gallery_1d/images/sphx_glr_plot_real_signal_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'First-order scattering') .. GENERATED FROM PYTHON SOURCE LINES 115-119 Finally, we plot the second-order scattering coefficients. These are also organized aling time, but has two log-frequency indices: one first-order frequency and one second-order frequency. Here, both indices are mixed along the vertical axis. .. GENERATED FROM PYTHON SOURCE LINES 119-123 .. code-block:: default plt.subplot(3, 1, 3) plt.imshow(Sx[order2], aspect='auto') plt.title('Second-order scattering') .. image-sg:: /gallery_1d/images/sphx_glr_plot_real_signal_004.png :alt: Second-order scattering :srcset: /gallery_1d/images/sphx_glr_plot_real_signal_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Second-order scattering') .. GENERATED FROM PYTHON SOURCE LINES 124-125 Display the plots! .. GENERATED FROM PYTHON SOURCE LINES 125-128 .. code-block:: default plt.tight_layout() plt.show() .. image-sg:: /gallery_1d/images/sphx_glr_plot_real_signal_005.png :alt: plot real signal :srcset: /gallery_1d/images/sphx_glr_plot_real_signal_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.777 seconds) .. _sphx_glr_download_gallery_1d_plot_real_signal.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_real_signal.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_real_signal.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_