Matlab中的简单滑动窗口滤波器

发布于 2024-12-12 22:26:37 字数 555 浏览 0 评论 0原文

我没有 nlfilter 包,而且我不太遵循 此示例

我有一个非常简单的函数 fun,我想将其应用于数组的移动窗口。数组是 Nx1,我想查看长度 k 间隔。因此,对于 N=10k=3fun = @(x) min(x); 我会得到

A = [13 14 2 14 10 3 5 9 15 8];

filter(A,k,fun) = [2 2 2 3 3 3 5 8];

Here 我只想查看索引 1,2,3,然后 2,3,4,然后...然后 8,9,10,所以最终序列的长度为 7。我可以使用 for 循环轻松完成此操作,但我不知道如何为 Matlab 对其进行矢量化。请帮忙。谢谢。

I don't have the package for nlfilter and I didn't quite follow this example.

I have a really simple function fun and I want to apply it to a moving window of an array. The array is Nx1, and I want to look at length k intervals, say. So for N=10 and k=3 and fun = @(x) min(x); I would get

A = [13 14 2 14 10 3 5 9 15 8];

filter(A,k,fun) = [2 2 2 3 3 3 5 8];

Here I only want to look at indices 1,2,3 then 2,3,4 then ... then 8,9,10, so the final sequence is length 7. I can do this easy with a for loop, but I have no idea how to vectorize it for Matlab. Help, please. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[浮城] 2024-12-19 22:26:38

您提到的帖子给出了构建的通用解决方案滑动窗口(您可以控制:重叠与不同、滑动步长、重叠量、窗口大小)

在您的情况下,它要简单得多,可以使用 HAN​​KEL 函数:

x = [13 14 2 14 10 3 5 9 15 8];
idx = hankel(1:3, 3:length(x))
min( x(idx) )

如果您想构建一个可重用的解决方案:

function y = myFilter(x,k,fcn)
    idx = hankel(1:k, k:length(x));
    y = cellfun(fcn, num2cell(x(idx),1));
end

我们将其用作:

x = [13 14 2 14 10 3 5 9 15 8];
y = myFilter(x, 3, @(x)min(x))

注意,我使用 CELLFUN 以防 fcn 无法在矢量化中跨维度操作方式...

The post you mentioned gave a general solution for building sliding windows (you could control: overlapping vs. distinct, slide step, overlap amount, windows size)

In your case, it is much simpler and can be easily performed with the HANKEL function:

x = [13 14 2 14 10 3 5 9 15 8];
idx = hankel(1:3, 3:length(x))
min( x(idx) )

If you want to build a reusable solution:

function y = myFilter(x,k,fcn)
    idx = hankel(1:k, k:length(x));
    y = cellfun(fcn, num2cell(x(idx),1));
end

which we use as:

x = [13 14 2 14 10 3 5 9 15 8];
y = myFilter(x, 3, @(x)min(x))

Note I am using CELLFUN in case fcn cannot operate across dimensions in a vectorized manner...

夜声 2024-12-19 22:26:37

这是一种非常简单快捷的方法:

>> min([A(1:(end-2)); A(2:(end-1)); A(3:end)], [], 1)

ans =

     2     2     2     3     3     3     5     8

编辑:因为你想要一个完整的功能......

function running_min = running_min(x, k)

xrep = repmat(x, 1, k);
xrep = reshape([xrep zeros(1, k)], length(x)+1, k);
running_min = min(xrep, [], 2)';
running_min = running_min(1:end-k);

Here is one very simple and fast way to do it:

>> min([A(1:(end-2)); A(2:(end-1)); A(3:end)], [], 1)

ans =

     2     2     2     3     3     3     5     8

EDIT: Since you want a full function...

function running_min = running_min(x, k)

xrep = repmat(x, 1, k);
xrep = reshape([xrep zeros(1, k)], length(x)+1, k);
running_min = min(xrep, [], 2)';
running_min = running_min(1:end-k);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文