I wish to calculate the duty ratio from a PWM waveform

1 次查看(过去 30 天)
Hi,
I wish to find the on time an off time from a PWM to calculate its duty ratio at the run time.
I tried with the following model but it does not give me the time aray in sec.
can someone please guide me how can I find an array of time (final and initial vale) so that I subtract the two values and find the On time.
Thanks.

回答(1 个)

Anay
Anay 2025-4-30
Hi Waqar,
You can write a simple MATLAB function to calculate the pulse width of PWM signal and then use it in Simulink using a MATLAB function block.
You can use the Clock block to give current time as input to your function. Second input will be your PWM signal (you can use a Pulse Generator block for test).
The function should,
  1. Take the current value of signal and current time as input
  2. Detect rising/falling edge occurred and maintain the time when last rising edge and last falling edge appeared
  3. Difference between the time of last rising edge and last falling edge will give you the on time
  4. Difference between the time of newest rising edge and previous riding edge will give the period
  5. (on time/period) * 100 will give the percentage of on time in the PWM cycle
Note: Make sure to use persistent variables for storing values like the time when rising/falling edge occurs or time when previous rising edge occurred in the MATLAB function.
I hope this solves the issue!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by