Skip to main content
Skip table of contents

Acquiring data on the analog to digital converter I

This example explains how to start the acquisition of data on the analogue port. To simulate a real experiment, the acquisition starts on a video sync, such as, for example, when a stimulus would be shown and your subject would start reacting. In this demo, the stimulus is simply a full white screen for a duration of exactly one frame (8.33 ms). The demo records on the analogue port, at a rate we chose. To determine this rate, we create a schedule. When defining the schedule, we must decide which port we want to monitor, and at what speed (rate).

Datapixx('SetAdcSchedule', 0, 1e4, nAdcSamples, [0 1]);

The 0 is the delay parameter. Since we will start this ourselves, we set it to 0. We set the rate to 10000 and since we specify no units, this is 10000 Hz, or 10 kHz. nAdcSamples is the number of samples before the schedule start. Set it to zero if you want to stop it manually. [0 1] are the channels we are going to monitor.

In this example, we use a loopback to create and read Analogue signals. In a real experiment, you would instead have a device sending the analogue signal, and would therefore not use the loopback.

function DatapixxAdcAcquireDemo()
% DatapixxAdcAcquireDemo()
% Shows how to acquire ADC data synchronized with a video stimulus.
% For demonstration purposes we use the DAC0/1 outputs to simulate an evoked
% potential, which we then acquire on ADC0/1 using internal loopback between
% DACs and ADCs.
% We'll then plot the simulated evoked potential.
% Also see: DatapixxAdcBasicDemo, DatapixxAdcStreamDemo
% History:
% Oct 1, 2009  paa     Written
% Oct 29, 2014 dml     Revised 

AssertOpenGL;   % We use PTB-3

Datapixx('RegWrRd');    % Synchronize Datapixx registers to local register cache

% Fill up a DAC buffer with 2 channels of 1000 samples of simulated visual evoked potential data.
% I guess our 2 simulated neurons will just output sin/cos functions.
% We'll generate a single period of the waveforms, and play them repeatedly.
nDacSamples = 1000;
dacData = [sin([0:nDacSamples-1]/nDacSamples*2*pi) ; cos([0:nDacSamples-1]/nDacSamples*2*pi)];
Datapixx('WriteDacBuffer', dacData);

% Play the downloaded DAC waveform buffers continuously at 100 kSPS,
% resulting in 100 Hz sin/cos waves being output onto DAC0/1.
Datapixx('SetDacSchedule', 0, 1e5, 0, [0 1], 0, nDacSamples);

% Configure ADC to acquire 2 channels (ADC0 and ADC1) at 10 kSPS.
% How much data should we acquire? We'll say 1000 samples, which is 100 milliseconds.
% This will give 10 cycles of our 100 Hz sin/cos simulated evoked potentials.
nAdcSamples = 1000;
Datapixx('SetAdcSchedule', 0, 1e4, nAdcSamples, [0 1]);
Datapixx('EnableDacAdcLoopback');   % Replace this with DisableDacAdcLoopback to collect real data
Datapixx('DisableAdcFreeRunning');  % For microsecond-precise sample windows

% Our simulated neuron will be responding to a white flash,
% and we shall start acquisition precisely on the vertical sync preceeding the flash.
    oldVerbosity = Screen('Preference', 'Verbosity', 1);   % Don't log the GL stuff
    screenNumber = max(Screen('Screens'));
    window = Screen('OpenWindow', screenNumber, 0, []);
    Screen('Preference', 'Verbosity', oldVerbosity);

    % Clear display to black, and wait about a second
    Screen('FillRect',window, [0 0 0]);     % Clear the display to black
    for frame = 0:60
        Screen('Flip', window);

    % Start the analog acquisition on next vertical sync pulse,
    % and show a white flash for one frame.
    Datapixx('RegWrVideoSync');                 % ADC will start at onset of next video VSYNC pulse
    Screen('FillRect',window, [255 255 255]);   % Fill the display with white
    Screen('Flip', window);
    Screen('FillRect',window, [0 0 0]);         % Return the display to black
    Screen('Flip', window);

    % Wait for the ADC to finish acquiring its scheduled dataset
    while 1
        Datapixx('RegWrRd');   % Update registers for GetAdcStatus
        status = Datapixx('GetAdcStatus');
        if (~status.scheduleRunning)

    % Show final status of ADC scheduler
    fprintf('\nStatus information for ADC scheduler:\n');

    % Upload our acquired data,
    % and plot our evoked potential (10 periods of sin/cos) as a function of time.
    [adcData, adcTimetags] = Datapixx('ReadAdcBuffer', nAdcSamples);
    plot(adcTimetags, adcData');

    % Job done
    Datapixx('StopAllSchedules');   % Stop the DAC waveform
    Datapixx('StopAllSchedules');   % Stop the DAC waveform

 fprintf('\n\nDemo completed\n\n');
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.