Bug in islocalmin/islocalmax?

11 ビュー (過去 30 日間)
Robert
Robert 2022 年 6 月 18 日
コメント済み: Star Strider 2022 年 6 月 18 日
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,

採用された回答

Robert
Robert 2022 年 6 月 18 日
I may have figured it out, thanks to comments here, it got me thinking towards filtering. Turns out simple truncation is the key. In my case, y = round(y,4) works for prominance of 1e-5. This essentially flattens the data perturbations.

その他の回答 (3 件)

Image Analyst
Image Analyst 2022 年 6 月 18 日
編集済み: Image Analyst 2022 年 6 月 18 日
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
Star Strider 2022 年 6 月 18 日
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 件のコメント
Robert
Robert 2022 年 6 月 18 日
Smoothing data does work, but I wanted to stay away from that as it also can produce perturbations and requires tunning the window size for example.
Star Strider
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
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
  1 件のコメント
Robert
Robert 2022 年 6 月 18 日
The issue is that there is perturbations to data, not just a min value.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMeasurements and Spatial Audio についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by