%% $RCSfile: sdspmean2.tlc,v $ %% $Revision: 1.9 $ %% $Date: 2000/06/15 20:37:44 $ %% %% Copyright 1995-2000 The MathWorks, Inc. %% %% Abstract: Find the mean value of a vector %implements sdspmean2 "C" %% Function: BlockInstanceSetup =============================================== %% %function BlockInstanceSetup(block, system) void %assign INPORT_DATA = 0 %assign INPORT_RESET = 1 %assign OUTPORT_DATA = 0 %% %assign running = CAST("Boolean",(SFcnParamSettings.isRunning == "Yes")) %assign cplx = (LibBlockInputSignalIsComplex(INPORT_DATA) != 0) %assign dType = cplx ? "creal_T" : "real_T" %% %assign inWidth = LibBlockInputSignalWidth(INPORT_DATA) %assign numDims = LibBlockInputSignalNumDimensions(INPORT_DATA) %assign dims = LibBlockInputSignalDimensions(INPORT_DATA) %% %assign isFullMatrix = LibBlockInputSignalIsFullMatrix(INPORT_DATA) %assign framebased = LibBlockInputSignalIsFrameData(INPORT_DATA) %assign nChans = (framebased || isFullMatrix) ? dims[1] : 1 %assign nSamps = (framebased || isFullMatrix) ? dims[0] : inWidth %assign block = block + ... INPORT_DATA + INPORT_RESET + OUTPORT_DATA + ... cplx + dType + inWidth + ... framebased + nChans + nSamps + ... running %endfunction %% BlockInstanceSetup %% Function: InitializeConditions ============================================= %% %function InitializeConditions(block, system) Output %% %if running /* DSP Blockset Mean (%) - % */ %% % = 0; %% %endif %endfunction %% InitializeConditions %% Function: Outputs ========================================================== %% %function Outputs(block, system) Output /* DSP Blockset Mean (%) - % */ %% %if !running % %else %% %% 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 % %else % %endif %if differentPortRates } %endif %endif %endfunction %% Function: Mean_non_running ====================================================== %function Mean_non_running(block) Output %% /* Non-running Mean Output */ %if inWidth==1 %% %% When width is 1, the mean output equals the input %% % = %; %else %% { % *u = %; %% %if nChans > 1 int j; for (j=0; j<%; j++) { %endif %% %if !cplx %% %% REAL %% real_T sum = *u++; int_T i = %; while(i-- > 0) { sum += *u++; } %if nChans > 1 % = sum / %; } %else % = sum / %; %endif %else %% %% COMPLEX %% creal_T sum = {0.0, 0.0}; int_T i = %; while (i-- > 0) { sum.re += u->re; sum.im += (u++)->im; } %if nChans > 1 %.re = sum.re / %; %.im = sum.im / %; } %else %.re = sum.re / %; %.im = sum.im / %; %endif %endif %% if !cplx } %endif %% if inWidth==1 %endfunction %% Mean_non_running %% Function: Mean_running_samp ====================================================== %function Mean_running_samp(block) Output %% %assign resetport = CAST("Boolean",(SFcnParamSettings.isRstPort == "Yes")) %% /* Output sample-based running mean value */ { % *curr_sum = %; uint32_T *iter_cnt = %; %if resetport if(% != 0.0) { /* Reset iteration count. */ *iter_cnt = 0; } %endif %% %% /* If a reset occurs */ if(*iter_cnt == 0) { % \ } (*iter_cnt)++; /* Protect against divide by zeros after increment. */ if (*iter_cnt == 0) { *iter_cnt = 1; % \ } %if !cplx %% %% REAL RUNNING %% %if inWidth == 1 *curr_sum += %; % = *curr_sum / (real_T)*iter_cnt; %else { real_T *u = %; real_T den = 1.0 / (real_T)*iter_cnt; %if inWidth > 1 int_T j; for(j=0; j<%; j++) { %endif %% real_T tsum = *curr_sum; tsum += *u++; *curr_sum++ = tsum; %% %if inWidth > 1 % = tsum * den; } %else % = tsum * den; %endif } %endif %else %% %% COMPLEX Running %% %% %if inWidth == 1 { real_T den = 1.0 / (real_T)*iter_cnt; curr_sum->re += %; curr_sum->im += %; % = curr_sum->re * den; % = curr_sum->im * den; } %else { creal_T *u = %; real_T den = 1.0 / (real_T)*iter_cnt; %% %if inWidth > 1 int_T j; for (j=0;j < %; j++) { %endif %% creal_T tsum = *curr_sum; tsum.re += u->re; tsum.im += (u++)->im; *curr_sum++ = tsum; %% %if inWidth > 1 %.re = tsum.re * den; %.im = tsum.im * den; } %else %.re = tsum.re * den; %.im = tsum.im * den; %endif } %endif %endif %% end complex running } %endfunction %% Mean_running_samp %% Function: Mean_runing_frame ====================================================== %function Mean_running_frame(block) Output %% %assign resetport = CAST("Boolean",(SFcnParamSettings.isRstPort == "Yes")) %% /* Output framebased running mean value */ { % *curr_sum = %; uint32_T *iter_cnt = %; uint32_T iter; %if resetport if(% != 0.0) { /* Reset iteration count. */ *iter_cnt = 0; } %endif %% %% if(*iter_cnt == 0) { % \ } { % *u = %; % *y = %; int_T i; for(i=0; i<%; i++) { % tsum = *curr_sum; int_T j; iter = *iter_cnt; for(j=0; j<%; j++) { /* Protect against divide by zeros after increment. */ if (++iter == 0) { iter = 1; %if !cplx tsum = 0.0; /* reset sum */ %else tsum.re = 0.0; tsum.im = 0.0; %endif } %if !cplx tsum += *u++; *y++ = tsum / (real_T)iter; %else tsum.re += u->re; tsum.im += (u++)->im; { real_T den = 1.0 / (real_T)iter; y->re = tsum.re * den; (y++)->im = tsum.im * den; } %endif } *curr_sum++ = tsum; } } *iter_cnt = iter; /* Save iteration count in DWork */ } %endfunction %% Mean_running_frame %% Function: ClearSum ====================================================== %function ClearSum(block, N) Output %% { /* Clear the current sum. */ %% %if inWidth > 1 int_T j = %; while(j-- > 0) { %if !cplx *curr_sum++ = 0.0; %else curr_sum->re = 0.0; (curr_sum++)->im = 0.0; %endif } curr_sum -= %; /* Reset the pointer */ %else %if !cplx *curr_sum = 0.0; %else curr_sum->re = 0.0; curr_sum->im = 0.0; %endif %endif } %endfunction %% ClearSum %% [EOF] sdspmean2.tlc