MATLAB: How to plot slope including maximum peaks

peakslope

Hello, How can I plot slope including peaks point? It should look like exp. I tried envelope but It does not work for my code.
Thank you.

Best Answer

If you have the Signal Processing Toolbox, use the findpeaks function to define the peaks and their locations. You can then use those values directly in your exponential regression.
EDIT
Try this:
y = [12 14 2 8 9 4 6 7 9 5 6];
x = 1:length(y);
ym = mean(y);
yz = y - ym; % Create Symmetry About Mean
[pospks, poslocs] = findpeaks(yz); % Positive Peaks & Locations
[negpks, neglocs] = findpeaks(-yz); % Negative Peaks & Locations
pkvct = [pospks negpks]; % Combine In One Vector FOr Sorting
[locs,idx] = sort([poslocs neglocs]); % Sort
pks = pkvct(idx);
objfcn = @(b,x) b(1).*exp(b(2).*x); % Exponential Objective Function
[B,resnorm] = fminsearch(@(b) norm(pks - objfcn(b,locs)), rand(2,1)); % Fit Data (EStimate Parameters)
figure(1)
plot(x, y, '-g')
hold on
% plot(locs, pks+ym, 'pg')
plot(locs, objfcn(B,locs)+ym, '-r')
hold off
text(5, 13, sprintf('y(x) = %.3f + e^{%.3f\\cdotx} + %.3f', B, ym))