LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to sum consecutive results

I am new to labview and I am trying to sum results that are output from a certain math function. This is what I am trying to do in detail:
 
I have a while loop that divides a number by two, then checks the remainder to see if it zero. If it is not, it will continue to divide the remainder resutling from the first, second...division by two until the a zero is detected. If a zero is detected, the loop will be stopped. During this whole process The quotient resulting from each division is multiplied by 2^(i), where (i) is each loop iteration. Here is where I am confused- the result of the multiplication is then fed into a build array and the array is added by the add array element function. However, this doesn't work.
 
I would like to sum each value resulting from the multiplication for each loop iteration. I have attached the labview file here. Please let me know what I'm doing wrong.
 
Thanks.
[]~.~[]
0 Kudos
Message 1 of 11
(6,634 Views)
Not sure what you hope to accomplish here, Since your remainder will always be a 1 until you find an even number.  But to answer your question, you need to save your array on each iteration.   See attached.

Message Edited by Stradis on 06-05-2007 01:11 PM


Paul
0 Kudos
Message 2 of 11
(6,623 Views)
Thanks Paul for your quick reply. I worked on the SumVi a little bit more...using a shift register, etc. (Very helpful...thanks). It almost does what I want to do except one thing.  Here is the main idea of what I'm trying to do:
 
Basically its a decimal to binary conversion with a computation to get a mantissa representation:-
 
(for example)
 
12/2 = 6  -->0  (six is fed back into the division)
 
6/2= 3    -->0 (three is fed back)
 
3/2= 1.5 -->1 (One is fed back)
 
1/2=0.5 ---> 1  Loop should stop here as soon as it detects the 0 in 0.5
 
Binary equivalent is 1100
 
The 'remainder & quotient' function rounds off the values which is exactly what I want. Now I want to take the values the ones the shift register arrayed and sum them up. Now this sum should not include the last computation that resulted in a 0 or 0.5. For this particular example the sum should yield (1x2^2)+ (0x2^1)+(0x2^0)=4 not 12 (The first '1' in the 1100 is ignored). Its giving me 12 instead of 4, do I need to fudge with the while loop??
 
I appologize if the block diagram looks messy...
 
Thank you in advance.
0 Kudos
Message 3 of 11
(6,610 Views)
I got it...hurray!!! I just had the variable outside the loop and it was doing something funky...but it works now.
 
See attached and please give me feedbacks on this.
 
 
Merci
0 Kudos
Message 4 of 11
(6,601 Views)
This seems like a silly problem, because it will just report the input back if the number is positive. If the input is negative, the loop will never stop and you lock up the code. There has to be more to it!
 
In any case, you don't need to form an array if you are only interested in the sum. SImply initialize another shift register with zero and add the values as they occur. (see image)
 
Can you take two steps back and tell us the purpose of the code. Maybe there is a much easier way. Also, are you sure the calculation should be in floating point. Maybe you are only interested in integer calculations?
 
Here is a quick draft. You need to insure that the input is positive. Else you need to do some exception handling in the code.
 
 
 
 

Message Edited by altenbach on 06-05-2007 11:32 AM

Download All
0 Kudos
Message 5 of 11
(6,597 Views)


@hotmail wrote:
See attached and please give me feedbacks on this.

This code is very silly. There is no reason in the world to use a local variable here, because you will end up with a race condition. Most likely, the local variable read will occur way before the upper loop has finished, and you'll get the sum of some unpredictable  random intermediary result.

It is also useless to place the indicators for quotient and remaider outside the loop. You won't see any variation. E.g the "remainder" indicator only receives data after the loop has finished, and since the loop does not finish until the remainder is zero, you'll get zero in 100% of the cases. 😉

Since you have a 1 second delay (maybe for debugging?) All indicators belong inside the loop. Once the code is debugged, all these indicators and the delay can be removed.

Also, you initialize your array with the wrong representation. Don't mix orange and blue for no good reason. RIght-click the array constant and to "representation...DBL".


 

Message Edited by altenbach on 06-05-2007 11:44 AM

0 Kudos
Message 6 of 11
(6,592 Views)
ahhh, passion lives!  I agree, the first example I provided back did eliminate the cohersions.  And I did question the purpose, but my assumption, as he said he is new at this, was just a little... See if I can do it project.

Paul
0 Kudos
Message 7 of 11
(6,580 Views)


@hotmail wrote:
The 'remainder & quotient' function rounds off the values which is exactly what I want. Now I want to take the values the ones the shift register arrayed and sum them up. Now this sum should not include the last computation that resulted in a 0 or 0.5. For this particular example the sum should yield (1x2^2)+ (0x2^1)+(0x2^0)=4 not 12 (The first '1' in the 1100 is ignored). Its giving me 12 instead of 4, do I need to fudge with the while loop??

So, what result do you actually want? Why all that song and dance if all you want is an unsigned integer that has the highest set bit zeroed out?? 🙂

Since boolean operations work bitwise on integers, maybe the following is a possible solution? Basically, it finds the highest set bit and zeroes it, keeping the rest intact. Please modify to your exact requirements (e.g. integer representation, currently U8). There are may other ways to do this, but this should getr you going.

An input of 12 will result in an output of 4.

Message Edited by altenbach on 06-05-2007 12:56 PM

Download All
0 Kudos
Message 8 of 11
(6,572 Views)

Thank you all for your help and feedback. Yes, I am new to Labview and yes there are many ways you can do what I am trying to do and maybe the way I'm doing it is silly...but... part of the learning process 'ey.

Okay...maybe I should have explained the whole purpose of why I wanted to sum up consecutive results. The whole purpose is to manually convert numbers to single precision floating points (http://en.wikipedia.org/wiki/Single_precision). See the finalized vi that I have attached. Well, not quite finalized because it only converted integers (-ve and +ve) and it doesnt do fraction. I was able to create a vi to do fractions but I cannot post it at this time.

The VI attached does the conversion in two different ways. One is using the built in Single Precision conversion funciton and the other is by step by step manual calculation. You may notice the conversions for the 0 and 1 are different for each type of conversion. This is because of the calculation methodoloy used.

Alntenback- your method is simple and straight forward...thanks. Smiley Happy

Thanks

0 Kudos
Message 9 of 11
(6,536 Views)
Well, to convert a number to SGL, you use the "toSGL" operation from the conversion palette.
 
If you want to see the bits of the SGL in hex representation, you can typecast it to U32 and set the display to %08x. See addition in upper left (image).
 
You code is still full of race conditions. You absolutely need to wait with the lower code until the loop has finished. RIght now, these disconnected segments will execute well before the local variables contain updated data. A big drawback of using local variables for no reason. Just wire these parts directly.
 
If you create data dependency such that these operations wait until the loop has finished, the results are more resonable. Why don't you run your code in execution highlighting mode to see the problem in slowmotion? 😉
 
None of these local variables are needed. Just wire everything up in sequence. The wire is the variable! 🙂
 
You should also get rid of all these coercions.

Message Edited by altenbach on 06-08-2007 10:07 AM

Download All
0 Kudos
Message 10 of 11
(6,532 Views)