Sign in to follow this  
Followers 0
peterservo

Finding the Median Value in a Dynamic Array

6 posts in this topic

Hello, I have searched this, and other online resources, for a way to do what I am attempting to do, but I have yet to find the solution to my problem. To begin, I am using a CompactLogix platform with Logix5000 software. I have a machine on which an operator can enable or disable the output of a probe using a toggle button on a Panelview. There are a total of 30 probes. If the operator enables a probe, I load it's analog value into an array. I will call the array "array_1" for sake of example. Array_1 has 30 elements and is formatted for the REAL datatype. For example, if the operator enables probe #1, I use a MOV instruction to put it's output into array_1[0]. If Probe 12 is enabled, it's value goes into array_1[11] etc... If a probe is disabled, I use a MOV instruction to move 0.0 into it's array placeholder. In another portion of the program, I use a NEQ instruction to scan each of the elements of array_1 for a non-zero value. If a given element is not equal to zero (it's enabled), then I use another MOV instruction to move that array_1 element into another array( "temp") and I increment a manual counter using an ADD instruction. I use an EQU instruction to scan the array_1 array for zero values. If a probe has been disabled, I use a MOV instruction to move 0.0 into my temp array, and I decrement my counter using a SUB instruction. The purpose of my counter is so that I can keep track of how many active probes there are. I use the value of this counter ("active_probes") as a pointer in my "temp" array. For example, if a probe is enabled, I move it's value into temp[active_probes] and now my active_probes counter=1. If another is activated, I store it into temp[active_probes], which is now equal to 2. This all works well. My problem is in coming up with a Median value for the temp array. In order to determine Median, I need to sort the temp array in ascending order. I have tried using the SRT, and it does work, but it acts like a FIFO. That is, I can't put a zero value into an inactive probe array and thereby disable it. If I have 2 probes active, the use of SRT puts my first active probe into element 29, then the next will shift 29 to 28 etc.. My temp array then contains 0.0 in elements 0-27, then my active probe readings (in ascending order). Sorry for being long winded, but if anyone has a better (working) way of finding the Median value, I would very much appreciate it.

Share this post


Link to post
Share on other sites
Do you modify the 'Length' parameter of the SRT instruction to match the number of items?

Share this post


Link to post
Share on other sites
Thanks for the reply, but unless I'm mistaken, the Length parameter of the SRT instruction won't allow anything but an Immediate value (no variables or expressions).

Share this post


Link to post
Share on other sites
Then go with what you get. Since you know the number of actives you can you can calculate the median index as (array end - 1/2 of count unless the whole even count thing.) I assume you will be copying the median value somewhere else. It really doesn't matter where it was (low end or high end) in the sorted array.

Share this post


Link to post
Share on other sites
I would not mess around with scanning the dynamic array, just copy the whole thing and then sort the whole thing. This takes care of the fixed length SRT/COP problem. If possible to sort descending, do that. Then find your first zero (FAL) offset it one to find one end of the range, add active_probes/2 and you have your median. If it is not a whole number you will need to average the two nearest ones. You can find out if it is a whole numebr quite easily by calculating it twice. Store it once in a REAL and once in a DINT then use EQU to determine that the REAL is a whole number... EDIT: By the way, calculating the number of active probes directly from the bits might be something that can be done simpler too, but you said it is working so I will hold off on that. Edited by OkiePC

Share this post


Link to post
Share on other sites
Yes, after thinking about this again, I have now gotten it to the point where, if the number of active probes is odd, I pick out the middle element of the sorted array. If it's even, I add the 2 "middle" elements of the array and divide by 2 to get the Median. I am still struggling on how to remove un-active probes correctly though. As i mentioned, the SRT seems to treat the array as a FIFO. I am going to try using FFU/FFL to populate the array to see if that helps. Thanks for your help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0