fitting peak points maxima
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I am facing some problems trying to create a curve that will pass through maximum peaks. Will a solution be to export peaks into a new array, plot them and then create a fitting curve or there is another way?
Thanks in advance!
채택된 답변
Image Analyst
2020년 5월 22일
0 개 추천
Just try interp1() or spline(). Pass in your peak points and then the x values of the whole array, something like
[peakValues, indexes] = findpeaks(Sig);
yOut = spline(indexes, peakValues, 1:length(Sig));
plot(yOut, 'b-', 'LineWidth', 2);
grid on;
Let me know if it works. Attach your Sig data if it doesn't. Accept this answer if it does.
댓글 수: 6
Ancalagon8
2020년 5월 25일
Have you seen it?
Image Analyst
2020년 5월 25일
편집: Image Analyst
2020년 5월 25일
Well that data looks totally different than the first set, but it shouldn't matter. But, no I didn't see your code - you forgot to post it so I can't fix it. All I saw was your mat file, not the code - where is the code that you tried attached? Was it something like this:
s = load('sig.mat')
y = s.Sig3b;
x = 1 : length(y);
plot(x, y, 'b.-', 'MarkerSize', 20);
grid on
[peakValues, peakIndexes] = findpeaks(y, 'MinPeakDistance', 15);
hold on;
plot(peakIndexes, peakValues, 'r*');
% Interpolate
yFit = spline(peakIndexes, peakValues, x);
plot(x, yFit, 'c-');
Do you want something that exactly hits the top of each peak (like the above code), or a regressed line that fits the peaks with an exponential decay, like the attached demo?
Image Analyst
2020년 5월 25일
OK. Try to adapt it and show your results. The demo is well commented so a smart engineer like you should be able to follow it.
It helps to manually solve the equation using a single point identified on the fit to get the estimated coefficients. But it doesn't look like it's a pure exponential decay, or else we need to do a better job with findpeaks() to get the right peaks.
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
s = load('sig.mat')
y = s.Sig3b;
% Get rid of negative.
y(y<0) = [];
x = 1 : length(y);
plot(x, y, 'b', 'MarkerSize', 15);
grid on
[peakValues, peakIndexes] = findpeaks(y, 'MinPeakDistance', 15);
hold on;
plot(peakIndexes, peakValues, 'r.');
% Interpolate
yFit = spline(peakIndexes, peakValues, x);
plot(x, yFit, 'r-');
% Define coefficients and function that the X values obey.
a = 100 % Arbitrary sample values I picked.
b = 0.4
c = 5
tbl = table(x(:), y(:));
% Define the model as Y = a * exp(-b*x) + c
% Note how this "x" of modelfun is related to big X and big Y.
% x((:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) * exp(-b(2)*x(:, 1)) + b(3);
% Guess values to start with. Just make your best guess.
aGuessed = 8000 % Arbitrary sample values I picked.
bGuessed = 0.005
cGuessed = 0
beta0 = [aGuessed, bGuessed, cGuessed]; % Guess values to start with. Just make your best guess.
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
coefficients = mdl.Coefficients{:, 'Estimate'}
% Create smoothed/regressed data using the model:
yFitted = coefficients(1) * exp(-coefficients(2)*x) + coefficients(3);
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(x, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'north');
legendHandle.FontSize = 30;
% Place formula text roughly in the middle of the plot.
formulaString = sprintf('Y = %.3f * exp(-%.3f * X) + %.3f', coefficients(1), coefficients(2), coefficients(3))
xl = xlim;
yl = ylim;
xt = xl(1) + abs(xl(2)-xl(1)) * 0.325;
yt = yl(1) + abs(yl(2)-yl(1)) * 0.59;
text(xt, yt, formulaString, 'FontSize', 25, 'FontWeight', 'bold');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Image Analyst
2020년 5월 25일
편집: Image Analyst
2020년 5월 25일
We need to make the table from ONLY the peak points, not all of them, so
% Define coefficients and function that the X values obey.
tbl = table(x(peakIndexes)', y(peakIndexes)');
NOT
% Define coefficients and function that the X values obey.
tbl = table(x(:), y(:));
New code:
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
s = load('sig.mat')
y = s.Sig3b;
% Get rid of negative.
y(y<0) = [];
x = 1 : length(y);
plot(x, y, 'b', 'MarkerSize', 15);
grid on
[peakValues, peakIndexes] = findpeaks(y, 'MinPeakDistance', 15);
hold on;
plot(peakIndexes, peakValues, 'r.', 'MarkerSize', 30);
% Interpolate
yFit = spline(peakIndexes, peakValues, x);
plot(x, yFit, 'r-');
% Define coefficients and function that the X values obey.
tbl = table(x(peakIndexes)', y(peakIndexes)');
% Define the model as Y = a * exp(-b*x) + c
% Note how this "x" of modelfun is related to big X and big Y.
% x((:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) * exp(-b(2)*x(:, 1)) + b(3);
% Guess values to start with. Just make your best guess.
aGuessed = 6000 % Arbitrary sample values I picked.
bGuessed = 0.004
cGuessed = 0
beta0 = [aGuessed, bGuessed, cGuessed]; % Guess values to start with. Just make your best guess.
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
coefficients = mdl.Coefficients{:, 'Estimate'}
% Create smoothed/regressed data using the model:
yFitted = coefficients(1) * exp(-coefficients(2)*x) + coefficients(3);
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(x, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'north');
legendHandle.FontSize = 30;
% Place formula text roughly in the middle of the plot.
formulaString = sprintf('Y = %.3f * exp(-%.3f * X) + %.3f', coefficients(1), coefficients(2), coefficients(3))
xl = xlim;
yl = ylim;
xt = xl(1) + abs(xl(2)-xl(1)) * 0.325;
yt = yl(1) + abs(yl(2)-yl(1)) * 0.59;
text(xt, yt, formulaString, 'FontSize', 25, 'FontWeight', 'bold');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Note that there are some peaks early on below 3000, so you'll need to experiment around with findpeaks() if you want to eliminate those and get a better fit.
Note: If you want to specify a point for the y axis intercept, that is possible by adjusting the model. As it is, it tries to give its best guess.
Image Analyst
2020년 6월 18일
Yes. Did you try to change the model? It's not hard. See attached.

추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Scopes and Data Logging에 대해 자세히 알아보기
제품
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
