- Take the current value of signal and current time as input
- Detect rising/falling edge occurred and maintain the time when last rising edge and last falling edge appeared
- Difference between the time of last rising edge and last falling edge will give you the on time
- Difference between the time of newest rising edge and previous riding edge will give the period
- (on time/period) * 100 will give the percentage of on time in the PWM cycle
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.

0 个评论
回答(1 个)
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,
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!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!