你好
我需要在应用中使用这个 Kolmogorov 过滤器。您将一些测量数据放入其中,并使用过滤器对其进行一些平滑处理。 我试着用“nchoosek”来做,但是当我尝试为 50 或更多的 I 做这件事时,它花费的时间太长了。
有人知道如何更快地做到这一点吗?
function [ filterd ] = kolmo(data, inter)
temp = 0;
temp1 = 0;
filterd(1:10, 1) = NaN;
for t=inter+1:(length(data)-inter)
for o=-inter:inter
temp = temp + (nchoosek(2*inter, (inter+o))*data(t+o));
temp1 = temp1 + nchoosek(2*inter, (inter+o));
end
filterd(t, 1) = temp/temp1;
temp = 0;
temp1 = 0;
end
end
谢谢 安迪
最佳答案
这是一个无循环的解决方案:
function y = MySoln(x, K)
%# Get the binomial coefficient terms
FacAll = factorial(0:1:2*K)';
BinCoefAll = FacAll(end) ./ (FacAll .* flipud(FacAll));
%# Get all numerator terms
NumerAll = conv(x, BinCoefAll, 'valid');
%# Rescale numerator terms into output
y = (1 / sum(BinCoefAll)) * NumerAll;
我避免使用 nchoosek
而是使用阶乘手动计算二项式系数。这确保每个阶乘计算只执行一次。相比之下,OP 的解决方案可能对每个阶乘计算执行数百次。
一旦计算出二项式系数,就会直接应用 conv
的解决方案,然后按分母项缩放。
我在 OP 解决方案和我的解决方案之间进行了快速速度测试。速度测试使用具有 50 个元素的随机向量 x
,并将 K
设置为 5。然后我对我的解决方案与 OP 进行 100
迭代解决方案。以下是结果:
Elapsed time is 2.637597 seconds. %# OP Solution
Elapsed time is 0.010401 seconds. %# My Solution
我对此非常满意。我怀疑从这一点可以使该方法更加有效(但很高兴被证明是错误的)。 :-)
关于performance - Kolmogorov – 滤波器 Matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495684/