%% $RCSfile: sdspstdvar.tlc,v $ %% $Revision: 1.8 $ %% $Date: 2000/06/15 20:37:50 $ %% %% Copyright 1995-2000 The MathWorks, Inc. %% %% Abstract: Find the stdvar value of a vector %implements sdspstdvar "C" %% Function: BlockInstanceSetup =============================================== %% %function BlockInstanceSetup(block, system) void %% % % % % % %% %endfunction %% BlockInstanceSetup %% Function: InitializeConditions ============================================= %% %function InitializeConditions(block, system) Output %% %assign running = LibBlockParameterValue(Running,0) != 0 %% %if running /* DSP Blockset Stdvar (%) - % */ %% % = 0.0; %% %endif %endfunction %% InitializeConditions %% Function: Outputs ========================================================== %% %function Outputs(block, system) Output /* DSP Blockset Stdvar (%) - % */ %% %assign INPORT_DATA = 0 %assign INPORT_RESET = 1 %assign OUTPORT = 0 %% %assign fcn = LibBlockParameterValue(Function,0) %assign FcnVar = 0 %assign FcnStd = 1 %% %assign running = LibBlockParameterValue(Running,0) != 0 %assign cplx = (LibBlockInputSignalIsComplex(INPORT_DATA) != 0) %assign contig = IsInputPortContiguous(block, INPORT_DATA) %assign width = LibDataInputPortWidth(INPORT_DATA) %assign dtype = cplx ? "creal_T" : "real_T" %% %if !running %% %% Not Running %% %if width==1 %% %% When width is 1, the stdvar output equals the input %% % = 0.0; %else %% %if !cplx %% REAL { real_T sx = 0.0; real_T sx2 = 0.0; %assign rollVars = ["U"] %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars %assign u = LibBlockInputSignal(0, "", lcv, sigIdx) sx += %; sx2 += % * %; %endroll %if fcn == FcnVar /* Variance */ % = (sx2 - sx*sx / %) / %; %else /* Standard Deviation */ % = sqrt((sx2 - sx*sx / %) / %); %endif } %else %% COMPLEX { creal_T sx = {0.0, 0.0}; real_T sx2 = 0.0; %assign rollVars = ["U"] %roll sigIdx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars sx.re += %%")>; sx.im += %%")>; sx2 += CMAGSQ(%); %endroll %if fcn == FcnVar /* Variance */ % = (sx2 - CMAGSQ(sx) / %) / %; %else /* Standard Deviation */ % = sqrt((sx2 - CMAGSQ(sx) / %) / %); %endif } %endif %endif %% if width==1 %else %% %% Running %% %assign resetport = LibBlockParameterValue(ResetPort,0) != 0 %assign framebased = LibBlockParameterValue(Framebased,0) != 0 %assign nchans = framebased ? CAST("Number",LibBlockParameterValue(Nchans,0)) : width %assign nsamps = framebased ? width/nchans : 1 %% %if (!contig) % %endif %% { typedef struct { % sum; real_T sqsum; } sv; sv *sv_data = (sv *)%; real_T *iter_cnt = %; %if resetport if(% != 0.0) { /* Reset iteration count. */ *iter_cnt = 0.0; } %endif %% %if !cplx %% %% Real Running %% if(*iter_cnt == 0.0) { %if nsamps == 1 %if nchans !=1 /* Single time step - special case for output value: */ real_T *u = %; real_T *y = %; int_T i = %; while(i-- > 0) { real_T val = *u++; sv_data->sum = val; (sv_data++)->sqsum = val * val; *y++ = 0.0; } %else %assign u = LibBlockInputSignal(INPORT_DATA,"","",0) sv_data->sum = %; sv_data->sqsum = % * %; % = 0.0; %endif (*iter_cnt)++; } else { %else %if nchans!=1 /* Multiple time steps - reset states and continue: */ int_T i = %; while(i-- > 0) { sv_data->sum = 0.0; (sv_data++)->sqsum = 0.0; } sv_data -= %; /* Reset the pointer */ %else sv_data->sum = 0.0; sv_data->sqsum = 0.0; %endif } %endif %% *iter_cnt += %; { %assign yout = (nchans !=1) ? "*y++" : "*y" %assign uptr = (nchans !=1) ? "*u++" : "*u" real_T *u = %; real_T *y = %; real_T iter = *iter_cnt; %if nchans != 1 int_T i = %; while(i-- > 0) { %endif %% %if nsamps !=1 real_T sx = sv_data->sum; real_T sx2 = sv_data->sqsum; int_T j = %; while(j-- > 0) { const real_T val = *u++; sx += val; sx2 += val * val; } /* Update state: */ sv_data->sum = sx; (sv_data++)->sqsum = sx2; %if fcn == FcnVar /* Variance */ % = (sx2 - sx*sx / iter) / (iter-1); %else /* Standard Deviation */ % = sqrt((sx2 - sx*sx / iter) / (iter-1)); %endif %% %else /* Update the state: */ real_T val = %; sv_data->sum += val; sv_data->sqsum += val * val; %if fcn == FcnVar /* Variance */ % = (sv_data->sqsum - sv_data->sum * sv_data->sum / iter) / (iter-1); %else /* Standard Deviation */ % = sqrt((sv_data->sqsum - sv_data->sum * sv_data->sum / iter) / (iter-1)); %endif %% %if nchans !=1 sv_data++; %endif %% %endif %% %if nchans != 1 } %endif } %if nsamps ==1 } %endif %else %% %% COMPLEX Running %% if(*iter_cnt == 0.0) { %if nsamps == 1 %if nchans !=1 /* Single time step - special case for output value: */ creal_T *u = %; real_T *y = %; int_T i = %; while(i-- > 0) { creal_T val = *u++; sv_data->sum = val; (sv_data++)->sqsum = CMAGSQ(val); *y++ = 0.0; } %else %assign u = LibBlockInputSignal(INPORT_DATA,"","",0) sv_data->sum = %; sv_data->sqsum = CMAGSQ(%); % = 0.0; %endif (*iter_cnt)++; } else { %else %if nchans!=1 /* Multiple time steps - reset states and continue: */ int_T i = %; while(i-- > 0) { sv_data->sum.re = 0.0; sv_data->sum.im = 0.0; (sv_data++)->sqsum = 0.0; } sv_data -= %; /* Reset the pointer */ %else sv_data->sum.re = 0.0; sv_data->sum.im = 0.0; sv_data->sqsum = 0.0; %endif } %endif %% *iter_cnt += %; { %assign yout = (nchans !=1) ? "*y++" : "*y" %assign uptr = (nchans !=1) ? "*u++" : "*u" creal_T *u = %; real_T *y = %; real_T iter = *iter_cnt; %if nchans != 1 int_T i = %; while(i-- > 0) { %endif %% %if nsamps !=1 creal_T sx = sv_data->sum; real_T sx2 = sv_data->sqsum; int_T j = %; while(j-- > 0) { const creal_T val = *u++; sx.re += val.re; sx.im += val.im; sx2 += CMAGSQ(val); } /* Update state: */ sv_data->sum = sx; (sv_data++)->sqsum = sx2; %if fcn == FcnVar /* Variance */ % = (sx2 - CMAGSQ(sx) / iter) / (iter-1); %else /* Standard Deviation */ % = sqrt((sx2 - CMAGSQ(sx) / iter) / (iter-1)); %endif %% %else /* Update the state: */ creal_T val = %; sv_data->sum.re += val.re; sv_data->sum.im += val.im; sv_data->sqsum += CMAGSQ(val); %if fcn == FcnVar /* Variance */ % = (sv_data->sqsum - CMAGSQ(sv_data->sum) / iter) / (iter-1); %else /* Standard Deviation */ % = sqrt((sv_data->sqsum - CMAGSQ(sv_data->sum) / iter) / (iter-1)); %endif %% %if nchans !=1 sv_data++; %endif %% %endif %% %if nchans != 1 } %endif } %if nsamps ==1 } %endif %endif %% end complex running } %endif %endfunction %% [EOF] sdspstdvar.tlc