auditory_stimulus

First pass at a stimulus model for abstracting the qualities and functionality of a stimulus into an abstract class. For now, we’ll assume the stimulus model only pertains to visual stimuli on a visual display over time (i.e., 3D). Hopefully this can be extended to other stimuli with an arbitrary number of dimensions (e.g., auditory stimuli).

AuditoryStimulus(stim_arr, NFFT, Fs, ...)
generate_spectrogram(signal, NFFT, Fs, noverlap) Generation of spectrogram from a 1D audio signal.

AuditoryStimulus

class popeye.auditory_stimulus.AuditoryStimulus(stim_arr, NFFT, Fs, noverlap, resample_factor, dtype, tr_length)

Bases: popeye.base.StimulusModel

__init__(stim_arr, NFFT, Fs, noverlap, resample_factor, dtype, tr_length)

A child of the StimulusModel class for auditory stimuli.

signal : ndarray
A 1D array containg the monophonic auditory stimulus.
NFFT : integer
The number of data points used in each block for the FFT. Must be even; a power 2 is most efficient. The default value is 256. This should NOT be used to get zero padding, or the scaling of the result will be incorrect. Use pad_to for this instead.
Fs : scalar
The sampling frequency (samples per time unit). It is used to calculate the Fourier frequencies, freqs, in cycles per time unit. The default value is 2.
noverlap : integer
The number of points of overlap between blocks. The default value is 128.
dtype : string
Sets the data type the stimulus array is cast into.
tr_length : float
The repetition time (TR) in seconds.

StimulusModel

class popeye.auditory_stimulus.StimulusModel(stim_arr, dtype=<class 'ctypes.c_short'>, tr_length=1.0)

Bases: object

__init__(stim_arr, dtype=<class 'ctypes.c_short'>, tr_length=1.0)

A base class for all encoding stimuli.

This class houses the basic and common features of the encoding stimulus, which along with a PopulationModel constitutes what is commonly referred to as the pRF model.

stim_arr : ndarray
An array containing the stimulus. The dimensionality of the data is arbitrary but must be consistent with the pRF model, as specified in PopulationModel and PopluationFit class instances.
dtype : string
Sets the data type the stimulus array is cast into.
dtype : string
Sets the data type the stimulus array is cast into.
tr_length : float
The repetition time (TR) in seconds.

auto_attr

popeye.auditory_stimulus.auto_attr(func)

Decorator to create OneTimeProperty attributes.

func : method
The method that will be called the first time to compute a value. Afterwards, the method’s name will be a standard attribute holding the value of this computation.
>>> class MagicProp(object):
...     @auto_attr
...     def a(self):
...         return 99
...
>>> x = MagicProp()
>>> 'a' in x.__dict__
False
>>> x.a
99
>>> 'a' in x.__dict__
True

generate_spectrogram

popeye.auditory_stimulus.generate_spectrogram(signal, NFFT, Fs, noverlap)

Generation of spectrogram from a 1D audio signal.

signal : ndarray
A 1D array containg the monophonic auditory stimulus.
NFFT : integer
The number of data points used in each block for the FFT. Must be even; a power 2 is most efficient. The default value is 256. This should NOT be used to get zero padding, or the scaling of the result will be incorrect. Use pad_to for this instead.
Fs : scalar
The sampling frequency (samples per time unit). It is used to calculate the Fourier frequencies, freqs, in cycles per time unit. The default value is 2.
noverlap : integer
The number of points of overlap between blocks. The default value is 128.

For more information, see help of mlab.specgram.

specgram

popeye.auditory_stimulus.specgram(x, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None, mode=None)

Compute a spectrogram.

Call signature:

specgram(x, NFFT=256, Fs=2,detrend=mlab.detrend_none,
        window=mlab.window_hanning, noverlap=128,
        cmap=None, xextent=None, pad_to=None, sides='default',
        scale_by_freq=None, mode='default')

Compute and plot a spectrogram of data in x. Data are split into NFFT length segments and the spectrum of each section is computed. The windowing function window is applied to each segment, and the amount of overlap of each segment is specified with noverlap.

x: 1-D array or sequence
Array or sequence containing the data

Keyword arguments:

Fs: scalar
The sampling frequency (samples per time unit). It is used to calculate the Fourier frequencies, freqs, in cycles per time unit. The default value is 2.
window: callable or ndarray
A function or a vector of length NFFT. To create window vectors see window_hanning(), window_none(), numpy.blackman(), numpy.hamming(), numpy.bartlett(), scipy.signal(), scipy.signal.get_window(), etc. The default is window_hanning(). If a function is passed as the argument, it must take a data segment as an argument and return the windowed version of the segment.
sides: [ ‘default’ | ‘onesided’ | ‘twosided’ ]
Specifies which sides of the spectrum to return. Default gives the default behavior, which returns one-sided for real data and both for complex data. ‘onesided’ forces the return of a one-sided spectrum, while ‘twosided’ forces two-sided.
pad_to: integer
The number of points to which the data segment is padded when performing the FFT. This can be different from NFFT, which specifies the number of data points used. While not increasing the actual resolution of the spectrum (the minimum distance between resolvable peaks), this can give more points in the plot, allowing for more detail. This corresponds to the n parameter in the call to fft(). The default is None, which sets pad_to equal to NFFT
NFFT: integer
The number of data points used in each block for the FFT. A power 2 is most efficient. The default value is 256. This should NOT be used to get zero padding, or the scaling of the result will be incorrect. Use pad_to for this instead.
detrend: [ ‘default’ | ‘constant’ | ‘mean’ | ‘linear’ | ‘none’] or
callable

The function applied to each segment before fft-ing, designed to remove the mean or linear trend. Unlike in MATLAB, where the detrend parameter is a vector, in matplotlib is it a function. The pylab module defines detrend_none(), detrend_mean(), and detrend_linear(), but you can use a custom function as well. You can also use a string to choose one of the functions. ‘default’, ‘constant’, and ‘mean’ call detrend_mean(). ‘linear’ calls detrend_linear(). ‘none’ calls detrend_none().

scale_by_freq: boolean
Specifies whether the resulting density values should be scaled by the scaling frequency, which gives density in units of Hz^-1. This allows for integration over the returned frequency values. The default is True for MATLAB compatibility.
mode: [ ‘default’ | ‘psd’ | ‘complex’ | ‘magnitude’
‘angle’ | ‘phase’ ]

What sort of spectrum to use. Default is ‘psd’. which takes the power spectral density. ‘complex’ returns the complex-valued frequency spectrum. ‘magnitude’ returns the magnitude spectrum. ‘angle’ returns the phase spectrum without unwrapping. ‘phase’ returns the phase spectrum with unwrapping.

noverlap: integer
The number of points of overlap between blocks. The default value is 128.

Returns the tuple (spectrum, freqs, t):

spectrum: 2-D array
columns are the periodograms of successive segments
freqs: 1-D array
The frequencies corresponding to the rows in spectrum
t: 1-D array
The times corresponding to midpoints of segments (i.e the columns in spectrum).

Note

detrend and scale_by_freq only apply when mode is set to ‘psd’

See also

psd()
psd() differs in the default overlap; in returning the mean of the segment periodograms; and in not returning times.
complex_spectrum()
A single spectrum, similar to having a single segment when mode is ‘complex’.
magnitude_spectrum()
A single spectrum, similar to having a single segment when mode is ‘magnitude’.
angle_spectrum()
A single spectrum, similar to having a single segment when mode is ‘angle’.
phase_spectrum()
A single spectrum, similar to having a single segment when mode is ‘phase’.