%% $RCSfile: sdspmin2.tlc,v $ %% $Revision: 1.8 $ %% $Date: 2000/06/15 20:37:44 $ %% %% Copyright 1995-2000 The MathWorks, Inc. %% %% Abstract: Find the min value of a vector %implements sdspmin2 "C" %% Function: BlockInstanceSetup =============================================== %% %function BlockInstanceSetup(block, system) void %% Function menu choices %assign fcnValandIdx = 1 %assign fcnVal = 2 %assign fcnIdx = 3 %assign fcnRunning = 4 %assign fcn = SFcnParamSettings.Fcn %assign INPORT_DATA = 0 %assign INPORT_RESET = 1 %assign OUTPORT_DATA = 0 %assign OUTPORT_IDX = (fcn != fcnIdx) ? 1 : 0 %assign cplx = (LibBlockInputSignalIsComplex(INPORT_DATA) != 0) %assign dType = cplx ? "creal_T" : "real_T" %assign inWidth = LibBlockInputSignalWidth(INPORT_DATA) %assign isFullMatrix = LibBlockInputSignalIsFullMatrix(INPORT_DATA) %assign framebased = LibBlockInputSignalIsFrameData(INPORT_DATA) %assign numDims = LibBlockInputSignalNumDimensions(INPORT_DATA) %assign dims = LibBlockInputSignalDimensions(INPORT_DATA) %assign nChans = (framebased || isFullMatrix) ? dims[1] : 1 %assign nSamps = (framebased || isFullMatrix) ? dims[0] : inWidth %assign resetport = CAST("Boolean",(SFcnParamSettings.isRstPort == "Yes")) %assign block = block + ... fcnValandIdx + fcnVal + fcnIdx + ... fcnRunning + fcn + INPORT_DATA + ... INPORT_RESET + OUTPORT_DATA + OUTPORT_IDX + ... cplx + dType + inWidth + ... framebased + nChans + nSamps + ... resetport %endfunction %% BlockInstanceSetup %% Function: InitializeConditions ============================================= %% %function InitializeConditions(block, system) Output %% %if(fcn == fcnRunning) /* DSP Blockset Minimum (%) - % */ %% % = 1; %% %endif %% %endfunction %% InitializeConditions %% Function: Outputs ========================================================== %% %function Outputs(block, system) Output /* DSP Blockset Minimum (%) - % */ %% %switch fcn %case fcnRunning %% %% Block could be multirate if and only if it has more than %% one input port (eg, a reset input). If it is multirate, %% generate "sample hit" conditional so code executes at the %% block's base rate, and not the overall model base rate. %% %assign differentPortRates = !LibIsSFcnSingleRate(block) %% %if differentPortRates if (%) { % %endif %if !framebased %if !cplx % %else % %endif %else %if !cplx % %else % %endif %endif %if differentPortRates } %endif %break %% %case fcnVal /* Output minimun value. */ %case fcnIdx /* Output index of minimum value. */ %case fcnValandIdx /* Output the minimum value and it's index. */ %% %if (inWidth == 1) %% Scalar input %% %% When width is 1, the min output equals the input %% and the index can only be 1. %% %if fcn != fcnIdx % = %; %endif %if fcn != fcnVal % = 1.0; %endif %else %% %if !cplx % %else % %endif %% %endif %break %endswitch %endfunction %% Function: Min_run_sr ====================================================== %function Min_run_sr(block) Output %% { /* Sample-based running MINIMUM with real inputs */ real_T *u = %; real_T *y = %; real_T *cache = %; boolean_T *reset = %; %if resetport if(% != 0.0) { *reset = 1; } %endif %% %if inWidth == 1 if(*reset) { *y = *cache = *u; /* output = input */ *reset = 0; /* No longer in reset mode */ } else { const real_T input = *u; const real_T tmin = *cache; *y = *cache = (input < tmin) ? input : tmin; } %% %else %% if(*reset) { /* Reset mode where the current input is the minimum */ int_T i; for(i=0; i<%; i++) { *y++ = *cache++ = *u++; /* output = input */ } *reset = 0; /* No longer in reset mode */ } else { /* Compare latest inputs to stored minimum values: */ int_T i; for(i=0; i<%; i++) { const real_T input = *u++; const real_T tmin = *cache; *y++ = *cache++ = (input < tmin) ? input : tmin; } } %endif } %endfunction %% Function: Min_run_sc ====================================================== %function Min_run_sc(block) Output %% { /* Sample-based running MINIMUM with complex inputs */ typedef struct { real_T magsq; creal_T cmplx; } minmax_cache; creal_T *u = %; creal_T *y = %; minmax_cache *cache = (minmax_cache *)%; boolean_T *reset = %; %if resetport if(% != 0.0) { *reset = 1; } %endif %% %if inWidth == 1 if(*reset) { const creal_T newval = *u; *y = cache->cmplx = newval; cache->magsq = CMAGSQ(newval); *reset = 0; /* No longer in reset mode */ } else { const creal_T newval = *u; const real_T newmag = CMAGSQ(newval); /* Mag squared of current input element */ real_T minmag = cache->magsq; creal_T minval = cache->cmplx; /* Compare to "cached" mag-squared value of last "minimum" complex element: */ if (newmag < minmag) { minval = newval; minmag = newmag; } *y = cache->cmplx = minval; cache->magsq = minmag; } %% %else %% if (*reset) { int_T i; for(i=0; i<%; i++) { const creal_T newval = *u++; *y++ = cache->cmplx = newval; (cache++)->magsq = CMAGSQ(newval); } *reset = 0; /* No longer in reset mode */ } else { /* Compare latest inputs to stored minimum values: */ int_T i; for(i=0; i<%; i++) { const creal_T newval = *u++; const real_T newmag = CMAGSQ(newval); /* Mag squared of current input element */ real_T minmag = cache->magsq; creal_T minval = cache->cmplx; /* Compare to "cached" mag-squared value of last "minimum" complex element: */ if (newmag < minmag) { minval = newval; minmag = newmag; } *y++ = cache->cmplx = minval; (cache++)->magsq = minmag; } } %endif } %endfunction %% Function: Min_run_fr ====================================================== %function Min_run_fr(block) Output %% { /* Frame-based running MINIMUM with real inputs */ real_T *u = %; real_T *y = %; real_T *cache = %; boolean_T *reset = %; %if resetport if(% != 0.0) { *reset = 1; } %endif %% if (*reset) { /* * If in reset mode, then the first sample * in each will be the starting minimum. */ %if nChans > 1 int_T i; for(i=0; i<%; i++) { %endif %% real_T tmin; int_T j; for(j=0; j<%; j++) { const real_T val = *u++; if(j==0) { tmin = val; *y++ = val; } else { if (val < tmin) { tmin = val; } *y++ = tmin; } } *cache++ = tmin; %% %if nChans > 1 } %endif *reset = 0; /* No longer in reset mode */ } else { /* Compare latest inputs to stored minimum values: */ %if nChans > 1 int_T i; for(i=0; i<%; i++) { %endif real_T tmin = *cache; int_T j; for(j=0; j<%; j++) { const real_T val = *u++; if (val < tmin) { tmin = val; } *y++ = tmin; } *cache++ = tmin; %% %if nChans > 1 } %endif } } %endfunction %% Function: Min_run_fc ====================================================== %function Min_run_fc(block) Output %% { /* Frame-based running MINIMUM with complex inputs */ typedef struct { real_T magsq; creal_T cmplx; } minmax_cache; creal_T *u = %; creal_T *y = %; minmax_cache *cache = (minmax_cache *)%; boolean_T *reset = %; %if resetport if(% != 0.0) { *reset = 1; } %endif if (*reset) { /* We are in reset mode * the current input is the output (minimum) value */ %if nChans > 1 int_T i; for(i=0; i<%; i++) { %endif %% real_T minmag; creal_T minval; int_T j; for(j=0; j<%; j++) { const creal_T newval = *u++; const real_T newmag = CMAGSQ(newval); /* Mag squared of current input element */ if(j==0) { minmag = newmag; minval = newval; *y++ = newval; } else { if (newmag < minmag) { minval = newval; minmag = newmag; } *y++ = minval; } } cache->magsq = minmag; (cache++)->cmplx = minval; %if nChans > 1 } %endif %% *reset = 0; /* No longer in reset mode */ } else { %if nChans > 1 int_T i; for(i=0; i<%; i++) { %endif %% real_T minmag = cache->magsq; creal_T minval = cache->cmplx; int_T j; for(j=0; j<%; j++) { const creal_T newval = *u++; const real_T newmag = CMAGSQ(newval); /* Mag squared of current input element */ /* Compare to "cached" mag-squared value of last "minimum" complex element: */ if (newmag < minmag) { minval = newval; minmag = newmag; } *y++ = minval; } cache->magsq = minmag; (cache++)->cmplx = minval; %if nChans > 1 } %endif } } %endfunction %% Function: Min_r ====================================================== %function Min_r(block) Output %% { real_T *u = %; int_T m_idx = 0; int idx; %% %if nChans > 1 int j; for (j=0; j < %; j++) { real64_T m_val = *u++; /* 1st element is initialmin */ m_idx = 0; /* 0-based index of 1st element is 0 */ %else %% real64_T m_val = *u++; /* 1st element is initial min */ %endif for (idx=1; idx < %; idx++) { real_T val = *u++; if (val < m_val) { m_val = val; m_idx = idx; } } %% fcnVal: scalar output, minimum value %% fcnIdx: scalar output, index of minimum value %% fcnValandIdx: 2 outputs, minimum value and index %% %if nChans > 1 %if ((fcn == fcnVal) || (fcn == fcnValandIdx)) % = m_val; %else % = (real_T)(m_idx + 1); %endif %% %if (fcn == fcnValandIdx) /* 2nd output must be the index * Convert C-index to 1-based MATLAB index */ % = (real_T)(m_idx + 1); %endif } /* nChans loop */ %else %% %if ((fcn == fcnVal) || (fcn == fcnValandIdx)) % = m_val; %else % = (real_T)(m_idx + 1); %endif %% %if (fcn == fcnValandIdx) { /* 2nd output must be the index * Convert C-index to 1-based MATLAB index */ % = (real_T)(m_idx + 1); } %endif %endif %% nChans fill in output loop } %endfunction %% Function: Min_c ====================================================== %function Min_c(block) Output %% { %if inWidth != 1 creal_T *u = %; %endif %% %if nChans > 1 int j; for (j=0; j < %; j++) { real64_T m_val = CMAGSQ(*u); /* 1st element is initial min */ int_T m_idx = 0; /* 0-based index of 1st element is 0 */ int idx; u++; %else real64_T m_val = CMAGSQ(*u); /* 1st element is initial min */ int_T m_idx = 0; int idx; u++; %endif for (idx=1; idx < %; idx++) { real_T val = CMAGSQ(*u); u++; if (val < m_val) { m_val = val; m_idx = idx; } } %% fcnVal: scalar output, minimum value %% fcnIdx: scalar output, index of minimum value %% fcnValandIdx: 2 outputs, minimum value and index %if nChans > 1 %% %if ((fcn == fcnVal) || (fcn == fcnValandIdx)) % = u[m_idx - %]; %endif %% %if ((fcn == fcnValandIdx) || (fcn == fcnIdx)) { /* 2nd output must be the index * Convert C-index to 1-based MATLAB index */ % = (real_T)(m_idx + 1); } %endif } /* nChans loop */ %else %% %if ((fcn == fcnVal) || (fcn == fcnValandIdx)) % = u[m_idx - %]; %endif %% %if ((fcn == fcnValandIdx) || (fcn == fcnIdx)) { /* 2nd output must be the index * Convert C-index to 1-based MATLAB index */ % = (real_T)(m_idx + 1); } %endif %endif %% nChans fill in output loop } %endfunction %% [EOF] sdspmin2.tlc