Digital In Data - multichannelsystems/McsMatlabDataTools GitHub Wiki

Digital-In Data

If Digital-In data has been recorded, it is stored as an AnalogStream in the HDF5 file. The data in this stream can be used to identify the times and indices where bits of the Digital-In data change from a HIGH to LOW state and vice versa, but a bit of processing is needed to achieve this.

Identifying the Digital-In Stream

The AnalogStream holding the Digital-In can be identified via its DataSubType:

for i = 1:length(data.Recording{1}.AnalogStream)
    if strcmp(data.Recording{1}.AnalogStream{i}.DataSubType,'Digital')
        digStream = data.Recording{1}.AnalogStream{i};
    end
end

Extracting the Digital State Changes

All Digital-In channels are represented in a single channel in digStream, the AnalogStream with digital data. This single channel holds one 16 Bit value per sample, each Bit corresponding to one of the Digital-In channels. Because we are usually interested in the state changes on each Digital-In channel, we need to split up the 16 Bit values into the individual Bits and detect state changes on these Bits. This can be achieved with the following code:

% use this for MC_Rack data
%raw_digital_data = uint16(digStream.ChannelData + 2^15);
% use this for Multi Channel Suite data
raw_digital_data = uint16(digStream.ChannelData);
timestamps = digStream.ChannelDataTimeStamps;

% extract digital channels
digital_channel_index = 1:16;

% digital_data (channels x samples) holds the state of the digital channels
% per sample
digital_data = false(length(digital_channel_index),length(raw_digital_data));

% index_of_state_changes stores for each channel the sample index, where a
% bit on the channel changes
index_of_state_changes = cell(1,length(digital_channel_index));

% timestamps_of_state_changes stores for each channel the time stamp in µs,
% where a bit on the channel changes
timestamps_of_state_changes = cell(1,length(digital_channel_index));

% iterate over channels
for dig = digital_channel_index
    digital_data(dig,:) = bitand(raw_digital_data,uint16(2^(dig-1))); % extract the appropriate bit
    index_before_change = find(diff(digital_data(dig,:)) ~= 0); % find state changes. Use > 0 for rising edges only, < 0 for falling edges only
    index_of_state_changes{dig} = index_before_change + 1; % indices into the ChannelData matrix
    timestamps_of_state_changes{dig} = timestamps(index_before_change + 1); % timestamps in µs
end

clear index_before_change 
clear dig