interpolates 1-D data (table lookup) and utilizes griddedInterpolant if availabe in the used MATLAB version call y = matRad_interp1(xi,yi,x) y = matRad_interp1(xi,yi,x,extrapolation) input xi: sample points yi: corresponding data to sample points x: query points for interpolation extrapolation: (optional) strategy for extrapolation. Similar to interp1. NaN is the default extrapolation value output y: interpolated data Note that all input data has to be given as column vectors for a correct interpolation. yi can be a matrix consisting out of several 1-D datasets in each column, which will all be interpolated for the given query points. Reference - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Copyright 2020 the matRad development team. This file is part of the matRad project. It is subject to the license terms in the LICENSE file found in the top-level directory of this distribution and at https://github.com/e0404/matRad/LICENSES.txt. No part of the matRad project, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the LICENSE file. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0001 function y = matRad_interp1(xi,yi,x,extrapolation) 0002 % interpolates 1-D data (table lookup) and utilizes griddedInterpolant 0003 % if availabe in the used MATLAB version 0004 % 0005 % call 0006 % y = matRad_interp1(xi,yi,x) 0007 % y = matRad_interp1(xi,yi,x,extrapolation) 0008 % 0009 % input 0010 % xi: sample points 0011 % yi: corresponding data to sample points 0012 % x: query points for interpolation 0013 % extrapolation: (optional) strategy for extrapolation. Similar to 0014 % interp1. NaN is the default extrapolation value 0015 % 0016 % output 0017 % y: interpolated data 0018 % 0019 % Note that all input data has to be given as column vectors for a correct 0020 % interpolation. yi can be a matrix consisting out of several 1-D datasets 0021 % in each column, which will all be interpolated for the given query points. 0022 % 0023 % Reference 0024 % - 0025 % 0026 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0027 % 0028 % Copyright 2020 the matRad development team. 0029 % 0030 % This file is part of the matRad project. It is subject to the license 0031 % terms in the LICENSE file found in the top-level directory of this 0032 % distribution and at https://github.com/e0404/matRad/LICENSES.txt. No part 0033 % of the matRad project, including this file, may be copied, modified, 0034 % propagated, or distributed except according to the terms contained in the 0035 % LICENSE file. 0036 % 0037 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0038 0039 persistent isGriddedInterpolantAvailable; 0040 0041 if isempty(isGriddedInterpolantAvailable) 0042 [env, ~] = matRad_getEnvironment(); 0043 switch env 0044 case 'MATLAB' 0045 isGriddedInterpolantAvailable = exist('griddedInterpolant','class'); 0046 case 'OCTAVE' 0047 isGriddedInterpolantAvailable = exist('griddedInterpolant'); 0048 end 0049 end 0050 0051 if nargin < 4 0052 extrapolation = NaN; 0053 end 0054 0055 0056 0057 % manual interpolation for only one query point to save time 0058 if numel(x) == 1 0059 0060 ix1 = find((x >= xi), 1, 'last'); 0061 ix2 = find((x <= xi), 1, 'first'); 0062 0063 if ix1 == ix2 0064 y = yi(ix1,:); 0065 elseif ix2 == ix1 + 1 0066 y = yi(ix1,:) + ( yi(ix2,:)-yi(ix1,:) ) * ( x - xi(ix1) ) / ( xi(ix2) - xi(ix1) ); 0067 else 0068 if isscalar(extrapolation) 0069 y = extrapolation; 0070 elseif strcmp(extrapolation,'extrap') 0071 %In this unlikely event fall back to classic 0072 y = interp1(xi,yi,x,'linear',extrapolation); 0073 else 0074 error('Invalid extrapolation argument!'); 0075 end 0076 end 0077 0078 elseif isGriddedInterpolantAvailable 0079 0080 if isscalar(extrapolation) 0081 extrapmethod = 'none'; 0082 elseif strcmp(extrapolation,'extrap') 0083 extrapmethod = 'linear'; 0084 else 0085 error('Invalid extrapolation argument!'); 0086 end 0087 0088 if size(yi,2) > 1 0089 % interpolation for multiple 1-D datasets 0090 samplePoints = {xi, 1:size(yi,2)}; 0091 queryPoints = {x, 1:size(yi,2)}; 0092 else 0093 % interpolation for a single 1-D dataset 0094 samplePoints = {xi}; 0095 queryPoints = {x}; 0096 end 0097 0098 F = griddedInterpolant(samplePoints,yi,'linear',extrapmethod); 0099 0100 y = F(queryPoints); 0101 0102 if isnumeric(extrapolation) && ~isnan(extrapolation) 0103 y(isnan(y)) = extrapolation; 0104 end 0105 0106 0107 else 0108 0109 % for older matlab versions use this code 0110 y = interp1(xi,yi,x,'linear',extrapolation); 0111 0112 end