%% $RCSfile: sdsppolyval2.tlc,v $ %% $Revision: 1.2 $ %% $Date: 2000/01/19 20:24:15 $ %% %% Copyright (c) 1995-2000 The MathWorks, Inc. %% %% Abstract: Polynomial evaluation S-function block for sdsppolyval2.c %% %implements "sdsppolyval2" "C" %%%%%%%%%%%%%%%%%%%%% %% Function: BlockInstanceSetup =============================================== %% %function BlockInstanceSetup(block, system) void %% %assign INPORT1 = 0 %assign INPORT2 = 1 %assign OUTPORT = 0 %% %assign useParamCoeffs = SFcnParamSettings.UseConstCoeffsNotInport %% %if useParamCoeffs %assign coeffsAreComplex = SFcnParamSettings.CoeffsAreComplex %assign numCoeffs = SFcnParamSettings.NumCoeffs %else %assign coeffsAreComplex = (LibBlockInputSignalIsComplex(INPORT2) != 0) %assign numCoeffs = LibDataInputPortWidth(INPORT2) %endif %% %assign block = block + INPORT1 + INPORT2 + OUTPORT + useParamCoeffs %assign block = block + coeffsAreComplex + numCoeffs %% %endfunction %% BlockInstanceSetup %%%%%%%%%%%%%%%%%%%%% %% Function: Outputs =========================================================== %% %function Outputs(block, system) Output /* DSP Blockset Polyval (%) - % - Output */ { %assign complexInputSignal = (LibBlockInputSignalIsComplex(INPORT1) != 0) %% %if useParamCoeffs %% %% This is the case for when the block parameter is to be used %% for the coefficients source (THERE IS NO INPORT2 for this case). %% The coefficients may be real or complex. %% %if !coeffsAreComplex /* Real coefficients */ const real_T realCoeffs[%] = { %% Array initialization: if non-scalar, %% first generate all but last coeff value %if numCoeffs > 1 %foreach cnt = (numCoeffs-1) %, %endforeach %endif %% Finally add the last coefficient value to end of array init. %% Note: this also handles the single scalar (real) coefficient.case % }; %if !complexInputSignal %% Real coeffs, Real input sig % %else %% Real coeffs, Cplx input sig % %endif %else /* Complex coefficients */ const creal_T cplxCoeffs[%] = { %% Array initialization: if non-scalar, first generate all but last coeff value %if numCoeffs > 1 %foreach cnt = (numCoeffs-1) { %, % }, %endforeach %endif %% Finally add the last coefficient value to end of array init. %% Note: this also handles the single scalar (complex) coeff case. { %, % } }; %if !complexInputSignal %% Cplx coeffs, Real input sig % %else %% Cplx coeffs, Cplx input sig % %endif %endif %else %% %% This is the case for when the block's INPORT2 is to be used for %% the coefficients source. The coefficients may be real or complex. %% %if !coeffsAreComplex const real_T *realCoeffs = %; /* coeffs input location */ %if !complexInputSignal %% Real coeffs, Real input sig % %else %% Real coeffs, Cplx input sig % %endif %else const creal_T *cplxCoeffs = %; /* coeffs input location */ %if !complexInputSignal %% Cplx coeffs, Real input sig % %else %% Cplx coeffs, Cplx input sig % %endif %endif %endif } %endfunction %% Outputs %% ------------ %% SUBFUNCTIONS %% ------------ %%%%%%%%%%%%%%%%%%%%% %% Subfunction: GenCodeForRlCfRlInpSig ====================================== %% %function GenCodeForRlCfRlInpSig(block) Output real_T *y = %; /* output location */ const real_T *u = %; /* input location */ %if (LibBlockInputSignalWidth(INPORT1) > 1) register int i; /* input index */ %endif /* Using Horner's Method to evaluate polynomial. * * This method is the most efficient in terms of * multiplies and adds (it also loops nicely): * * y = ax^4 + bx^3 + cx^2 + dx + e * * becomes * * y = e + x*( d + x( c + x*( b + x*a ) ) ) */ %if (LibBlockInputSignalWidth(INPORT1) > 1) for (i = 0; i < %; i++) { /* loop over inputs */ register real_T x = *u++; %else register real_T x = *u; %endif register const real_T *tmpReCoeffs = realCoeffs; register real_T accum = *tmpReCoeffs++; register int_T j; for (j = 1; j < %; j++) { /* loop over coeffs */ accum *= x; accum += *tmpReCoeffs++; } %if (LibBlockInputSignalWidth(INPORT1) > 1) *y++ = accum; } %else *y = accum; %endif %endfunction %% GenCodeForRlCfRlInpSig %%%%%%%%%%%%%%%%%%%%% %% Subfunction: GenCodeForRlCfCpInpSig ====================================== %% %function GenCodeForRlCfCpInpSig(block) Output creal_T *y = %; /* output location */ const creal_T *u = %; /* input location */ %if (LibBlockInputSignalWidth(INPORT1) > 1) register int i; /* input index */ %endif /* Using Horner's Method to evaluate polynomial. * * This method is the most efficient in terms of * multiplies and adds (it also loops nicely): * * y = ax^4 + bx^3 + cx^2 + dx + e * * becomes * * y = e + x*( d + x( c + x*( b + x*a ) ) ) */ %if (LibBlockInputSignalWidth(INPORT1) > 1) for (i = 0; i < %; i++) { /* loop over inputs */ %endif register const real_T *tmpReCoeffs = realCoeffs; register creal_T accum; register creal_T x; register int_T j; accum.re = *tmpReCoeffs++; accum.im = 0.0; x.re = u->re; %if (LibBlockInputSignalWidth(INPORT1) > 1) x.im = (u++)->im; %else x.im = u->im; %endif for (j = 1; j < %; j++) { /* loop over coeffs */ volatile real_T reInputProduct; volatile real_T imInputProduct; reInputProduct = CMULT_RE(accum, x); imInputProduct = CMULT_IM(accum, x); accum.re = reInputProduct; accum.im = imInputProduct; accum.re += *tmpReCoeffs++; } y->re = accum.re; %if (LibBlockInputSignalWidth(INPORT1) > 1) y++->im = accum.im; } %else y->im = accum.im; %endif %endfunction %% GenCodeForRlCfCpInpSig %%%%%%%%%%%%%%%%%%%%% %% Subfunction: GenCodeForCpCfRlInpSig ====================================== %% %function GenCodeForCpCfRlInpSig(block) Output creal_T *y = %; /* output location */ const real_T *u = %; /* input location */ %if (LibBlockInputSignalWidth(INPORT1) > 1) register int i; /* input index */ %endif /* Using Horner's Method to evaluate polynomial. * * This method is the most efficient in terms of * multiplies and adds (it also loops nicely): * * y = ax^4 + bx^3 + cx^2 + dx + e * * becomes * * y = e + x*( d + x( c + x*( b + x*a ) ) ) */ %if (LibBlockInputSignalWidth(INPORT1) > 1) for (i = 0; i < %; i++) { /* loop over inputs */ register real_T x = *u++; %else register real_T x = *u; %endif register const creal_T *tmpCplxCoeffs = cplxCoeffs; register creal_T accum; register int_T j; accum.re = tmpCplxCoeffs->re; accum.im = tmpCplxCoeffs++->im; for (j = 1; j < %; j++) { /* loop over coeffs */ accum.re *= x; accum.im *= x; accum.re += tmpCplxCoeffs->re; accum.im += tmpCplxCoeffs++->im; } y->re = accum.re; %if (LibBlockInputSignalWidth(INPORT1) > 1) y++->im = accum.im; } %else y->im = accum.im; %endif %endfunction %% GenCodeForCpCfRlInpSig %%%%%%%%%%%%%%%%%%%%% %% Subfunction: GenCodeForCpCfCpInpSig ====================================== %% %function GenCodeForCpCfCpInpSig(block) Output creal_T *y = %; /* output location */ const creal_T *u = %; /* input location */ %if (LibBlockInputSignalWidth(INPORT1) > 1) register int i; /* input index */ %endif /* Using Horner's Method to evaluate polynomial. * * This method is the most efficient in terms of * multiplies and adds (it also loops nicely): * * y = ax^4 + bx^3 + cx^2 + dx + e * * becomes * * y = e + x*( d + x( c + x*( b + x*a ) ) ) */ %if (LibBlockInputSignalWidth(INPORT1) > 1) for (i = 0; i < %; i++) { /* loop over inputs */ %endif register const creal_T *tmpCplxCoeffs = cplxCoeffs; register creal_T x; register creal_T accum; register int_T j; accum.re = tmpCplxCoeffs->re; accum.im = tmpCplxCoeffs++->im; x.re = u->re; %if (LibBlockInputSignalWidth(INPORT1) > 1) x.im = (u++)->im; %else x.im = u->im; %endif for (j = 1; j < %; j++) { /* loop over coeffs */ volatile real_T reInputProduct; volatile real_T imInputProduct; reInputProduct = CMULT_RE(accum, x); imInputProduct = CMULT_IM(accum, x); accum.re = reInputProduct; accum.im = imInputProduct; accum.re += tmpCplxCoeffs->re; accum.im += tmpCplxCoeffs++->im; } y->re = accum.re; %if (LibBlockInputSignalWidth(INPORT1) > 1) y++->im = accum.im; } %else y->im = accum.im; %endif %endfunction %% GenCodeForCpCfCpInpSig %% [EOF] sdsppolyval2.tlc