%HECUBIC - cubic polynomial fit to Hall effect sensor data
%
%    p = hecubic( fname )
%    p = hecubic( [m_angle m_he_raw] ), angle in radians
%
%B. A. Moran, 26-Sep-1998

%    Copyright (C) 1995-1998
%    Massachussetts Institute of Technology
%    Sea Grant AUV Laboratory
%    $Id: hecubic.m,v 1.1 2000/01/10 19:08:00 rob Exp $

function p = hecubic( arg1 )

if isstr( arg1 )
    [fid,message] = fopen( arg1, 'r' );
    if fid == -1, error( message ); end
    [a, count] = fscanf( fid, '%x %f' );
    fclose( fid );
    rawHE = a(1:2:count);
    finpos = a(2:2:count);
else
    finpos = 180/pi*arg1(:,1);
    rawHE = arg1(:,2);
end

[p,S] = polyfit( rawHE, finpos, 3 );
y = linspace( min(rawHE), max(rawHE), 256 );
[x,delta] = polyval( p, y, S );

plot( finpos, rawHE, 'r+', x, y, 'm', x-delta, y, 'b', x+delta, y, 'b' );

% set limits on axes... Y-axis multiples of 16 to aid in HE readings

set( gca, 'YTick', 16*(floor(min(rawHE)/16):ceil(max(rawHE)/16)), ...
    'XTick', -30:10:30, 'XLim', [-35 35] );

