西门子SCL的平均值算法
前 言:
//---- INPUTS I_RAW_VALUE Real Raw Value to be averaged I_NEW_SAMPLE_PULSE Bool New Value One-Shot Pulse I_RESET Bool Reset MAV Buffer to 0 //---- OUTPUTS Q_MAV Real Output //---- STATIC BUFFER Array[1..#CONST_MAV_PERIOD] of Real Buffer SUM Real Sum for Average i Int Pointer to Array FULL Bool Buffer Full //---- CODE IF #I_RESET THEN // Reset Buffer FOR #i:=1 TO #CONST_MAV_PERIOD DO #BUFFER[#i] := 0; END_FOR; #i := 1; #FULL := False; // Reset Full Pulse END_IF; IF #i < 1 OR #i > #CONST_MAV_PERIOD THEN // Test Index and reset to 0 if out of range #i := 0; END_IF; IF #I_NEW_SAMPLE_PULSE THEN // New Value? #SUM := #SUM + #I_RAW_VALUE - #BUFFER[#i]; // Update Sum - add newest // element and take out oldest // one from the buffer #BUFFER[#i] := #I_RAW_VALUE; // Store new raw value in // the buffer #Q_MAV := #SUM / #CONST_MAV_PERIOD; // Calculate Moving Average IF #i = #CONST_MAV_PERIOD THEN // Buffer Full #FULL := True; END_IF; i:=i+1; // Update Pointer END_IF; ENO := #FULL; // ENO when buffer full