fitting with a rectangular pulse function
이전 댓글 표시
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
Jim Joy
2017년 8월 31일
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
2017년 9월 4일
Jim Joy
2017년 9월 5일
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
2017년 9월 6일
편집: Paolo Bartolini
2017년 9월 6일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Simulation, Tuning, and Visualization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!