Skip to main content
Skip table of contents

Acquiring data on the analog to digital converter II

This demo generates data on the DAC0 and 1, which is then read continuously on ADC 0/1 using an internal loop-back. As in Acquiring data on the analog to digital converter I, we must set up the ADC schedule with the right terms. This time, since we are continuously buffering the values, the 4th argument is set to zero.

Datapixx('SetAdcSchedule', 0, adcRate, 0, [0 1], adcBuffBaseAddr, nAdcBuffFrames);

The first zero is the schedule start delay, the adcRate was defined before as 10000 Hz, the second zero here means this schedule will run until we stop it by hand: Datapixx('StopAdcSchedule');, [0 1] means we are looking at ADC 0/1, adcBuffBaseAddr is where the data will be in the ram and nAdcBuffFrames is how much memory is allocated to buffer data. In this case, nAdcBuffFrames is defined to be 100*adcRate, therefore it will be able to contain 100 seconds of data.

We can then read the buffer and its time tags at regular intervals using

Datapixx('ReadAdcBuffer', nReadFrames, -1);

nReadFrames is the number of data available, and can be obtained using

status = Datapixx('GetAdcStatus'); nReadFrames = status.newBufferFrames;

The -1 signifies we use the same buffer defined in the previous SetAdcSchedule call (a buffer starting at adcBuffBaseAddr).

function DatapixxAdcStreamDemo()
% DatapixxAdcStreamDemo()
% Demonstrates how to continuously acquire streaming ADC data from the DATAPixx.
% For demonstration purposes we use the DAC0/1 outputs to generate waveforms,
% which we then acquire on ADC0/1 using internal loopback between DACs and ADCs.
% We'll also plot the last 100 ms of acquired data.
% Also see: DatapixxAdcBasicDemo, DatapixxAdcAcquireDemo
% 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 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.
dacRate = 1e5;
dacBuffBaseAddr = 0;
Datapixx('SetDacSchedule', 0, dacRate, 0, [0 1], dacBuffBaseAddr, nDacSamples);

% Start acquiring ADC data.
% This streaming demo stores collected ADC data in a 1 second circular buffer within the DATAPixx.
% This circular buffer is then uploaded to a large local matrix at regular intervals.
adcRate = 10000;                            % Acquire ADC data at 10 kSPS
nAdcLocalBuffFrames = adcRate*100;          % Preallocate a local buffer for 100 seconds of data
adcDataset = zeros(2, nAdcLocalBuffFrames); % We'll acquire 2 ADC channels into 2 matrix rows
nAdcBuffFrames = adcRate;                   % Streaming will use 1 second circular buffer in DATAPixx
adcBuffBaseAddr = 4e6;                      % DATAPixx internal buffer address
minStreamFrames = floor(adcRate / 100);     % Limit streaming reads to 100 times per second
Datapixx('SetAdcSchedule', 0, adcRate, 0, [0 1], adcBuffBaseAddr, nAdcBuffFrames);
Datapixx('EnableDacAdcLoopback');           % Replace this with DisableDacAdcLoopback to collect real data
Datapixx('DisableAdcFreeRunning');          % For microsecond-precise sample windows
Datapixx('RegWrRd');                        % This will cause the acquisition to start

% Continuously acquire ADC data until we've filled our local buffer,
% or until a key is pressed.
fprintf('\nADC acquisition started, press any key to stop.\n');
if (exist('OCTAVE_VERSION'))
nAcquiredFrames = 0;
while (nAcquiredFrames < nAdcLocalBuffFrames)
    if (KbCheck)    % A keypress will immediately abort the acquisition

    % How much data is available to read?
    Datapixx('RegWrRd');                    % Update registers for GetAdcStatus
    status = Datapixx('GetAdcStatus');
    nReadFrames = status.newBufferFrames;   % How many frames can we read?

    % It's not really necessary to limit the dataset to its preallocated size,
    % but we'll do it anyways, just for elegance.
    if (nReadFrames > nAdcLocalBuffFrames - nAcquiredFrames)
        nReadFrames = nAdcLocalBuffFrames - nAcquiredFrames;
    % Do not waste CPU time doing millions of tiny buffer reads    
    elseif (nReadFrames < minStreamFrames)
    % Upload the acquired ADC data
    adcDataset(:, nAcquiredFrames+1: nAcquiredFrames+nReadFrames) = Datapixx('ReadAdcBuffer', nReadFrames, -1);
    nAcquiredFrames = nAcquiredFrames + nReadFrames;

% Stop the DAC and ADC schedules, and show ADC status
fprintf('\nStatus information for ADC scheduler:\n');

% OK, the dog caught the car.  Now what do we do with it?
% We'll plot the last 100 milliseconds of data.
fprintf('\nAcquired %d data\nPlotting last 100ms of data\n', nAcquiredFrames);
startPlotFrame = nAcquiredFrames - floor(adcRate/10);
if (startPlotFrame < 1)
    startPlotFrame = 1;
plot(startPlotFrame:nAcquiredFrames, adcDataset(:,startPlotFrame:nAcquiredFrames)');

% Job done
fprintf('\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.