.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery_1d/plot_synthetic.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_synthetic.py: Compute the scattering transform of a synthetic signal ====================================================== In this example we generate a harmonic signal of a few different frequencies and analyze it with the 1D scattering transform. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Import the necessary packages ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: default from kymatio.numpy import Scattering1D import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 18-28 Write a function that can generate a harmonic signal ---------------------------------------------------- Let's write a function that can generate some simple blip-type sounds with decaying harmonics. It will take four arguments: T, the length of the output vector; num_intervals, the number of different blips; gamma, the exponential decay factor of the harmonic; random_state, a random seed to generate random pitches and phase shifts. The function proceeds by splitting the time length T into intervals, chooses base frequencies and phases, generates sinusoidal sounds and harmonics, and then adds a windowed version to the output signal. .. GENERATED FROM PYTHON SOURCE LINES 28-54 .. code-block:: default def generate_harmonic_signal(T, num_intervals=4, gamma=0.9, random_state=42): """ Generates a harmonic signal, which is made of piecewise constant notes (of random fundamental frequency), with half overlap """ rng = np.random.RandomState(random_state) num_notes = 2 * (num_intervals - 1) + 1 support = T // num_intervals half_support = support // 2 base_freq = 0.1 * rng.rand(num_notes) + 0.05 phase = 2 * np.pi * rng.rand(num_notes) window = np.hanning(support) x = np.zeros(T, dtype='float32') t = np.arange(0, support) u = 2 * np.pi * t for i in range(num_notes): ind_start = i * half_support note = np.zeros(support) for k in range(1): note += (np.power(gamma, k) * np.cos(u * (k + 1) * base_freq[i] + phase[i])) x[ind_start:ind_start + support] += note * window return x .. GENERATED FROM PYTHON SOURCE LINES 55-57 Let's take a look at what such a signal could look like ------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: default T = 2 ** 13 x = generate_harmonic_signal(T) plt.figure(figsize=(8, 2)) plt.plot(x) plt.title("Original signal") .. image-sg:: /gallery_1d/images/sphx_glr_plot_synthetic_001.png :alt: Original signal :srcset: /gallery_1d/images/sphx_glr_plot_synthetic_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 64-67 Spectrogram ----------- Let's take a look at the signal spectrogram .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: default plt.figure(figsize=(8, 4)) plt.specgram(x, Fs=1024) plt.title("Time-Frequency spectrogram of signal") .. image-sg:: /gallery_1d/images/sphx_glr_plot_synthetic_002.png :alt: Time-Frequency spectrogram of signal :srcset: /gallery_1d/images/sphx_glr_plot_synthetic_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Time-Frequency spectrogram of signal') .. GENERATED FROM PYTHON SOURCE LINES 72-74 Doing the scattering transform ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 74-98 .. code-block:: default J = 6 Q = 16 scattering = Scattering1D(J, T, Q) meta = scattering.meta() order0 = np.where(meta['order'] == 0) order1 = np.where(meta['order'] == 1) order2 = np.where(meta['order'] == 2) Sx = scattering(x) plt.figure(figsize=(8, 8)) plt.subplot(3, 1, 1) plt.plot(Sx[order0][0]) plt.title('Zeroth-order scattering') plt.subplot(3, 1, 2) plt.imshow(Sx[order1], aspect='auto') plt.title('First-order scattering') plt.subplot(3, 1, 3) plt.imshow(Sx[order2], aspect='auto') plt.title('Second-order scattering') plt.tight_layout() plt.show() .. image-sg:: /gallery_1d/images/sphx_glr_plot_synthetic_003.png :alt: Zeroth-order scattering, First-order scattering, Second-order scattering :srcset: /gallery_1d/images/sphx_glr_plot_synthetic_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.548 seconds) .. _sphx_glr_download_gallery_1d_plot_synthetic.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_synthetic.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_synthetic.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_