stimuli.audio.SoundAMπŸ”—

class stimuli.audio.SoundAM(frequency_carrier, frequency_modulation, method, volume, duration, sample_rate=None, device=None, n_channels=1, *, backend='sounddevice', clock=<class 'stimuli.time._clock.Clock'>, **kwargs)[source]πŸ”—

Amplitude modulated sound.

This sound is composed of a carrier frequency fc which is amplitude modulated at fm.

Parameters:
frequency_carrierfloat

Carrier frequency in Hz.

frequency_modulationfloat

Modulation frequency in Hz.

method'conventional' | 'dsbsc'

'conventional' is also called classical AM, the equation used is:

signal = (1 - M(t)) * cos(2*pi*fc*t)
M(t) = cos(2*pi*fm*t)

'dsbsc' is also called double side band suppressed carrier, the equation used is:

signal = M(t)*cos(2*pi*fc*t)
M(t) = sin(2*pi*fm*t)
volumefloat | list of float | tuple of float | array of float

Volume of the sound as a percentage between 0 and 100. If a float is provided, the same volume is used for all channels. If a sequence is provided, the length must match the number of channels.

durationfloat

Duration of the sound in seconds, used to generate the time array.

sample_rateint | None

Sample rate of the sound. If None, the default sample rate of the device provided by the backend is used.

deviceint | None

Device index to use for sound playback. If None, the default device provided by the backend is used.

n_channelsint

Number of channels of the sound.

backend"sounddevice"

The backend to use for sound playback.

clockstimuli.time.BaseClock

Clock object to use for timing measurements.

**kwargs

Additional keyword arguments passed to the backend initialization.

Attributes

duration

The duration of the audio stimulus.

frequency_carrier

Sound's carrier frequency in Hz.

frequency_modulation

Sound's modulation frequency in Hz.

method

The amplitude modulation method.

sample_rate

The sample rate of the audio stimulus.

signal

The audio signal.

times

The time array of the audio stimulus.

volume

The volume of the audio stimulus per channel given as a percentage.

window

Window applied to the audio signal.

Methods

play([when, blocking])

Play the audio data.

plot()

Plot the audio signal waveform.

save(fname, *[, overwrite])

Save the audio stimulus to a WAV file.

stop()

Interrupt immediately the playback of the audio data.

play(when=None, *, blocking=False)[source]πŸ”—

Play the audio data.

Parameters:
whenfloat | None

The relative time in seconds when to start playing the audio data. For instance, 0.2 will start playing in 200 ms. If None, the audio data is played as soon as possible. A duration superior to the device latency is recommended.

blockingbool

If True, the function blocks until the audio playback is finished.

plot()[source]πŸ”—

Plot the audio signal waveform.

Returns:
fmatplotlib.figure.Figure

The matplotlib figure object.

axmatplotlib.axes.Axes

The matplotlib axes object.

save(fname, *, overwrite=False)[source]πŸ”—

Save the audio stimulus to a WAV file.

The saving is handled by scipy.io.wavfile.write().

Parameters:
fnamestr | pathlib.Path

Path to the output file. The extension should be '.wav'.

overwritebool

If True, existing files are overwritten.

stop()[source]πŸ”—

Interrupt immediately the playback of the audio data.

property durationπŸ”—

The duration of the audio stimulus.

Type:

float

property frequency_carrierπŸ”—

Sound’s carrier frequency in Hz.

Type:

float

property frequency_modulationπŸ”—

Sound’s modulation frequency in Hz.

Type:

float

property methodπŸ”—

The amplitude modulation method.

Type:

str

property sample_rateπŸ”—

The sample rate of the audio stimulus.

Type:

int

property signalπŸ”—

The audio signal.

Type:

ndarray of shape (n_samples, n_channels)

property timesπŸ”—

The time array of the audio stimulus.

Type:

ndarray of shape (n_samples,)

property volumeπŸ”—

The volume of the audio stimulus per channel given as a percentage.

Type:

ndarray of shape (n_channels,)

property windowπŸ”—

Window applied to the audio signal.

Type:

ndarray of shape (n_samples,) | None

Examples using stimuli.audio.SoundAMπŸ”—

Synchronize sound and trigger

Synchronize sound and trigger