fitting with a rectangular pulse function
Show older comments
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 Comments
Jim Joy
on 31 Aug 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
on 4 Sep 2017
Jim Joy
on 5 Sep 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
on 6 Sep 2017
Edited: Paolo Bartolini
on 6 Sep 2017
Accepted Answer
More Answers (0)
Categories
Find more on Simulation, Tuning, and Visualization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!