%% $RCSfile: sdsprms2.tlc,v $ %% $Revision: 1.5 $ %% $Date: 2000/06/15 20:37:48 $ %% %% Copyright 1995-2000 The MathWorks, Inc. %% %% Abstract: Find the rms value of a vector %implements sdsprms2 "C" %% Function: BlockInstanceSetup ================================================== %% %function BlockInstanceSetup(block, system) void %% %assign INPORT_DATA = 0 %assign INPORT_RESET = 1 %assign OUTPORT_DATA = 0 %% %assign cplx = (LibBlockInputSignalIsComplex(INPORT_DATA) != 0) %assign running = CAST("Boolean",(SFcnParamSettings.isRunning == "Yes")) %assign frameBased = LibBlockInputSignalIsFrameData(INPORT_DATA) %assign dType = cplx ? "creal_T" : "real_T" %assign inWidth = LibBlockInputSignalWidth(INPORT_DATA) %assign dims = LibBlockInputSignalDimensions(INPORT_DATA) %assign numDims = LibBlockInputSignalNumDimensions(INPORT_DATA) %assign isFullMatrix = ((numDims == 2) && (dims[0] != 1) && (dims[1] != 1)) %assign isRowVector = ((numDims == 2) && (dims[0] == 1)) %% %assign block = block + INPORT_DATA + INPORT_RESET + OUTPORT_DATA %assign block = block + cplx + running + frameBased + dType %assign block = block + inWidth + dims + numDims + isFullMatrix + isRowVector %% %endfunction %% BlockInstanceSetup %% Function: InitializeConditions ============================================= %% %function InitializeConditions(block, system) Output %if running /* DSP Blockset RMS (%) - % */ { % = 1; } %endif %endfunction %% InitializeConditions %% Function: Outputs ========================================================== %% %function Outputs(block, system) Output %if (!running) % %else % %endif %% (!running) %% %endfunction %% Outputs %% Function: GenerateNonRunningRMSCode ======================================== %% %function GenerateNonRunningRMSCode(block,system) Output /* DSP Blockset non-running RMS (%) - % */ { %if (inWidth==1) /* Scalar input (degenerate case) */ %assign y = LibBlockOutputSignal(OUTPORT_DATA, "", "", 0) %assign u = LibBlockInputSignal(INPORT_DATA, "", "", 0) %if !cplx % = fabs(%); %else CABS(%, %); %endif %else %if (isFullMatrix || (isRowVector && frameBased)) %% Treat input as frame-based (multi-channel), %% operate down each column separately %assign nSamps = dims[0] %assign nChans = dims[1] %else %% Collapse output to single scalar RMS value %% (single-channel operation) %assign nSamps = inWidth %assign nChans = 1 %endif %% %% Non-trivial non-running RMS code %% % *u = %; %% %if (nChans > 1) int j; for (j=0; j < %; j++) { %endif %% (nChans > 1) real_T sx2 = 0.0; int_T i = %; while(i-- > 0) { % uval = *u++; %% %if !cplx sx2 += (uval * uval); %else sx2 += CMAGSQ(uval); %endif } %if (nChans > 1) %if (nSamps > 1) % = sqrt(sx2 / %); %else % = sqrt(sx2); %endif } %else %if (nSamps > 1) % = sqrt(sx2 / %); %else % = sqrt(sx2); %endif %endif %% (nChans > 1) %endif %% if (inWidth==1) } %endfunction %% GenerateNonRunningRMSCode %% Function: GenerateRunningRMSCode =========================================== %% %function GenerateRunningRMSCode(block,system) Output /* DSP Blockset running RMS (%) - % */ { %% %% 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 %% %assign resetport = CAST("Boolean",(SFcnParamSettings.isRstPort == "Yes")) %% %% Number of independent channels and %% number of samples per channel/frame %% %assign nChans = (frameBased) ? dims[1] : inWidth %assign nSamps = (frameBased) ? dims[0] : 1 %% real_T *msqsum = %; uint32_T *iter_cnt = %; %if resetport /* Reset iteration count if necessary */ if(% != 0.0) *iter_cnt = 1; %endif if(*iter_cnt == 1) { /* First time or just reset */ % } else { /* Check increment with overflow handling before algorithm */ uint32_T old_iter_cnt = *iter_cnt; if (old_iter_cnt >= (*iter_cnt + %)) { % *iter_cnt = 1; /* restart counter */ } } %if (inWidth == 1) /* Scalar input and output */ %assign u = LibBlockInputSignal(INPORT_DATA,"","", 0) %if (!cplx) *msqsum += (% * %); %else *msqsum += CMAGSQ(%); %endif %% (!cplx) %% % = sqrt(*msqsum / ((real_T)(*iter_cnt))); %% %else { % *u = %; real_T *y = %; %% %% Need to calculate denominator on-the-fly %% for more than one sample-per-frame. However, %% for the trivial case of one sample-per-frame, %% do it here once (to reduce number of divides). %% %if (nSamps == 1) real_T den = 1.0 / ((real_T)(*iter_cnt)); %endif %% %if (nChans > 1) int_T i = %; while(i-- > 0) { %endif %% %if (nSamps > 1) %% Frame-size greater than one int_T j; for(j=0; j<%; j++) { const % val = *u++; %if !cplx *msqsum += (val * val); %else *msqsum += CMAGSQ(val); %endif *y++ = sqrt(*msqsum / ((real_T)(*iter_cnt + j))); } %else %% One sample-per-frame -> easy case % val = *u++; %if !cplx *msqsum += (val * val); %else *msqsum += CMAGSQ(val); %endif *y++ = sqrt((*msqsum) * den); %endif %% (nSamps > 1) %if (nChans > 1) msqsum++; } %endif %% (nChans > 1) } %endif %% (inWidth == 1) *iter_cnt += %; %if differentPortRates } %endif } %endfunction %% GenerateRunningRMSCode %% Function: GenerateRunningResetMsqCode =========================================== %% %function GenerateRunningResetMsqCode(block,system) Output %assign nChans = (frameBased) ? dims[1] : inWidth %if (nChans > 1) int_T i = %; while(i-- > 0) { *msqsum++ = 0.0; /* clear current sum */ } msqsum -= %; /* reset pointer */ %else *msqsum = 0.0; /* clear current sum */ %endif %% (nChans > 1) %endfunction %% GenerateRunningResetMsqCode %% [EOF] sdsprms2.tlc