From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Square Wave with Randomized Duty Cycle

Hi! LabVIEW Beginner here.

I was wondering if there was a way to create a square wave with a randomized duty cycle within a range, such that it completes an entire cycle before changing its duty cycle to another random percentage in the range. It must also move across the waveform chart or graph slow enough so that the user could follow the square wave with a joystick. My current attempt moves at around the speed I want but the wave changes very abruptly and always stays up for a while before it starts changing abruptly and then down for a while.RandomSquareWaveVI.PNG

0 Kudos
Message 1 of 3
(2,336 Views)

Your issue is that the square wave generator is generating the square wave piecemeal. You want to generate one cycle at a time. With a frequency of 0.075 Hz, and the default Sampling Info (1000 pts at 1000 Hz) you're not generating a full period. Change either your sampling info or your frequency to match (to generate a full period of data), and set the Reset? input to True so you get a fresh start each time.

0 Kudos
Message 2 of 3
(2,311 Views)

@DotsonLCCF wrote:

Hi! LabVIEW Beginner here.


Hello, LabVIEW Beginner.  You are making a common mistake of not first thinking about what you want to do, and instead jump into LabVIEW to start coding how you are going to do it.  So let's ask what do you want to do?  What do you mean by a "square wave with a randomized duty cycle"?  I can think of several things this might mean:

  • A 1 Hz Square Wave whose Duty Cycle varies "randomly" (how?  Uniform?  Gaussian?).  An example could be first cycle at 50%, next cycle at 22%, next cycle at 69%, ... [On 0.5, off 0.5, on 0.22, off 0.78, on 0.69, off 0.31 ...].
  • A flip-flop whose "time between switching" varies "randomly" (how?).  If the dwell time is U(0, 1), an example could be On 0.32, off 0.58, on 0.49, off 0.27, on 0.84, off 0.75, ...
  • A "combination of ingredients" where you vary the square wave frequency according to some rule and the duty cycle according to some rule.

As you initially phrased it, the first method sounds most like what you said, but might not be what you really want.  But in any case, what do you end up with?  A series of times, and a rule for what to do when the time runs out (namely "flip" the flip-flop).

 

Now, think LabVIEW.  I see three basic things happening here -- something happens repetetively, time is involved, and there is the concept of a flip-flop, something that has two values and changes from one to the other.

  • How do you do something repetetively in LabVIEW?
  • How do you handle time (particularly variable time) in LabVIEW?
  • How do you handle a "variable" (as it would be called in other Programming environments) in LabVIEW?  Hint -- the correct answer does not have the word "variable" in its name, nor does it have the words "control" or "indicator".

Putting these things together (with some "rules" to handle the "variability" questions raised above) should lead you to a simple solution.

 

Bob Schor

0 Kudos
Message 3 of 3
(2,287 Views)