Find next value above threshold

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

 采纳的回答

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 个评论

Thank you so much for this, it was a great help.
I have implemented the code below, i have a few hundred variables, so please excuse messy variable names.
I cant seem to get the output quite right. The centre line is a known value in time, so ive made it constant with AAAM.
I get the resultant graph and the smaller red line should be where the value goes above 300 (data has changed slighly since first question).
I am very new to matlab and its been a huge learning curve, so forgive me if the changes ive made to the code are messy.
Thank you
Joe
Capture.JPG
AAA = Input165(89094:91060,:);
TAA= TimeSeries(89094:91060,:);
AAAM = TimeSeries(90077,:);
plot(TAA,AAA)
ylim([0 1000]);
hold on;
plot([AAAM AAAM],[0 1500])
line(xlim, [300, 300], 'Color', 'r', 'LineWidth', 2);
logicalIndexes = AAA > 300;
linearIndexes = find(logicalIndexes);
indexDistances = abs(linearIndexes - AAAM);
[indexDistance, indexOfThreshold] = min(indexDistances)
hold on;
line([TAA(indexOfThreshold), TAA(indexOfThreshold)], [min(AAA), AAA(indexOfThreshold)], 'Color', 'r', 'LineWidth', 2);
You forgot to attach your data so I can't do anything (yet).
Joe:
I misunderstood what you want. Now that we've wasted 2 days, I think you realize how important it is to give plots right away. If we had had your plots right at the very start, at the top, you'd have your answer 2 days ago. I'll have to work on a totally new and different program for you.
Joe! These x and y ranges are totally different than what you originally stated. What gives?
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

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by