Clear Filters
Clear Filters

Find local min point in plot between range

12 views (last 30 days)
Hello,
I want to write a code that is able to find a local min point between range that i will choose.
For example in the plot i would like to find the local minimun between 400-700 and 1000-1400.
i used islocalmin but it finds me min point where i dont need to.
At this plot i just daleted the point that i dont need from plot but i want to save time and to tell where to look for this min points.
Thank you.

Answers (2)

Davide Masiello
Davide Masiello on 8 Mar 2022
Edited: Davide Masiello on 8 Mar 2022
A way of doing this could be the following.
clear,clc
wavelength = 0:100; % Wavelength data
rel_reflec = 10*rand(size(wavelength)); % Relative reflectance data
wv_min = 20; % Lower bound for minima detection
wv_max = 40; % Upper bound for minima detection
wv = wavelength(wavelength>=wv_min & wavelength<=wv_max); % Selects x_values in desired range
rr = rel_reflec(wavelength>=wv_min & wavelength<=wv_max); % Selects y_values in desired range
TF = islocalmin(rr); % Finds local minima as logical indexes
plot(wavelength,rel_reflec,wv(TF),rr(TF),'or') % Plot results
This, for instance, yields
You should obtain the desired result by using your own data of wavelength and relative reflectance.

Mathieu NOE
Mathieu NOE on 8 Mar 2022
hello Anton (again !)
this would be my suggestion - based on dummy data
% some dummy data
Wavelength=1:1000;
reflection = min(ones(size(Wavelength)),1+0.55*sin(Wavelength/330).*sin(Wavelength/50+1.5));
% define range as (xlow xhigh) limits; define as many lines as number of
% ranges needed
range = [100 300;700 900]; % two range case
% range = [100 300;700 900;400 600]; % three range case
lmin = islocalmin(reflection,"MinSeparation",100);
lmin_index = find(lmin);
lmin_value = reflection(lmin);
Wavelength_lmin = Wavelength(lmin);
% define valid index according to range definition
for ck = 1:size(range,1) % loop over range lines
valid(ck) = find(lmin_index>=range(ck,1) & lmin_index<=range(ck,2));
end
% keep only data in range
lmin_index = lmin_index(valid);
lmin_value = lmin_value(valid);
Wavelength_lmin = Wavelength_lmin(valid);
plot(Wavelength,reflection,Wavelength_lmin,lmin_value,'ro');
hold on
title('Relative Reflectance-Wavelength')
xlabel('Wavelength[nm]')
ylabel('Relative Reflectance')
for ci = 1:numel(lmin_value)
bellheight=1-lmin_value(ci);
bellmid=1-bellheight/2;
threshold = bellmid; %
[t0_pos,s0_pos,t0_neg,s0_neg]= crossing_V7(reflection,Wavelength,threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
% ind => time index (samples)
% t0 => corresponding time (x) values
% s0 => corresponding function (y) values , obviously they must be equal to "threshold"
% keep closest t0_neg t0_pos values from lmin_value
[val,indd] = min(abs(lmin_index(ci)-t0_neg));
t0_neg_selected = t0_neg(indd);
[val,indd] = min(abs(lmin_index(ci)-t0_pos));
t0_pos_selected = t0_pos(indd);
x_line = [t0_neg_selected t0_pos_selected];
y_line = threshold*ones(size(x_line));
plot(x_line,y_line,'r','linewidth',2,'markersize',12);grid on
end
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t0_pos,s0_pos,t0_neg,s0_neg] = crossing_V7(S,t,level,imeth)
% [ind,t0,s0,t0close,s0close] = crossing_V6(S,t,level,imeth,slope_sign) % older format
% CROSSING find the crossings of a given level of a signal
% ind = CROSSING(S) returns an index vector ind, the signal
% S crosses zero at ind or at between ind and ind+1
% [ind,t0] = CROSSING(S,t) additionally returns a time
% vector t0 of the zero crossings of the signal S. The crossing
% times are linearly interpolated between the given times t
% [ind,t0] = CROSSING(S,t,level) returns the crossings of the
% given level instead of the zero crossings
% ind = CROSSING(S,[],level) as above but without time interpolation
% [ind,t0] = CROSSING(S,t,level,par) allows additional parameters
% par = {'none'|'linear'}.
% With interpolation turned off (par = 'none') this function always
% returns the value left of the zero (the data point thats nearest
% to the zero AND smaller than the zero crossing).
%
% check the number of input arguments
error(nargchk(1,4,nargin));
% check the time vector input for consistency
if nargin < 2 | isempty(t)
% if no time vector is given, use the index vector as time
t = 1:length(S);
elseif length(t) ~= length(S)
% if S and t are not of the same length, throw an error
error('t and S must be of identical length!');
end
% check the level input
if nargin < 3
% set standard value 0, if level is not given
level = 0;
end
% check interpolation method input
if nargin < 4
imeth = 'linear';
end
% make row vectors
t = t(:)';
S = S(:)';
% always search for zeros. So if we want the crossing of
% any other threshold value "level", we subtract it from
% the values and search for zeros.
S = S - level;
% first look for exact zeros
ind0 = find( S == 0 );
% then look for zero crossings between data points
S1 = S(1:end-1) .* S(2:end);
ind1 = find( S1 < 0 );
% bring exact zeros and "in-between" zeros together
ind = sort([ind0 ind1]);
% and pick the associated time values
t0 = t(ind);
s0 = S(ind);
if ~isempty(ind)
if strcmp(imeth,'linear')
% linear interpolation of crossing
for ii=1:length(t0)
%if abs(S(ind(ii))) >= eps(S(ind(ii))) % MATLAB V7 et +
if abs(S(ind(ii))) >= eps*abs(S(ind(ii))) % MATLAB V6 et - EPS * ABS(X)
% interpolate only when data point is not already zero
NUM = (t(ind(ii)+1) - t(ind(ii)));
DEN = (S(ind(ii)+1) - S(ind(ii)));
slope = NUM / DEN;
slope_sign(ii) = sign(slope);
t0(ii) = t0(ii) - S(ind(ii)) * slope;
s0(ii) = level;
end
end
end
% extract the positive slope crossing points
ind_pos = find(sign(slope_sign)>0);
t0_pos = t0(ind_pos);
s0_pos = s0(ind_pos);
% extract the negative slope crossing points
ind_neg = find(sign(slope_sign)<0);
t0_neg = t0(ind_neg);
s0_neg = s0(ind_neg);
else
% empty output
ind_pos = [];
t0_pos = [];
s0_pos = [];
% extract the negative slope crossing points
ind_neg = [];
t0_neg = [];
s0_neg = [];
end
end

Community Treasure Hunt

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

Start Hunting!