%% File : sfun_kflookupnd.tlc generated from sfun_kflookupnd.ttlc revsion 1.4 %% $Date: 2000/05/17 20:17:04 $ %% %% Rob Aberg, 13-Aug-1999 %% Copyright 1990-2000 The MathWorks, Inc. %% %% Abstract: N-D interpolation using output from PreLook-Up %% Index Search block, using calculated index %% and interval fraction values. %implements sfun_kflookupnd "C" %% Function: BlockInstanceSetup =================================================== %% Abstract: %% Have mdlhdr.tlc include rtlibsrc.h %% %function BlockInstanceSetup(block, system) void %assign numDims = NumDataInputPorts %switch SFcnParamSettings.interpMethod %case 2 %if numDims > 2 %assign ::CompiledModel.IncludeLibsrc = 1 %endif %break %default %break %endswitch %endfunction %% BlockInstanceData ========================================================== %% Abstract: %% Create persistant data for PWork Vector. %% %function BlockInstanceData(block, system) Output /* Look-Up Table, Interpolated (N-D) (LookupNDInterpIdx) Block: */ /* % */ %% %assign bufferSize = SIZE(SFcnParamSettings.tabDims,1) %assign ttype = LibBlockOutputSignalDataTypeName(0,"") %% %% Always declare dimSizes buffer, xxx maxIdx is only if checking is on %% %if UsingMalloc % = malloc(% * sizeof(int_T)); rt_VALIDATE_MEMORY(%, %); % = malloc(% * sizeof(int_T)); rt_VALIDATE_MEMORY(%, %); %% Declare index, lambda buffers if table is 3-D or greater %if bufferSize > 2 % = malloc(% * sizeof(int_T)); rt_VALIDATE_MEMORY(%, %); % = malloc(% * sizeof(%)); rt_VALIDATE_MEMORY(%, %); %endif %else static int_T maxIdxBuffer[%] ; static int_T dimSizesBuffer[%] ; %% Declare index, lambda buffers if table is 3-D or greater %if bufferSize > 2 static int_T bpIndexBuffer[%] ; static % bpLambdaBuffer[%] ; %endif %assign vCast = "(void *) " % = %&maxIdxBuffer[0]; % = %&dimSizesBuffer[0]; %if bufferSize > 2 % = %&bpIndexBuffer[0]; % = %&bpLambdaBuffer[0]; %endif %endif %endfunction %% Function: Start ============================================================ %% Abstract: %% Set up work vectors %% %function Start(block, system) Output /* Look-Up Table, Interpolated (N-D) (LookupNDInterpIdx) Block: */ /* % */ { %assign ttype = LibBlockOutputSignalDataTypeName(0,"") %assign bufferSize = SIZE(SFcnParamSettings.tabDims,1) %assign dims = SFcnParamSettings.tabDims %assign tlcDimSizes = dims %assign tlcDimSizes[0] = 1 %foreach i = % %assign tlcDimSizes[i+1] = tlcDimSizes[i]*dims[i] %endforeach %assign declDimSizes = "int_T *dimSizes" %assign declMaxIdx = "int_T *maxIdx" % = (int_T *)%; % = (int_T *)%; %foreach idx = % dimSizes[%] = %; %endforeach %foreach idx = % maxIdx[%] = %; %endforeach %if bufferSize > 2 { %assign declBpIndex = "int_T *bpIndex" %assign declBpFraction = "% *bpFraction" % = (int_T *)%; % = (% *)%; %assign idxVar = "i0" int_T %; for (%= 0; % < %; %++) { bpIndex[%] = 0; bpFraction[%] = 0.0; } } %endif } %endfunction %% Function: Outputs ========================================================== %% Abstract: %% Perform indicated interpolation, flat or linear %% %function Outputs(block, system) Output /* %-D Look-Up Table, Interpolated (LookupNDInterpIdx) */ /* Block: % */ %assign numEl = LibBlockOutputSignalWidth(0) %assign ttype = LibBlockOutputSignalDataTypeName(0,"") %assign tmpIdx = "i1" %assign dims = SFcnParamSettings.tabDims %assign tlcDimSizes = dims %assign tlcDimSizes[0] = 1 %foreach i = % %assign tlcDimSizes[i+1] = tlcDimSizes[i]*dims[i] %endforeach %switch SFcnParamSettings.interpMethod %case 1 %% %% FLAT interpolation (i.e., don't interpolate) %% %assign flatComment = "/* flat, no interpolation */" %if NumDataInputPorts > 1 %% %% N-D case %% { % %if SFcnParamSettings.rangeErrorMode < 0 %%3 int_T maxIdx; %endif register int_T offset; %if numEl > 1 register int_T %; for (%= 0; % < %; %++) { %if SFcnParamSettings.rangeErrorMode < 0 %%3 %assign tmp = % maxIdx = %; %endif %assign istr = tmpIdx %assign istr2 = "%<<1" %else %assign istr = "0" %assign istr2 = "0" %endif %assign y = LibBlockOutputSignal(0, istr, "", "") %foreach portIdx = NumDataInputPorts %if DataInputPort[portIdx].Width == 2 %% a scalar of (k,f) %assign k = LibBlockInputSignal(portIdx, "0", "", "") %else %assign k = LibBlockInputSignal(portIdx, istr2, "", "") %endif %if tlcDimSizes[portIdx]==1 %if portIdx == 0 offset = (int_T)%; %else offset += (int_T)%; %endif %else %if portIdx == 0 offset = % * (int_T)%; %else offset += % * (int_T)%; %endif %endif %endforeach % = %; %if numEl > 1 END_FOR %endif } %else %% %% 1-D flat case, don't use offset or dimSizes %% %if numEl > 1 { % register int_T %; for (%= 0; % < %; %++) { %if SFcnParamSettings.rangeErrorMode < 0 %%3 %assign tmp = % int_T maxIdx = %; %endif %assign istr = tmpIdx %assign istr2 = "%*2" %else %assign istr = "0" %assign istr2 = "0" %endif %assign u = LibBlockInputSignal(0, istr2, "", "") %assign y = LibBlockOutputSignal(0, istr, "", "") % = %", "", "")>; %if numEl > 1 } } %endif %endif %break %case 2 %% %% LINEAR interpolation %% %assign numDims = SIZE(SFcnParamSettings.tabDims,1) %switch numDims %case 1 { /* Linear 1-D interpolation */ register int_T offset; %if numEl > 1 register int_T %; for (%= 0; % < %; %++) { %if SFcnParamSettings.rangeErrorMode < 0 %%3 %assign tmp = % int_T maxIdx = %; %endif %assign istr = tmpIdx %assign istr2 = "%*2" %assign istr21 = "%*2+1" %else %assign istr = "0" %assign istr2 = "0" %assign istr21 = "1" %endif %assign y = LibBlockOutputSignal(0, istr, "", "") %assign f = LibBlockInputSignal( 0, istr21, "", "") %assign k = LibBlockInputSignal( 0, istr2, "", "") offset = (int_T)%; % = % + %*(% - %); %if numEl > 1 } %endif } %break %case 2 %assign ttype = LibBlockOutputSignalDataTypeName(0,"") { /* Linear 2-D interpolation */ % ylower; % yupper; register int_T offset; %if numEl > 1 %% xxx clipping code int_T maxIdx = magic; register int_T %; for (%= 0; % < %; %++) { %if SFcnParamSettings.rangeErrorMode < 0 %%3 %assign tmp = % int_T maxIdx = %; %endif %assign istr = tmpIdx %assign istr2 = "%*2" %else %assign istr = "0" %assign istr2 = "0" %assign istr21 = "1" %endif %assign y1 = LibBlockParameter(table, "offset", "", "") %assign y2 = LibBlockParameter(table, "offset+1", "", "") %assign y = LibBlockOutputSignal(0, istr, "", "") %if DataInputPort[0].Width == 2 %% a scalar of (k,f) %assign rowIdx = "0" %assign rowIdx1 = "1" %else %assign rowIdx = istr2 %assign rowIdx1 = "%+1" %endif %if DataInputPort[1].Width == 2 %% a scalar of (k,f) %assign colIdx = "0" %assign colIdx1 = "1" %else %assign colIdx = istr2 %assign colIdx1 = "%+1" %endif %assign krow = LibBlockInputSignal(0, rowIdx, "", "") %assign frow = LibBlockInputSignal(0, rowIdx1, "", "") %assign kcol = LibBlockInputSignal(1, colIdx, "", "") %assign fcol = LibBlockInputSignal(1, colIdx1, "", "") offset = (int_T)% + ... %*(int_T)%; ylower = % + %*( % - %); offset += %; yupper = % + %*( % - %); % = ylower + %*(yupper - ylower); %if numEl > 1 } %endif } %break %default %assign t = LibBlockParameterAddr(table,"","",0) { /* Linear %-D interpolation */ %assign declBpIndex = "int_T *bpIndex" %assign declBpFraction = "% *bpFraction" % = (int_T *)%; % = (% *)%; %if numEl > 1 %assign tmpIdx = "i" register int_T %; for (%= 0; % < %; %++) { %if SFcnParamSettings.rangeErrorMode < 0 %%3 %assign tmp = % int_T maxIdx = %; %endif %assign istr = tmpIdx %assign istr2 = "%*2" %assign istr21 = "%*2+1" %else %assign istr = "0" %assign istr2 = "0" %assign istr21 = "1" %endif %foreach portIdx = numDims bpIndex[%] = (int_T)%; bpFraction[%] = %; %endforeach %assign iCast = "(int_T *) " %assign y = LibBlockOutputSignal(0, istr, "", "") % = rt_LookupLinearInterpND_%(%, 0, ... bpIndex, bpFraction, %%, %); %if numEl > 1 } %endif } %endswitch %endswitch %endfunction %% %% Function: Terminate ========================================================= %% Abstract: %% For malloc'ed work vectors, free memory %% %function Terminate(block, system) Output %if UsingMalloc /* %-D Look-Up Table, Interpolated (LookupNDInterpIdx) */ /* Block: % */ #if defined(RT_MALLOC) || defined(MATLAB_MEX_FILE) %assign bufferSize = SIZE(SFcnParamSettings.tabDims,1) rt_FREE(%); rt_FREE(%); %% %% rt_FREE the allocated index & lambda buffers %% for when table is 3-D or greater %if bufferSize > 2 rt_FREE(%); rt_FREE(%); %endif #endif %endif %endfunction %%[EOF] sfun_kflookupnd.tlc