how to count no. of elements exceeding given threshold value in selected moving window

I have time vs values plot. time length=100, I want count number of values exceeds 20 for time window 4, then skipping 1st & adding next time so window size should be constant 4, and count number of values which exceeds 20.

Réponses (3)

Try movsum(). (It's a fairly new function).
% Create 30 random values.
signal = randi(100, 1, 30)
% Only want values more than 20 in the sum so set those under to 0
signal(signal <= 20) = 0
% Compute the moving sum
summedSignal = movsum(signal, 4)

9 commentaires

Regarding your comment to Steven, try this
% Create 100 random values.
signal = randi(100, 1, 100)
% Only want values more than 20 in the sum so set those under to 0
signal(signal <= 20) = 0
% Compute the moving sum
summedSignal = conv(signal, [1,1,1,1], 'valid');
I don't want to sum element values but just want to count how many elements are exceeding given threshold value for size of window 4, I think I didn't put question clearly, I will change it.
To sum elements above a threshold:
aboveThreshold = signal > yourThresholdValue;
theSum = conv(aboveThreshold, [1,1,1,1], 'valid');
I ran code
% Create 100 random values.
signal = randi(100, 1, 10)
% Only want values more than 20 in the sum so set those under to 0
signal(signal <= 20) = 0
% Compute the moving sum
summedSignal = conv(signal, [1,1,1,1], 'valid');
aboveThreshold = signal > 25;
theSum = conv(aboveThreshold, [1,1,1,1], 'valid');
but it was showing following error
Error using conv2
First and second arguments must be single or double.
Error in conv (line 40)
c = conv2(a(:),b(:),shape);
Error in okokok (line 9)
theSum = conv(aboveThreshold, [1,1,1,1], 'valid');
I guess it didn't like that aboveThreshold was logical instead of double. So try this:
theSum = conv(double(aboveThreshold), [1,1,1,1], 'valid');
so this is for all 100 length, how to do this for 40 length i.e. I have 100 length and I want to compute it for first 40
Nothing in my code (other than generating some sample data) has any reliance on the length of the signal being 40 or 100 or anything else. Any length signal will work. If it's longer and you only want the first 40, then crop it off
theSum = theSum(1:40); % Take only the first 40.
for next 41 to 100 I have to do same operation but with value comes below 20 (but not exceed 20) & for different window size so I am thinking there is way to do above for only 40

Connectez-vous pour commenter.

It's not really clear to me from your description whether the window length is fixed (in which case I recommend using the movsum function that was introduced in release R2016a) or whether the window length can vary and the endpoints of the window are selected based on the sum of the elements in the window.

2 commentaires

time length =100, so 1st 1 to 4, then 2 to 5 likewise, at last 97 to 100 then stop. and I want number of elements which are exceeding limit, not sum of values i.e. in particular window, 4 values are 12 34 36 25 so ans would be 3.

Connectez-vous pour commenter.

above_threshold = (values(:) > 20) .';
cum_above = cumsum([0 above_threshold]);
running_count = cum_above(5:end) - cum_above(1:end-4);
or
above_threshold = (values(:) > 20) .';
running_count = conv(above_threshold, ones(1,4), 'valid');
Note that these will be shorter than the original time series because they only use full windows.

Community Treasure Hunt

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

Start Hunting!

Translated by