Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a component for EMC2 HAL
- // Copyright 2006 Jeff Epler <jepler@unpythonic.net>
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- component rollingavg "calculate simple rolling average of up to 200 samples";
- pin in float in;
- pin out float out;
- pin in unsigned depth=1;
- variable double avgarray[199];
- variable int placeholder;
- function _;
- license "GPL";
- ;;
- FUNCTION(_) {
- double avgtotal = 0 ;
- int i = 0;
- int idepth = 0;
- idepth=depth;
- if (idepth > 200) idepth=200;
- avgarray[placeholder++]=in;
- if (placeholder >= idepth) placeholder=0;
- for(i=0;i<idepth;i++){
- avgtotal=avgtotal + avgarray[i];
- }
- if (idepth!=0) out = avgtotal/idepth;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement