% Finding the timing of the CTF MEG system data stream
%
% Feb. 2012

host = '172.16.50.107';        % IP address of host computer (Acq)
port = 1972;                       

Niteration = 100;
% clear t s
tic
s = zeros(Niteration,1);
t = zeros(Niteration,1);

for i=1:Niteration
%     disp(i);

    % read the header to see whether new samples are available
    hdr = buffer('get_hdr',[],host,port);

    t(i) = toc;
    s(i) = hdr.nsamples

    if i>1 & s(i)>s(i-1)
        % read a block of data, we won't have any request with empty block
        % of data
        dat = buffer('get_dat', [s(i-1),s(i)],host,port);
    else
        WaitSecs(0.002);
    end

end % for Niteration

    % Subtract the time and sample from the first iteration
s = s-s(1);
t = t-t(1);

    % Plot the results
figure('name','Data Streaming Timing')
plot(t, s, '.')
xlabel('time (s)')
xlabel('sample number')

figure('name','Rate of data streaming')
plot(s./t)
title('number of samples per second')
xlabel('iteration number')
