Home > classes > @tsdata > fitfs.m

fitfs

PURPOSE ^

FITFS computes the sample rate of the input data. If the min and max sample

SYNOPSIS ^

function varargout = fitfs(varargin)

DESCRIPTION ^

 FITFS computes the sample rate of the input data. If the min and max sample 
 intervals are within a certain tolerance (1%) of the median sample interval,
 the sample rate is returned as the median sample interval. If not, the sample
 rate is computed by fitting a grid to the input vertices. The fit is a two 
 parameter fit for the start time and the sample rate. The MATLAB function 
 lsqcurvefit is used to minimise

    sum[ (ts - linspace(t0, (N-1)/fs, N)^2 ]

 Call the function with

     fs = fitfs(x)
  or
     [fs, t0] = fitfs(x)
  or
     [fs, t0, fitted] = fitfs(x)
  where 'fitted' is a binary flag that tells whether the sample rate was
  fitted to the data. In other words, if fitted==true, then the data are
  considered to be unevenly sampled.

 The fit for fs is constrained to be within 1 sigma of the median of the
 sample intervals. The fit for t0 is constrained to be within +-1/fs.

 In addition, the recovered sample rate is reduced to 3 significant
 figures.

 M Hewitson 26-05-08

 $Id: fitfs.m,v 1.4 2008/08/13 15:41:25 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = fitfs(varargin)
0002 % FITFS computes the sample rate of the input data. If the min and max sample
0003 % intervals are within a certain tolerance (1%) of the median sample interval,
0004 % the sample rate is returned as the median sample interval. If not, the sample
0005 % rate is computed by fitting a grid to the input vertices. The fit is a two
0006 % parameter fit for the start time and the sample rate. The MATLAB function
0007 % lsqcurvefit is used to minimise
0008 %
0009 %    sum[ (ts - linspace(t0, (N-1)/fs, N)^2 ]
0010 %
0011 % Call the function with
0012 %
0013 %     fs = fitfs(x)
0014 %  or
0015 %     [fs, t0] = fitfs(x)
0016 %  or
0017 %     [fs, t0, fitted] = fitfs(x)
0018 %  where 'fitted' is a binary flag that tells whether the sample rate was
0019 %  fitted to the data. In other words, if fitted==true, then the data are
0020 %  considered to be unevenly sampled.
0021 %
0022 % The fit for fs is constrained to be within 1 sigma of the median of the
0023 % sample intervals. The fit for t0 is constrained to be within +-1/fs.
0024 %
0025 % In addition, the recovered sample rate is reduced to 3 significant
0026 % figures.
0027 %
0028 % M Hewitson 26-05-08
0029 %
0030 % $Id: fitfs.m,v 1.4 2008/08/13 15:41:25 hewitson Exp $
0031 %
0032 
0033 import utils.const.*
0034 
0035 tol = 1e-2;
0036 
0037 % Get input vertices
0038 xi  = varargin{1};
0039 % reshape x to match with linspace output
0040 ss = size(xi);
0041 if ss(1) > ss(2)
0042   xi = xi.';
0043 end
0044 d = diff(xi);
0045 
0046 % Some statistics on x
0047 medd  = median(d);
0048 mind  = min(d);
0049 maxd  = max(d);
0050 % Initial guess at sample rate
0051 ifs   = 1.0/medd;
0052 
0053 % Do we need a fit?
0054 fitted = false;
0055 if abs(maxd-medd)/medd>tol || abs(mind-medd)/medd>tol
0056   
0057   utils.helper.msg(msg.OPROC1, 'fitting t0 and sample rate for unevenly sampled data.');
0058 
0059   % We need sigma for fit constraints
0060   sigma = std(1./d);
0061   
0062   % Initial guess
0063   x0 = [xi(1) ifs];
0064 
0065   lb = [-1/ifs (1-sigma)*ifs];
0066   ub = [1/ifs (1+sigma)*ifs];
0067 
0068 %     'Display','iter',...
0069   options = optimset('MaxFunEvals', 1e3, ...
0070     'TolFun', 1e-20, 'TolX', 1e-20, 'MaxIter', 1e3);
0071 
0072   x = lsqcurvefit(@tgrid, x0, xi, xi, lb, ub, options);
0073   
0074   utils.helper.msg(msg.OPROC2, 'fit returned: %g s, %g Hz', x(1), x(2));
0075   % Return only 3 significant figures
0076   x(1) = eval(sprintf('%0.8g', x(1)));
0077   x(2) = eval(sprintf('%0.8g', x(2)));
0078   utils.helper.msg(msg.OPROC2, 'setting: %g s, %g Hz', x(1), x(2));
0079   fitted = true;
0080 else
0081   % We return the median and the intial sample time
0082   x(1) = eval(sprintf('%0.8g', xi(1)));
0083   x(2) = eval(sprintf('%0.8g', ifs));
0084 end
0085 
0086 
0087 % Set outputs
0088 if nargout == 0
0089   disp(x);
0090 elseif nargout == 1
0091   varargout{1} = x(2);
0092 elseif nargout == 2
0093   varargout{1} = x(2);
0094   varargout{2} = x(1);
0095 elseif nargout == 3
0096   varargout{1} = x(2);
0097   varargout{2} = x(1);
0098   varargout{3} = fitted;
0099 else
0100   error('### Unknown number of outputs.');
0101 end
0102 
0103 %------------------------------
0104 % Target function to minimise
0105 %------------------------------
0106   function t = tgrid(x, xdata)
0107     t0  = x(1);
0108     fs  = x(2);
0109     N = length(xdata);
0110     t = linspace(t0, (N-1)/fs, N);
0111   end
0112 
0113 end % End fitfs
0114 % END

Generated on Mon 25-Aug-2008 22:39:29 by m2html © 2003