Bug in islocalmin/islocalmax?
古いコメントを表示
I have some data that has some numerical round off errors that I need to find local minimums and local maximums within a tolerance, but it doesn't work the way I think I should. Here is a reproducable example.
y = [1 0 1e-10 0 2]; % ideally this should be [1 0 0 0 2];
tolerance = 1e-5;
% islocalmax works, it gives an empty array
find(islocalmax(y,'FlatSelection','center','MinProminence',1e-5))
% however islocalmin does not work, it gives two minimums at 2 and 4 rather
% than expected 3 according to my tolerance
find(islocalmin(y,'FlatSelection','center','MinProminence',1e-5))
The same odd behaviour occurs for the following
y2 = [1 0 1e-10 1e-10 0 2];
find(islocalmin(y2,'FlatSelection','center','MinProminence',1e-5))
% yields 2 and 5, instead of expected 3
y3 = [1 0 1e-10 -1e-10 0 2];
find(islocalmin(y3,'FlatSelection','center','MinProminence',1e-5))
% yields 4, instead of expected 3
Is there a workaround? Is there a way I could "flatten" the values according to tolerance? Note, I only used 0 to 1e-10 to ilustrate this, I would like this to work for any values with smaller variations than 1e-5.
Thank you all,
採用された回答
その他の回答 (3 件)
Image Analyst
2022 年 6 月 18 日
編集済み: Image Analyst
2022 年 6 月 18 日
0 投票
I think you should be using findpeaks. It has the ability to filter the peaks to get just the ones you want - the peaks with the particular properties you desire.
Star Strider
2022 年 6 月 18 日
0 投票
Those functions (and findpeaks) do not use anything that could be considered to be a ‘tolerance’. The ‘prominence’ of a peak (or valley, depending on the function) relates the peak to the values that surround it.
To use a ‘tolerance’ value (that I assume means a range of peak height values), the only option that I can think of would require two different calls to the functions with different criteria, getting the logical vectors (or using find to get the numeric indices), and then using logical (logical vectors) or set (numeric indices) functions on the results to get the differences between the outputs of the two calls.
4 件のコメント
Star Strider
2022 年 6 月 18 日
I am not certain what result you want.
You could consider the low-amplitude values to be noise, and some sort of filtering or smoothing might work (for example smoothdata), however the example vectors are too short to do any sort of processing with, so I cannot demonstrate those results with them.
.
Robert
2022 年 6 月 18 日
Star Strider
2022 年 6 月 18 日
O.K.
That’s the best option I can think of. I didn’t consider the round function, since I’m still not certain what the actual problem is.
Image Analyst
2022 年 6 月 18 日
Perhaps you just want to set values less than the tolerance to the min?
y(y<tolerance) = min(y);
Or perhaps you want to do some denoising with something like a median filter or Savitzky-Golay filter sgolayfilt
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!