logovernightInPru %contains the "values" variable, which contains the return value of rt_read_time() every 16384*16samples. Times are in ns
measuredUs=values/1000; %now they are microseconds
measuredUs=measuredUs(1:end)-measuredUs(1); % remove the time offset, so that we start with 0
Fs=44100;
samplesPerBuffer=16;
sincus=1/Fs*1e6;
usPerBuffer=samplesPerBuffer*sincus;
numMeasurements=length(measuredUs);
samplesPerMeasurement=16384*samplesPerBuffer;
totalSamples=samplesPerMeasurement*Fs;
usPerMeasurement=samplesPerMeasurement*sincus;
expectedUs=((0:numMeasurements-1)')*usPerMeasurement; %ideal time axis in us
minutes=expectedUs/1e6/60; %ideal time axis in seconds

expectedUs1=expectedUs(1:1000)-expectedUs(1);
measuredUs1=measuredUs(1:1000)-measuredUs(1);
minutes1=expectedUs1/1e6/60;

expectedUs2=expectedUs1;
measuredUs2=measuredUs(end-999:end)-measuredUs(end-999);
minutes2=minutes(end-999:end);
plot(expectedUs1,measuredUs1-expectedUs1,expectedUs2,measuredUs2-expectedUs2);
error=measuredUs-expectedUs;
error1=measuredUs1-expectedUs1;
error2=measuredUs2-expectedUs2;
disp('On all the data:')
disp(['Average error is ' num2str(mean(error)) 'us, that is ' num2str(mean(error)/usPerBuffer*100) '% of a single buffer. Error range is ' num2str((abs(max(error))+abs(min(error)))/usPerBuffer*100) '% of a single buffer'])
disp('On the first 1000 measurements:')
disp(['Average error is ' num2str(mean(error1)) 'us, that is ' num2str(mean(error1)/usPerBuffer*100) '% of a single buffer. Error range is ' num2str((abs(max(error1))+abs(min(error1)))/usPerBuffer*100) '% of a single buffer'])
disp('On the last 1000 measurements:')
disp(['Average error is ' num2str(mean(error2)) 'us, that is ' num2str(mean(error2)/usPerBuffer*100) '% of a single buffer. Error range is ' num2str((abs(max(error2))+abs(min(error2)))/usPerBuffer*100) '% of a single buffer'])


figure(1)
plot(minutes,error,[minutes(1) minutes(end)], [0 0]); hold on; plot(minutes,polyval(polyfit((1:length(expectedUs))',error,1), 1:length(expectedUs)),'linewidth',3); hold off
xlabel('time[minutes]')
ylabel('error [\mus]','Interpreter','Tex')
title(['Error. One buffer is ' num2str(usPerBuffer) '\mus. A measurement is taken every ' num2str(usPerMeasurement) '\mus'])
axis tight
disp(['Average error is ' num2str(mean(measuredUs-expectedUs)) 'us, that is ' num2str(mean(error)/usPerBuffer*100) '% of a single buffer. Error range is ' num2str((abs(max(error))+abs(min(error)))/usPerBuffer*100) '% of a single buffer'])
legend('Error','0','Linear regression of error')
print('-dpng','-r300','error')

figure(2)
semilogy(minutes, abs(error)./expectedUs, [minutes(1) minutes(end)], [0 0])
xlabel('time[minutes]')
ylabel('abs(error)[relative]')
title('Relative absolute error')
axis([minutes(1) minutes(end) 1e-11 1e-5])
print('-dpng','-r300','relative error')

% figure(3)
% 
% subplot(1,3,1)
% plot(minutes, abs(measuredUs./expectedUs-1),[minutes(1) minutes(end)],[0 0])
% xlabel('time[minutes]')
% ylabel('ratio-1')
% title('Ratio-1 of measured and expected values')
% axis tight
% 
% subplot(1,3,2)
% plot(minutes1, abs(measuredUs1./expectedUs1-1),[minutes1(1) minutes1(end)],[0 0])
% xlabel('time[minutes]')
% ylabel('ratio-1')
% title('Ratio-1 of measured and expected values')
% axis tight
% 
% subplot(1,3,3)
% plot(minutes2, abs(measuredUs2./expectedUs2-1),[minutes2(1) minutes2(end)],[0 0])
% xlabel('time[minutes]')
% ylabel('ratio-1')
% title('Ratio-1 of measured and expected values')
% axis tight
% 
