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.7 2008/09/04 14:14:02 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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

Generated on Mon 08-Sep-2008 13:18:47 by m2html © 2003