fitting with a rectangular pulse function
Mostrar comentarios más antiguos
Hi, I would like to fit a signal pulse with a rectangular pulse function. this is my script, a rectangular pulse signal should be fitted with the same function:
%%%%%%%%%%
clear all
xdata=[1:1000];
a=100;
b=30;
c=12;
ydata=squarepulse(xdata,a,b,c);
plot(xdata,ydata)
par0=[ 100
2
7];
lb=[xdata(1)
1
5];
ub=[xdata(end)
50
20];
options = optimoptions('lsqcurvefit','tolx',1e-10,'tolfun',1e-10,'maxfunevals',5000000)
parOut=lsqcurvefit(@squarefun,par0,xdata,ydata,lb,ub,options)
yfit=squarefun(parOut,xdata);
plot(xdata,ydata,xdata,yfit,'r')
%%%%%%%%
and this is the script for build the rectangular pulse signal and used to fit:
%%%%%%%%%%%%%
function y = squarefun(par,xx)
y=xx*0;
%y=par(3)*exp(-(xx-par(1)).^2/par(2)^2);
for i=1:length(xx)
if xx(i)<=par(1)
y(i)=heaviside(xx(i)-par(1)+par(2))*par(3);
else
y(i)=heaviside(-xx(i)+par(1)+par(2))*par(3);
end
end
end
%%%%%%%%%%%%
the problem is that the parameters par(1) and par(2) (x position of the signal and its width) do not change, only the par(3) is correctly found. If I substitute the rectangular function with a gaussian the minimisation works good. Thank you regards Andrea
4 comentarios
Jim Joy
el 31 de Ag. de 2017
Hi Andrea,
Could you please provide the function 'squarepulse' that you are using to generate y? This does not appear to be a MATLAB built-in, and it would be helpful to run your script to further understand the issue you are facing.
Best Regards, Jim
Paolo Bartolini
el 4 de Sept. de 2017
Jim Joy
el 5 de Sept. de 2017
Thank you for clarifying.
I have played around with this a little bit, and I see the issue that you're dealing with. It likely has to do with the fact that the gradients of step-functions are either 0 or infinity.
What is the end goal of your calculation? Are you trying to measure pulse height and width?
Best,
Jim
Paolo Bartolini
el 6 de Sept. de 2017
Editada: Paolo Bartolini
el 6 de Sept. de 2017
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Simulation, Tuning, and Visualization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!