Analysing PWM duty cycle of a signal

I have recorded a PWM signal from a given device.
I require to determine the duty cycle(s) of the signal as the transitions from one state to another.
How may I output the duty cycle of the PWM thoughout the signal?
I am using Matlab 2016b

Respuestas (1)

David Hill
David Hill el 20 de Mzo. de 2023
s=pwm>threshold;%pwm is your signal, threshold is when it is considered on or off
d=diff(s);
idx1=find(s,1);
idx2=find(d==-1);
idx3=find(d==1);
num=idx2-[idx1-1,idx3(1:end-1)];
denom=diff([idx1-1,idx3]);
dutyCycle=num./denom;

3 comentarios

thank you for the snippet.
I tried the followin
s=CH01_01>0.75;%Ch01_01 is a 200x200x1 single matrix
d=diff(s);
idx1=find(s,1);
idx2=find(d==-1);
idx3=find(d==1);
num=idx2-[idx1-1,idx3(1:end-1)];
denom=diff([idx1-1,idx3]);
dutyCycle=num./denom;
but I'm getting the following error:
Error using horzcat
Dimensions of matrices being concatenated are not consistent. 
Error in plot1 (line 8)
num=idx2-[idx1-1,idx3(1:end-1)];
could you please advise on how to resolve this issue?
David Hill
David Hill el 21 de Mzo. de 2023
Editada: David Hill el 21 de Mzo. de 2023
I assumed your data was a horizontal array. For a better response, attach your data.
s=CH01_01(:)';%convert to horizontal array
s=s>.75;
Sherzaad Dinah
Sherzaad Dinah el 22 de Mzo. de 2023
Editada: Sherzaad Dinah el 24 de Mzo. de 2023
Thank you for your help,
After making the change, I no longer have any error.
However all the calculated duty cycles are greater than 1?!
Is there something else that needs to be done with the data please?
I have attached the data as requested. Kindly please advise.
s = CH01_01(:)';
s=s>0.75;%pwm is your signal, threshold is when it is considered on or off
d=diff(s);
idx1=find(s,1);
idx2=find(d==-1);
idx3=find(d==1);
num=idx2-[idx1-1,idx3(1:end-1)];
denom=diff([idx1-1,idx3]);
dutyCycle=num./denom

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink en Centro de ayuda y File Exchange.

Productos

Versión

R2016b

Etiquetas

Preguntada:

el 20 de Mzo. de 2023

Editada:

el 24 de Mzo. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by