我正在尝试在Matlab中实现自己的代码,以便将闭合的b样条拟合为一组2D数据。
我以椭圆的形式生成了2D数据并使用了De boor算法。设置为打开的b样条时,代码确实可以正常工作,但它会被闭合(周期性)b样条卡住。
以下是步骤:
function u = npa_contfit_genknot(t,d,nmp,opt,varargin) switch opt case 'uniform-clamped' a = varargin{1}(1); b = varargin{1}(2); u = [a*ones(1,d),... a:(b-a)/(nmp-d):b,... b*ones(1,d)]'; case 'uniform' a = varargin{1}(1); b = varargin{1}(2); u = linspace(a,b,nmp+d)'; case 'average' u = conv(t, [zeros(1,d) ones(1,d)]/d, 'same'); u = [zeros(d,1);u(d+1:end-1);ones(d,1)]; end end
构造b样条基矩阵的函数 B :
% de-boor algorithm B = zeros(nmt_i,nmp); for j=0:nmp-1 [b,px] = npa_contfit_bsplinebasis(j,d,u,t_i); B(:,j+1) = b; end
估计b样条混合函数值的函数
function [y,t] = npa_contfit_bsplinebasis(i,d,u,t) y = bspline_basis_recur(i,d,u,t); % nested function function y = bspline_basis_recur(i,d,u,t) y = zeros(size(t)); if d > 1 b = npa_contfit_bsplinebasis(i,d-1,u,t); dn = t - u(i+1); dd = u(i+d) - u(i+1); if dd ~= 0 % indeterminate forms 0/0 are deemed to be zero y = y + b.*(dn./dd); end b = npa_contfit_bsplinebasis(i+1,d-1,u,t); dn = u(i+d+1) - t; dd = u(i+d+1) - u(i+1+1); if dd ~= 0 y = y + b.*(dn./dd); end elseif u(i+2) < u(end) % treat last element of knot vector as a special case y(u(i+1) <= t & t < u(i+2)) = 1; else y(u(i+1) <= t) = 1; end end % end end
P = (pinv(B)*C')';
P = [P(:,end-d+1:end),P(:,d+1:end-d)]
打开b样条设置的结果:
关闭(周期性)b样条设置的结果