muckingfuddle Posted March 20, 2021 Report Share Posted March 20, 2021 Good Morning Guys / Gals I am trying to find a solution for a piece of test equipment that i have built for a client, i am using a SM43 with Analog inputs and outputs and when we built the equipment the client wanted a peak reading, he is now asking for an average reading. The test being carried out is about 15 seconds long and the first 2 seconds are ignored so in essence i need to average a reading from a 0-10v input for 13 seconds and display it on the screen at the end of the test, i am hitting a total blank of how to do this my thoughts were to take a sample every 1/2 second and add together and divide the end result but the amount of code seems huge, is there a simpler way to do it or even a block i am missing, or can i create a block with all this code in. Thanks Quote Link to comment Share on other sites More sharing options...
MVP 2022 Flex727 Posted March 20, 2021 MVP 2022 Report Share Posted March 20, 2021 You're right, averaging can get really complex in a hurry, but the specific task you have seems pretty easy. Try this: 1 Quote Link to comment Share on other sites More sharing options...
vincentBsef Posted March 26, 2021 Report Share Posted March 26, 2021 Is there not a filter FB that could be used for that ? Visilogic juste crashed after that screenshot, so i can not check. But i would try with this. Quote Link to comment Share on other sites More sharing options...
MVP 2022 Ausman Posted March 26, 2021 MVP 2022 Report Share Posted March 26, 2021 Yes there is, but it is limited to the amount of numbers it can work with. In simpler cases I often cascade things. cheers, Aus Quote Link to comment Share on other sites More sharing options...
MVP 2022 Joe Tauser Posted March 28, 2021 MVP 2022 Report Share Posted March 28, 2021 +1 on what Aus says on the Filter limitation. I've had jumpy analog inputs I've had to filter up to 100 values. I wrote a routine that stuffed a small data table that added the incoming value to a total and then stuffed it into the top of a data table after pushing all the other ones down and incrementing a pointer. The pointer was the divisor of the total until it reached the total number of samples that I wanted, and then I used it to point to the row of the data table that had the oldest value and subtract it from the total before dividing it. Overkill? Yes. But very precise overkill with the ability to change the number of samples on the fly. Joe T. Quote Link to comment Share on other sites More sharing options...
MVP 2022 Flex727 Posted March 28, 2021 MVP 2022 Report Share Posted March 28, 2021 I assumed @muckingfuddle wanted an actual average, not a moving average. For a 100 point moving average you could use MIs and indirect addressing. For 1000 points, you probably need to use a Data Table, as Joe suggests above, but for 10,000 points or more you're probably out of luck. At one time I came up with a routine for an approximation of a moving average for any number of points that I've never used, because I don't trust it, but I'm interested in finding out if it might be useful. Simply subtract the average from the sum and add the most recent data point then divide by the number of points to obtain the new average, and repeat. Only 4 registers needed - the most recent data point, the sum, the number of points total, and the average. I've simulated it in an Excel spreadsheet and it appears to follow an actual moving average very closely, but I'm worried that there is a flaw that I'm not seeing that might crop up under unusual circumstances. I think I need a mathematician. Quote Link to comment Share on other sites More sharing options...
muckingfuddle Posted March 28, 2021 Author Report Share Posted March 28, 2021 Hi All, i have tried the solution provided by @Flex727 and the solution works pretty well, i did have to change the MI inputs to ML inputs as the number was overrunning and becoming negative, not really sure if that is the correct way but it did seem to work, one issue i seem to get is that sometimes my analog input seems to go negative and that really screws up my average, is there a way to stop my analog input going negative, i do have a calibration adjustment which i would like to stop at 0 but can potentially go negative. Quote Link to comment Share on other sites More sharing options...
MVP 2022 Flex727 Posted March 29, 2021 MVP 2022 Report Share Posted March 29, 2021 13 hours ago, muckingfuddle said: one issue i seem to get is that sometimes my analog input seems to go negative and that really screws up my average, is there a way to stop my analog input going negative Yes, insert this just above ladder rung 36: Quote Link to comment Share on other sites More sharing options...
MVP 2022 kratmel Posted March 29, 2021 MVP 2022 Report Share Posted March 29, 2021 Hi, I recommend STORE DIRECT "Value" to "Variable" with condition (value>=0). And use "Variable" for calculate AVERAGE. If "Value"<0 then RESET "Variable". In this case it is possible to generate error if "Value" is not correct ("Value"<<0) with no signal on input. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.