Find next value above threshold

13 views (last 30 days)
Joe Gee
Joe Gee on 22 Mar 2019
Commented: Image Analyst on 24 Mar 2019
Hello,
I have a large volume of data, and i need to find the next value above a threshold but within a range of values.
ie
Data range 300000 to 340000 (the corresponding values will drop off in the middle and increase either side of centre, centre = 320000)
i need to set a datum point of 320000 (middle) then i need to find the closest value to the centre that is greater than >1500
Im fairly new to matlab, so sorry if this is begineers question, i cant seem to find relavant links
Thank you
Joe

Accepted Answer

Image Analyst
Image Analyst on 22 Mar 2019
Try this.
% First make sample data because Joe forgot to give us his actual data.
period = 20000;
x = linspace(22000, 42000, 800);
y = 3000 * (0.5 + cos(2 * pi * (x - 32000) / period));
% Add some noise
y = y + 800 * rand(1, length(y));
plot(x, y, 'b-');
grid on;
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
% Now we have our sample x,y data and we can begin:
% Find max of y - it should be at an x value of around 32,000.
[yMax, indexOfMax] = max(y);
% Draw vertical red line there
hold on;
line([x(indexOfMax), x(indexOfMax)], [min(y), y(indexOfMax)], 'Color', 'r', 'LineWidth', 2);
% Also draw red line across the threshold of 1500
line(xlim, [1500, 1500], 'Color', 'r', 'LineWidth', 2);
% Find logical indexes for indexes where y is less than 1500
logicalIndexes = y < 1500;
% Find linear indexes:
linearIndexes = find(logicalIndexes);
% Note: the above two lines could be done in one line
% but I wanted to demo both linear and logical indexes for you.
% Find distances of all indexes where y<1500 to the index of the max y.
indexDistances = abs(linearIndexes - indexOfMax);
% Find out which index is closest to indexOfMax
[indexDistance, indexOfThreshold] = min(indexDistances)
% Draw red line there, at the closest threshold.
hold on;
line([x(indexOfThreshold), x(indexOfThreshold)], [min(y), y(indexOfThreshold)], 'Color', 'r', 'LineWidth', 2);
0001 Screenshot.png
The index of the point you want is indexOfThreshold, and the x value is x(indexOfThreshold) and the y value is y(indexOfThreshold).
You can see in this example, that the closest point to the max where the data first goes above 1500 is 26500.
  6 Comments
Image Analyst
Image Analyst on 24 Mar 2019
Joe! These x and y ranges are totally different than what you originally stated. What gives?
Image Analyst
Image Analyst on 24 Mar 2019
Joe:
Try this:
% Load mat files into structures.
s1 = load('TAA.mat')
s2 = load('AAA.mat')
s3 = load('AAAM.mat')
AAA = s2.AAA;
TAA = s1.TAA;
AAAM = s3.AAAM;
% AAA = Input165(89094:91060,:);
% TAA= TimeSeries(89094:91060,:);
% AAAM = TimeSeries(90077,:);
middleIndex = find(TAA < AAAM, 1, 'last')
plot(TAA,AAA, 'b-')
ylim([0 1000]);
hold on;
plot([AAAM AAAM],[0 1500], 'k-', 'LineWidth', 2)
grid on;
% Define threshold.
thresholdValue = 550;
% Draw line at threshold.
line(xlim, [thresholdValue, thresholdValue], 'Color', 'g', 'LineWidth', 2);
% Find left index
leftIndex = middleIndex;
for k = middleIndex : -1 : 1
if AAA(k) > thresholdValue
leftIndex = k
TAALeft = TAA(k)
% Draw vertical red line there.
hold on;
line([TAALeft, TAALeft], ylim, 'Color', 'r', 'LineWidth', 2);
% Draw horizontal line
line([TAA(1), TAA(leftIndex)], [AAA(leftIndex), AAA(leftIndex)], 'Color', 'r', 'LineWidth', 2);
break;
end
end
% Find right index
thresholdValue = 550;
rightIndex = middleIndex;
for k = middleIndex : length(TAA)
if AAA(k) > thresholdValue
rightIndex = k
TAARight = TAA(k)
% Draw vertical red line there.
hold on;
line([TAARight, TAARight], ylim, 'Color', 'm', 'LineWidth', 2);
% Draw horizontal line
line([TAA(1), TAA(rightIndex)], [AAA(rightIndex), AAA(rightIndex)], 'Color', 'm', 'LineWidth', 2);
break;
end
end
0001 Screenshot.png

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!