Kolmogorov - Filter Matlab
Hy
I need to use this Kolmogorov filter in my application. You put in some measured data and with the filter it gets a smoothed hoe. I tried to do it with "nchoosek", however, when I try to do it for I'm 50 or more, it takes too long.
Does anyone know how to do this faster?
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
thanks Andy
source to share
Here is a solution without a loop:
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;
I avoided using it nchoosek
and instead calculating the binomial coefficients manually using factorials. This ensures that each factorial calculation is performed only once. In contrast, the OP's solution potentially performs each factorial calculation hundreds of times.
Once the binomial coefficients are calculated, the solution from there is directly applied conv
and then scaled by the denominator.
I quickly checked the speed between the OP's solution and my solution. The speed test uses a random vector x
with 50 elements and sets K
to 5. I then run iterations 100
over my solution versus the OP's solution. Here are the results:
Elapsed time is 2.637597 seconds. %# OP Solution
Elapsed time is 0.010401 seconds. %# My Solution
I am very happy with this. I doubt this method can be made much more efficient from now on (but would be glad to be wrong). :-)
source to share