04-18-2012 04:42 AM
I am having a continuous stream of amplitudes which keep changing. I want to make an array out of these values. I have used build array function. The problem is that the first value keeps getting replaced. If the build array block is used then the incoming values should fill in the next vacant slot logically. But, rather than doing that it replaces the first one only. Also, I want only two values in my array.
How can i solve this problem. A example will be helpful. I'm using LabView 2010.
Regards,
Sanjay
04-18-2012 07:42 AM
And since you've shown us no code, we're supposed to tell you the problem how? Perhaps you've wired the Build Array backwards. Perhaps you're using Insert Into Array instead of Build Array.
Your concept also lacks details. You said you have a "continuous stream of amplitudes which keeps changing", but you want an array that has only 2 values. Which 2 values? The last two? The highest 2 seen so far? The highest and the lowest? A random selection of 2?
04-18-2012 07:55 AM
If you only generate one number per iteration in a while loop then the short answer is that you have to store the previous output in a feedback node / shift register, then make sure you have the build array expanded to two inputs (the default state) - and wire the previous output to one of the inputs (the top one if you want the new value to be added to the end of the array).
HOWEVER - building arrays like this is very costly both in terms of memory and speed due to the continous need for memory allocation. The bigger the array grows, the slower and more memory inefficient it will get. It's OK to build the array this way if the array is guaranteed to never grow larger than a few elements (and/or speed and memory is of no importance), but I suggest you make it a rule to avoid it. The same goes for repeated deletion and insertion.
So how should you do it then? Well, there are many ways, but the key is that they limit the need for reallocation. Auto-indexing data exiting a for-loop is one of the easiest and most efficient ways when applicable. LabVIEW will then preallocate an array of the required size for you, and put the data into that array when they are available.
If the for loop with auto-indexing is not applicable then you can preallocate the full array yourself and use the replace array element function to put the data into the array. You allocate the array by using the initialize node, or - if the final size is unknown - resize the array in chunks. In some cases you might know the maximum size, but not how much of it you will actually end up using. The most efficient then might be to allocate an array of the maximum size, fill in the data you get while keeping track of how many elements you fill, then resize the array afterwards to the number of elements you ended up with.
For actual examples and more info about this you can read the following thread, or search ni.com for phrases like memory management, optimization etc.
http://forums.ni.com/t5/LabVIEW/how-do-i-create-arrays-more-efficiently/td-p/1146001
04-18-2012 08:08 AM
@Mads wrote:
HOWEVER - building arrays like this is very costly both in terms of memory and speed due to the continous need for memory allocation. The bigger the array grows, the slower and more memory inefficient it will get. It's OK to build the array this way if the array is guaranteed to never grow larger than a few elements (and/or speed and memory is of no importance), but I suggest you make it a rule to avoid it. The same goes for repeated deletion and insertion
True in general. But I don't think we have to worry about needing thousands of petabytes of memory for an array of just 2 elements.
04-18-2012 08:36 AM
No, but I'm not sure I get the joke/comment.
Is the reference to an array of just two elements a reference from the rude goldberg thread (I skipped reading it all), or from the original question? Or is it about the unclarity of the phrase "previous output" (which only implicitly refers to the previously built array, i.e. the output of the build function)?
04-18-2012 09:44 AM
In the original post the statement was made: "Also, I want only two values in my array."
04-18-2012 01:38 PM
Ah, missed that one. But I guess he'll soon enough need to tackle bigger arrays