% give a file with path to load
fname = 'Z:\muse-lsl\2015-07-17.xdf';

% define some args
% these are set to the default values, but adjusting them does not appear
% to correct the timestamping error
clock_sync = true;
jitter_removal = true;
clock_resets = true;

% import the data
[streams, fh] = load_xdf(fname, 'HandleClockSynchronization', clock_sync, 'HandleJitterRemoval', jitter_removal, 'HandleClockResets', clock_resets);

% determine which stream is which
eeg_idx = 2;
acc_idx = 1;
if strcmp(streams{1,1}.info.type, 'EEG')
    eeg_idx = 1;
    acc_idx = 2;
end

% grab the data and timestamps
acc_data = streams{1,eeg_idx}.time_series;
eeg_ts = streams{1, eeg_idx}.time_stamps;
eeg_info = streams{1, eeg_idx}.info;
if jitter_removal
    eeg_segs = streams{1, eeg_idx}.segments
end
disp(sprintf('EEG first timestamp %8.3f', eeg_ts(1)));
disp(sprintf('EEG last timestamp %8.3f', eeg_ts(end)));

acc_data = streams{1,acc_idx}.time_series;
acc_ts = streams{1, acc_idx}.time_stamps;
acc_info = streams{1, acc_idx}.info;
if jitter_removal
    acc_segs = streams{1, acc_idx}.segments
end
disp(sprintf('Accelerometer first timestamp %8.3f', acc_ts(1)));
disp(sprintf('Accelerometer last timestamp %8.3f', acc_ts(end)));

%% plot it
figure;
subplot(2,1,1);
plot(eeg_ts, eeg_data(3:4,:));
xlabel('Time (s)');
ylabel('EEG data');
subplot(2,1,2);
plot(acc_ts, acc_data(1:3,:));
xlabel('Time (s)');
ylabel('Accelerometer data');