.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/tone-complex.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_tone-complex.py: Calibrated multitone complex ============================ This demonstrates how to generate multiple tones of equal SPL when the speaker's output is not uniform. .. GENERATED FROM PYTHON SOURCE LINES 8-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from psiaudio import calibration, stim, util frequencies = [1e3, 2e3, 4e3, 8e3] levels = [ 94, 98, 92, 100] cal = calibration.InterpCalibration.from_spl(frequencies, levels) fs = 100e3 .. GENERATED FROM PYTHON SOURCE LINES 22-26 We take advantage of Numpy broadcasting to return a 2D array of frequency x time. Each row represents a tone of the desired frequency. By summing across rows, we can get a single stimulus waveform that contains all tone frequencies. .. GENERATED FROM PYTHON SOURCE LINES 26-38 .. code-block:: Python frequencies = np.array(frequencies)[:, np.newaxis] tone_complex = stim.ramped_tone( fs=fs, duration=1, rise_time=5e-3, frequency=frequencies, level=94, calibration=cal, ) tone_complex = tone_complex.sum(axis=0) .. GENERATED FROM PYTHON SOURCE LINES 39-40 Calculate an offset such that the tone is centered in the time plot. .. GENERATED FROM PYTHON SOURCE LINES 40-50 .. code-block:: Python t = np.arange(len(tone_complex)) / fs * 1e3 figure, axes = plt.subplots(1, 2, figsize=(8, 4)) axes[0].plot(t, tone_complex) axes[0].set_xlabel('Time (msec)') axes[0].set_ylabel('Amplitude (Pascals)') psd = util.patodb(util.psd_df(tone_complex, fs)) .. image-sg:: /gallery/images/sphx_glr_tone-complex_001.png :alt: tone complex :srcset: /gallery/images/sphx_glr_tone-complex_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 51-53 Plot the spectrum. Note the use of psiaudio's custom scale to show ticks on an octave scale. .. GENERATED FROM PYTHON SOURCE LINES 53-63 .. code-block:: Python axes[1].plot(psd) axes[1].set_xscale('octave') axes[1].axis(xmin=250, xmax=16e3, ymin=0, ymax=100) axes[1].set_xlabel('Frequency (kHz)') axes[1].set_ylabel('Amplitude (dB SPL)') figure.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.178 seconds) .. _sphx_glr_download_gallery_tone-complex.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: tone-complex.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: tone-complex.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: tone-complex.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_