This example shows how to create a rectangular pulse waveform and measure the duty cycle. You can think of a rectangular pulse waveform as a sequence of on and off states. One pulse period is the total duration of an on and off state. The pulse width is the duration of the on state. The duty cycle is the ratio of the pulse width to the pulse period. The duty cycle for a rectangular pulse describes the fraction of time that the pulse is on in one pulse period.
Create a rectangular pulse sampled at 1 gigahertz. The pulse is on, or equal to 1, for a duration of 1 microsecond. The pulse if off, or equal to 0, for a duration of 3 microseconds. The pulse period is 4 microseconds. Measure the duty cycle of the waveform.
Fs = 1e9; t = 0:1/Fs:(10*4e-6); pulsewidth = 1e-6; pulseperiods = [0:10]*4e-6; x = pulstran(t,pulseperiods,@rectpuls,pulsewidth); plot(t.*1e6,x); axis([0 40 -0.5 1.5]); xlabel('\mus'); ylabel('Amplitude');
Determine the duty cycle using dutycycle. Input both the pulse waveform and the sampling frequency to output the duty cycle. dutycycle outputs a duty cycle value for each detected pulse.
D = dutycycle(x,Fs)
In this example, the duty cycle for each of the 10 detected pulses is identical and equal to 0.25. This is the expected duty cycle because the pulse is on for 1 microsecond and off for 3 microseconds in each 4 microsecond period. Therefore, the pulse is on for 1/4 of each period. Expressed as a percentage, this is equal to a duty cycle of 25%.
Calling dutycycle with no output arguments produces a plot with all the detected pulse widths marked.
Using the same sampling rate and pulse period, vary the pulse on time (pulse width) from 1 to 3 microseconds in a loop and calculate the duty cycle. Plot the pulse waveforms and display the duty cycle value in the plot title for each step through the loop. You see the duty cycle increase from 0.25 (1/4) to 0.75 (3/4) as the pulse width increases.
pulsewidths = 1e-6:1e-6:3e-6; for nn = 1:length(pulsewidths) x = pulstran(t,pulseperiods,@rectpuls,pulsewidths(nn)); plot(t.*1e6,x); axis([0 40 -0.5 1.5]); xlabel('\mus'); ylabel('Amplitude'); D = dutycycle(x,Fs); title(['Duty cycle is ' num2str(mean(D)) ]); pause(1); end