How to avoid the ending drop of the movmean curve?
18 views (last 30 days)
Show older comments
I'm playing around with the built-in movmean function, which creates the moving average series of given data.
I noticed that in some cases the movmean curve badly fits the data at the last values, consider the following example
data = [386 379 295 238 159 190 384 402 552 347 463 259 412 481 523 589 611 479 320 403 642];
plot(movmean(data,7)) % 7 since data are collected daily and show cycles of length 7
hold on
plot(data)
legend('movmean','data','Location','northwest')
Since data are increasing, the movmean is supposed to increase too, while it drops at the end.
Is there a way to avoid this behaviour?
1 Comment
christina widyaningtyas
on 23 Jun 2022
Hi, I also need more clear explanation related this post
I run the script to calculate "Average data for period xx seconds"
P is pressure data,
P1mean = movmean(P_1(130:5000),120); %average for 120secs
my question, how to determine 130 and 5000? my prof told me that because at the beginning of the measurement, the data was discontinuous (a.png file), so He just deleted that part.
Does this mean, the red part (b.png file) is not included in the movmean calculation?
Any one can explained it more details how to determined the value 130 and 5000, please?
Answers (1)
Stephan
on 20 Aug 2020
Edited: Stephan
on 20 Aug 2020
Use the Endpoints option:
data = [386 379 295 238 159 190 384 402 552 347 463 259 412 481 523 589 611 479 320 403 642];
window = 7;
avgMove = nan(1,numel(data));
avgMove(window+1:end) = movmean(data,[window 0],'Endpoints','discard');
plot(avgMove) % 7 since data are collected daily and show cycles of length 7
hold on
plot(data)
hold off
legend('movmean','data','Location','northwest')
Also if you give only 1 value for the window the data is centered, which is usually not what is expected. See documentation for details.
5 Comments
Steven Lord
on 20 Aug 2020
On the documentation page for the movmean function, look at the description of the k input argument. Imagine that the window of length 3 was centered over the 0 that is the first element of A.
If the value of the 'Endpoints' name-value pair argument was 'shrink' (the default) for that window movmean would take the average of the first two elements of A (0 and 2) and ignore the section of the window that's outside the vector A.
If the value was 'discard', movmean realizes the window extends beyond the end of the vector A, and so the result of that movmean call is two elements shorter than A. The first window gets thrown out, as does the last.
If the value was 'fill' then the elements in the window that are outside the vector A are treated as NaN, and if the value was a constant the elements would be treated as that constant.
A = [0 2 4 1 3 5 7];
y1 = movmean(A, 3) % 'shrink' is the default
y2 = movmean(A, 3, 'Endpoints', 'discard') % Matches y1(2:6)
y3 = movmean(A, 3, 'Endpoints', 'fill') % First and last are NaN, otherwise matches y1
y4 = movmean(A, 3, 'Endpoints', 7) % First element is (7+0+2)/3 = 3
Also note that Stephan used a window of [window 0] not a window of 7. Compare the pictures in the descriptions of the [kb kf] and k inputs on that documentation page for an illustration of the difference. That's why the first seven points were discarded.
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!