LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate Square wave Duty Cycle

Solved!
Go to solution

Hello everyone,

I'm acquiring (with USB-6212) a square wave, "100Hz" with variable duty cycle - which can be 20%, 50% or 80% - for matters of decodification; the acquisition frequency is set to 4k. Actually, I said the frequency is 100Hz, because I get a rising edge every 10ms, but the length of the high level is defined by the communication protocol and that is what I called duty cycle (even though this does not match the exact definition of this property)

Since I need to decode the information contained by the wave, it's necessary for me to be able to tell what the duty cycle is. I've acomplished this so far by taking the derivative of the signal, then calculating peaks and valleys index in the waveform and finally subtracting them, as shown below.

Code.PNG

 

It works ok almost the whole opeartion, however, the acquisition is not 100% exact. Sometimes I get the signal in the picture A, that is ideal for what I intended.

Other times, the signal is like B and the derivative amplitude does not cross my threshold, even though there is a rising edge there. I could lower my threshold to 2, for instance, but then I'd get vulnerable to the signal in picture C, which would accuse 2 peaks, instead of just one.

SW_ok.png

SW_belowThreshold.png

SW_overThreshold.png

Anyone have an idea on how to work around this or any other method more reliable than taking the derivative of the signal?


0 Kudos
Message 1 of 10
(7,678 Views)

It looks like your signal is fairly clean (low noise).  I would just set the threshold to 2.5 and use the Greater Than comparison.  The percentage of True in the resulting boolean array is the duty cycle.

 

Lynn

 

Duty cycle.png

Message 2 of 10
(7,665 Views)

Hello there, thank you for your reply.

 

I reckon that the picture may be misleading, given that only one cycle is shown. However, my acquisited signal is composed by one hundred of cycles, thus I need to calculate 100 different duty cycles.

This way, the sum of high levels would give the average duty cycle and not the duty cycle of each and every "square".

 

You may suggest that I get just one cycle at a time (10ms acquisition), but that would hit my performance spec, given that I would have to calculate more often. Summing up, I'm trying to find a method that works with big loads of data, more than 2k points at least

0 Kudos
Message 3 of 10
(7,644 Views)

Hmmmm Duty Cycle on a cycle by cycle basis.

Time to break out the Waveform measurements pallate

Try :

DC.png

Or if the signal is a bit noisy- add some histeresis by changing out the Ref Units

DC w Hist.png


"Should be" isn't "Is" -Jay
Message 4 of 10
(7,630 Views)

Well, we can only answer question based on the information supplied!  The LabVIEW Mind Reading Toolkit is still in development.

 

Your application is one where parallel loops may be valuable.  In the acquisition loop you read data from the data acquistion device at a rate which optimizes the DAQ perfomance (the >= 2k points you mentioned).  The data is transferred to an analysis loop via a queue. In the anaylsis loop you can put the data into a shift register.  When more data is available from the Acquisition loop, dequeue it and append it to the data in the shift register.  The Duty Cycle section will find two consecutive rising edges and the falling edge between them ( the 10 ms segment).  It calculates the duty cycle for that segment. Then it uses the second of the rising edges as the first rising edge for the next segment.  Repeat until duty cycles have been determined for all segments.

 

There are a lot of details which can be imprortant in a process like this. Many of them are dependent on other things you have not specified.  What will be done with the duty cycle values? Is the raw data to be displayed? Saved to file? Discarded after the duty cycle is measured?  How long will the process continue?  How much data can accumulate?

 

Lynn

0 Kudos
Message 5 of 10
(7,629 Views)

Or, do it Jeff's way.

 

Lynn

Message 6 of 10
(7,627 Views)

@johnsold wrote:

Or, do it Jeff's way.

 

Lynn


Well-  I do have a magic 8-ball....


"Should be" isn't "Is" -Jay
0 Kudos
Message 7 of 10
(7,616 Views)

@Jeff Bohrer wrote:

@johnsold wrote:

Or, do it Jeff's way.

 

Lynn


Well-  I do have a magic 8-ball....


Can you put it into a sub-vi for the rest of us?

Message 8 of 10
(7,613 Views)
Solution
Accepted by topic author Edmi0

@Hornless.Rhino wrote:

@Jeff Bohrer wrote:

@johnsold wrote:

Or, do it Jeff's way.

 

Lynn


Well-  I do have a magic 8-ball....


Can you put it into a sub-vi for the rest of us?


Sure
You Asked!.pngUntitled.png
Smiley Surprised

"Should be" isn't "Is" -Jay
0 Kudos
Message 9 of 10
(7,608 Views)

Thanks for the quick reply, that was really helpful.

 

Awesome "mind reading" skills

0 Kudos
Message 10 of 10
(7,579 Views)