02-13-2018 10:33 PM
Hi,
I need to design a program that displays a random number between 0 and 1 once every second. Then it will compute and display the average of the last four random numbers generated. Display the average only after four numbers have been generated; otherwise display a 0. Each time the random number exceeds 0.5, let a LED turn on.
Please help me. Or a flowchart of things i need to follow would be really appreciated
this is what i did so far and its not right 😞
Solved! Go to Solution.
02-13-2018 11:32 PM
In order to average the last four numbers, you could keep the last four numbers in an array of size 4 in the shift register, always replacing the oldest element and averaging the current elements once at least four elements have been added (ask the iteration terminal ;)). This should get you started. There is also mean-ptbypt, but I think your teacher wants you to code it from scratch. Adding the comparison and LED seems trivial. Don't use a FOR loop. Everything need to happen in a while loop that runs forever or until the program is terminated.
02-14-2018 08:48 AM
02-14-2018 09:24 AM
@paul_cardinale wrote:
Using an expanded shift register is a horrible kludge, because it is not really scalable and makes you rewrite the code basically from scratch whenever you want to change N. My suggested solution is much more scalable, because changing the averaging length only requires changing a single diagram constant. Of course "mean ptbypt" does the same thing as you can see by the comparison here. So all you would use is the lower part.
Currently, it always outputs an average but It would be trivial to modify it so it outputs zero based on [i] and N, try it. Now you don't even need the min&max function. 😄
For even fancier methods, e.g. when using very large history averages, you can avoid certain computations to save CPU. For example instead of summing all data with each iteration, it would be sufficient to also keep the sum in a scalar shift register and, with each iteration, subtract the oldest and add the newest value. Have a look at our 2016 NI week presentation.
02-14-2018 10:03 AM
Thank you Sir. It was extremely helpful 🙂
I figured out how to average zero till the 4th iteration but connecting a comparing tool to the iteration counter.
This is my first labview class. and we did not go over array and other tools you have mentioned such as ptbypt (Which i couldn't find by going into the probability palette :D, let me know where it is located if possible :D)
02-14-2018 10:09 AM
This is great 🙂
Thank you so much. As it is my first labview class, it is very simple and easy to understand so early in the semester 😄
Quick question though,
if i understand the function of shift register correctly, it carries the value of the first iteration and gives it to the next
So if the follow the wire connection, at first random number generator creates a number, it goes into the compound arithmetic function and takes the first of the 4 places, and also goes to the 2nd iteration thru the shift register which is connected to the 2nd place of the compound arithmetic function. So aren't we using the same number twice.
It is probably a very dumb question and i am sorry to bother you experts with such nonsense.
I really apologize for that. I am trying to understand and not just copy it 😄
02-14-2018 10:15 AM
Sir,
I am sure this will work. But as i am very new to labview, there are few functions that you have used is something i am not fimiliar with. I am trying to read the book, using help function to get myself familiarized with those. And after that i will try this way as well. Thank you very much 🙂
02-14-2018 10:16 AM
@Alyssa001 wrote:
So aren't we using the same number twice.
Each number will be part of four different running averages, so yes, each number is used four times in a row.
02-14-2018 10:25 AM
Here's what I would do. It is probably more reasonable to output NaN until four values have been acquired instead of outputting zero. It simplifies the code and once in a gazillion years, zero (withing five significant digits) could be a valid average. 😄
02-14-2018 02:35 PM
After seeing your code, I remembered something I came up with a long time ago, See attachment.