Appendix D. MATLAB Script File for BASS Calibration
%function [cal_cms_in, bass_cms_out, fit_err, calc_factor] =
% basscal(bass_count, cal_ns_in, avgint, offset, file);
%
% display the results from a bass calibration with
% least squares fit, compute the conversion factor for
% each calibration data point, plot the results.
% bass_count = bass raw data in counts, a cumulative sum of
% samples collected over the given average interval avgint
% cal_ns_in = calibrator's ns delay input to BASS
% avgint = number of samples per data point
% offset = ns delay offset applied to zero the output at start
% file = filename in which to save text output
% cal_cms_in = calibrator's delay input in cm/s
% bass_cms_out = cm/s computed from the BASS output
% fit_err = error from a linear fit to the BASS output
% calc_factor = the count to cm/s conversion factor based on each data point
%
% Written by Marinna Martini for the
% U.S. Geological Survey, Woods Hole, MA
function [fit_err, calc_factor] = ...
basscal(bass_count, cal_ns_in, avgint, offset, file);
if exist('avgint') ~= 1,
avgint = 1; % assume no averaging
end
% ---- account for the offset
if exist('offset') ~= 1, offset = 0; end
% ---- convert to real units
Factor = 0.03; % 0.03 cm/s per count
bass_cms=(bass_count./avgint).*Factor; % BASS normalized using generic factor
cal_cms_in=cal_ns_in.*0.75; % 0.75 cm/s per ns
calc_factor=cal_cms_in./(bass_count./avgint);
% ---- liner fit
l=length(bass_count);
c = polyfit(cal_ns_in, bass_cms,1);
fit_cms = polyval(c, cal_ns_in);
clf
% *********************** upper plot **************************
ptr=axes('Position',[0.15 0.1 0.8 0.6]);
% ---- display fit and data points
plot(cal_ns_in,bass_cms,'x',cal_ns_in,fit_cms,'-',cal_ns_in,cal_cms_in,'--');
grid
if mean(bass_cms) > 0,
set(ptr,'ylim',[0 120],'xlim',[0 160]);
else
set(ptr,'ylim',[-120 0],'xlim',[-160 0]);
end
%title('BASS Calibration Linearity Check')
text('units','normalized','position',[0.01 0.85],'string','--- least squares fit')
text('units','normalized','position',[0.01 0.90],'string',' X BASS output')
text('units','normalized','position',[0.01 0.95],'string','- - Calibrator Input')
xlabel('Nanosecond Delay')
ylabel('cm/s')
fit_err=fit_cms-bass_cms;
txt=sprintf('mean(calibrator-BASS) = %7.4f',mean(cal_cms_in-bass_cms));
text('units','normalized','position',[0.5 0.05],'string',txt)
txt=sprintf('fit error: std = %7.4f',std(fit_err));
text('units','normalized','position',[0.6 0.1],'string',txt)
% *********************** lower plot *************************
ptr=axes('Position',[0.15 0.725 0.8 0.15]);
% ---- plot different in cm/s
plot(cal_ns_in,calc_factor,'-');
if mean(bass_cms) > 0,
set(ptr,'ylim',[0.028 0.032 ],'xlim',[0 160]);
else
set(ptr,'ylim',[0.028 0.032],'xlim',[-160 0]);
end
ylabel('cm/s per count')
set(ptr,'xtick',[]);
text('units','normalized','position',[0.01 1.1],'string',...
'Conversion factor computed from calibration data');
grid
text('units','normalized','position',[0.45 1.5],...
'HorizontalAlignment','center',...
'string',['BASS: Linearity Check ',date]);
%
% ------- Generate Text Report
%
if exist('file') == 1, % user wants output
if exist(file) >= 2, % file already exists
disp([file ' already exists. Saving to basscal.out'])
file='basscal.out';
delete basscal.out,
end
eval(['diary ' file])
end
fprintf ('\n')
fprintf(' BASS Calibration %s\n', date)
fprintf('\n')
fprintf('Delay Input\tOut: BASS, Fit\tError Factor\n')
fprintf('ns\tcm/s\tcm/s\tcm/s\tcm/s\tcm/s per count\n')
for j=1:l,
fprintf('%04.1f\t%04.1f\t%6.3f\t%6.3f\t%5.3f\t%6.4f\n', cal_ns_in(j),...
cal_cms_in(j), bass_cms(j), fit_cms(j), fit_err(j), calc_factor(j));
end
fprintf('\nOffset applied to zero BASS output: %4.2f ns\n',offset);
fprintf('Number of samples averaged per calibration point: %3.0f\n',avgint);
fprintf('Mean(calibrator-BASS) = %7.4f cms\n',mean(cal_cms_in-bass_cms));
fprintf('Fit error: std = %7.4f cms\n',std(fit_err));
if exist('file') == 1,
diary off
end
|