MATLAB Answers

# How can I get the slope of the peak?

57 views (last 30 days)
tzu-hsin yang on 21 Sep 2020 at 2:50
Commented: tzu-hsin yang on 23 Sep 2020 at 7:42

I would like to know the slpoe of the orange line( picture shows below)
Thank you!
data=xlsread('data.xlsx',1,'A:B'); %import data,column A means the frame number; column B means the intensity%
frame=data(:,1);
time=data(:,1)./25;
avgI=data(:,2);
B=transpose(avgI); %to make column into row%
[TF,S1,S2]=ischange(B,'linear','Threshold',200);
segline = S1.*(1:3012) + S2; % to get a linear line that fit the peak%
plot(time,B,time,segline)
legend('Data','Linear Regime')
xlabel('Time / s')
ylabel('Mean gray value')
%%

#### 2 Comments

Image Analyst on 21 Sep 2020 at 2:59
The slope at the peak itself is zero. Do you mean the slope from x=54 to the peak at x=80? Or the slope from x=80 to x=100?
tzu-hsin yang on 21 Sep 2020 at 4:50
The slope from x=54 to x=63, the slope oh the orange line.

Sign in to comment.

### Accepted Answer

Image Analyst on 21 Sep 2020 at 14:57
Try this:
% Your existing code:
data=xlsread('data.xlsx',1,'A:B'); %import data,column A means the frame number; column B means the intensity%
frame=data(:,1);
time=data(:,1)./25;
avgI=data(:,2);
B=transpose(avgI); %to make column into row%
[TF,S1,S2]=ischange(B,'linear','Threshold',200);
segline = S1.*(1:3012) + S2; % to get a linear line that fit the peak%
plot(time,B,time,segline)
legend('Data','Linear Regime')
xlabel('Time / s')
ylabel('Mean gray value')
%% New Code
x = time;
y = B;
% Get index for section from time = 54 to 63
index1 = find(x > 54, 1, 'first');
index2 = find(x > 63, 1, 'first');
% Compute slope in that section:
coefficients = polyfit(x(index1:index2), y(index1:index2), 1)
yFit = polyval(coefficients, x(index1:index2));
hold on;
plot(x(index1:index2), yFit, 'r-', 'LineWidth', 2);
caption = sprintf('Slope from time = %f to time = %f is %f', x(index1), x(index2), slope)
title(caption, 'FontSize', 20)
slope = coefficients(1)

#### 1 Comment

tzu-hsin yang on 23 Sep 2020 at 7:41
Thank you very much ! This really help me a lot.

Sign in to comment.

### More Answers (1)

Vasishta Bhargava on 21 Sep 2020 at 4:13
% if true
% code
%end
F = gradient(segline); % w.r.t x
[Fx Fy] = gradient(segline); % w.r.t x & y

#### 4 Comments

Show 1 older comment
Vasishta Bhargava on 21 Sep 2020 at 14:43
Try only the first line in my previous comment
Vasishta Bhargava on 21 Sep 2020 at 14:45
The second one can be used if you want slope for both x and y directions
tzu-hsin yang on 23 Sep 2020 at 7:42
Thanks!

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by