D = load('Breather_Data.m');
x = D(:,1);
y = D(:,2);
y = detrend(y);
yu = max(y);
yl = min(y);
yr = (yu-yl);
yz = y-yu+(yr/2);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0);
zt = x(zci(y));
per = 2*mean(diff(zt));
ym = mean(y);
fit = @(b,x) b(1) .* exp(b(2).*x) .* (sin(2*pi*x.*b(3) + b(4))) + b(5);
fcn = @(b) norm(fit(b,x) - y);
[s,nmrs] = fminsearch(fcn, [yr; -1E+11; 1/per; -1; ym])
xp = linspace(min(x),max(x), 500);
figure
plot(x,y,'b', 'LineWidth',1.5)
hold on
plot(xp,fit(s,xp), '--r')
hold off
grid
xlabel('Time')
ylabel('Amplitude')
legend('Original Data', 'Fitted Curve')
text(min(xlim)+0.05*diff(xlim), min(ylim)+0.8*diff(ylim), sprintf('$y = %.3E\\cdot e^{%.3E\\cdot x}\\cdot sin(2\\pi\\cdot x\\cdot %.3E + %.3f) %+.3f$', s), 'Interpreter','latex')
Best Answer