Determine Threshold in Graph

45 views (last 30 days)
Daan
Daan on 5 Oct 2015
Edited: Image Analyst on 28 Sep 2020
Hi all, I want to split my data file into two data files. One with the data of the peaks (above the threshold) and one with the remaining data (under the threshold). Like this:
I have got hundreds of data files so I want to split them in a for loop. My problem is that every dataset has a different threshold value:
All data files have got the same shape, so I wonder if it is possible to let matlab determine the threshold value. My problem is that I can't find the good way to determine the threshold value. Can someone help me out with this?
Thanks, Daan

Accepted Answer

Image Analyst
Image Analyst on 5 Oct 2015
To threshold a signal where the "baseline" varies, I think you should take the histogram. Then compute the PDF and cdf. Compute the cdf with cumsum(). If you know that the peaks always occupy, say 3% of the signal, then you can find the gray level of that from the cdf.
Histogram has changed recently. What version of MATLAB do you have? If you have the latest one, use histogram() or histcounts(). If earlier than about R2014b then use hist() or histc().
[counts, binCenters] = hist(data, 100);
cdf = cumsum(counts);
cdf = cdf / cdf(end);
thresholdIndex = find(cdf>0.97);
thresholdLevel = binCenters(thresholdIndex);
logicalIndexes = data > thresholdLevel; % Indexes of peaks.
  6 Comments
Andrew Park
Andrew Park on 28 Sep 2020
Edited: Andrew Park on 28 Sep 2020
Thank you for the answer. I'm not sure if I should make this as a whole separate post, so I'll ask you here first.
If I don't know how much percentage of the total signal the peaks occupy, what would be the best method to set the percentage? For instance, I have graphs that have varying number of peaks, so I don't know how many there are until the algorithm scans the signal once to count them - which will take additional time. As of now, I'm just making an assumption that the 100th largest peak height will suffice as the threshold value.
(If you think I should post this separately, I'll do that.)
Image Analyst
Image Analyst on 28 Sep 2020
Edited: Image Analyst on 28 Sep 2020
Yes, post separately after reading this link so you'll know to do certain things, like including any scripts and data, and a screenshot.

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!