.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/queue-introduction.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_queue-introduction.py: ============================ Introduction to using queues ============================ This demonstrates how to use queues to order stimuli .. GENERATED FROM PYTHON SOURCE LINES 8-21 .. code-block:: Python import pylab as plt import numpy as np from psiaudio import stim from psiaudio.calibration import FlatCalibration from psiaudio.queue import FIFOSignalQueue from psiaudio.queue import (BlockedRandomSignalQueue, FIFOSignalQueue, InterleavedFIFOSignalQueue) fs = 20000 .. GENERATED FROM PYTHON SOURCE LINES 22-24 First, let's create factories for our stimuli (chirp, bandlimited noise bursts, and tones). .. GENERATED FROM PYTHON SOURCE LINES 24-40 .. code-block:: Python calibration = FlatCalibration.as_attenuation() chirp = stim.ChirpFactory(fs, start_frequency=5, end_frequency=50, duration=1, level=0, calibration=calibration) noise = stim.BandlimitedNoiseFactory(fs, seed=0, level=0, fl=100, fh=1000, filter_rolloff=2, passband_attenuation=1, stopband_attenuation=80, calibration=calibration) noise_burst = stim.Cos2EnvelopeFactory(fs, duration=1, rise_time=0.25, input_factory=noise) tone = stim.ToneFactory(fs, frequency=3, level=0, calibration=calibration) tone_burst = stim.Cos2EnvelopeFactory(fs, duration=1, rise_time=0.25, input_factory=tone) .. GENERATED FROM PYTHON SOURCE LINES 41-45 Now, create a first-in-first-out queue with five trials of each stimulus. This meanst that five chirps will be presented, followed by five noise bursts, followed by five tone bursts. The `delays` specifies the intertrial interval. .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: Python queue = FIFOSignalQueue(fs) uuid = queue.append(chirp, trials=5, delays=1) uuid = queue.append(noise_burst, trials=5, delays=1) uuid = queue.append(tone_burst, trials=5, delays=1) .. GENERATED FROM PYTHON SOURCE LINES 51-54 Whenever you append a new stimulus to the queue, a universally unique identifier (UUID) is returned. You can use that as an internal reference to track what stimuli are generated .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python uuid .. rst-class:: sphx-glr-script-out .. code-block:: none 'd23cb901-4f7d-49f1-9687-40f35c2352ff' .. GENERATED FROM PYTHON SOURCE LINES 57-60 Since each stimulus is 1 second long with a 1 second intertrial interval and there are five trials of three stimuli, the entire train will take 30 seconds. Let's "pop" that off of the queue buffer. .. GENERATED FROM PYTHON SOURCE LINES 60-66 .. code-block:: Python y = queue.pop_buffer(samples=fs*30) t = np.arange(len(y)) / fs plt.plot(t, y) plt.xlabel('Time (s)') plt.show() .. image-sg:: /gallery/images/sphx_glr_queue-introduction_001.png :alt: queue introduction :srcset: /gallery/images/sphx_glr_queue-introduction_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 67-68 Want to interleave the stimuli? Use `InterleavedFIFOSignalQueue`. .. GENERATED FROM PYTHON SOURCE LINES 68-79 .. code-block:: Python queue = InterleavedFIFOSignalQueue(fs=fs) queue.append(chirp, trials=5, delays=1) queue.append(noise_burst, trials=5, delays=1) queue.append(tone_burst, trials=5, delays=1) y = queue.pop_buffer(samples=fs*30) t = np.arange(len(y)) / fs plt.plot(t, y) plt.xlabel('Time (s)') plt.show() .. image-sg:: /gallery/images/sphx_glr_queue-introduction_002.png :alt: queue introduction :srcset: /gallery/images/sphx_glr_queue-introduction_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 80-83 There are many interesting permutations on the queueing mechanism. We can present the set of stimuli in random order, but ensure that in each "block" of three trials all three stimuli are presented (but in random order). .. GENERATED FROM PYTHON SOURCE LINES 83-96 .. code-block:: Python # Set the random seed to 1 since this creates an ordering that is clearly # semi-randomized. queue = BlockedRandomSignalQueue(fs=fs, seed=1) queue.append(chirp, trials=5, delays=1) queue.append(noise_burst, trials=5, delays=1) queue.append(tone_burst, trials=5, delays=1) y = queue.pop_buffer(samples=fs*30) t = np.arange(len(y)) / fs plt.plot(t, y) plt.xlabel('Time (s)') plt.show() .. image-sg:: /gallery/images/sphx_glr_queue-introduction_003.png :alt: queue introduction :srcset: /gallery/images/sphx_glr_queue-introduction_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.395 seconds) .. _sphx_glr_download_gallery_queue-introduction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: queue-introduction.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: queue-introduction.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: queue-introduction.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_