Smoothing with a nonuniform window size

조회 수: 9 (최근 30일)
David Honegger
David Honegger 2021년 9월 11일
답변: Prachi Kulkarni 2021년 9월 21일
Is there an optimized method to smooth data where the window size can vary? Essentially, I'm looking to find (or create)
>> movmean(A,k)
where size(A)==size(k).
A simple but slow working example might look like:
function ys = movmean2(yn,x,k)
ys = NaN(size(yn));
for i = 1:length(yn)
id = abs(x-x(i))<=k(i);
ys(i) = mean(yn(id));
end
end
In my application, which takes it to higher dimensions and large arrays, this is too slow. Can I avoid the loop? Where might I optimize it?

답변 (1개)

Prachi Kulkarni
Prachi Kulkarni 2021년 9월 21일
Hi,
Let A be a 1-D array. Let k be a 1-D array of the same length as A, containing the moving mean window length for each corresponding element of A. The following code gives you the moving mean with nonuniform window lengths without using a loop for computation. The loop only creates the function handle.
functionHandleString = "movmeanNonUniform = @(A,k) diag([movmean(A,k(1))";
for i = 2:length(k)
functionHandleString = functionHandleString + "; movmean(A,k(" + num2str(i) + "))";
end
functionHandleString = functionHandleString + "]);";
eval(functionHandleString);
movmeanNonUniform (A,k)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by