Вы находитесь на странице: 1из 4

39.1 Overview.

B-splines are commonly used as basis functions to t smoothing curves to large data sets. To do this, the abscissa axis is broken up into some number of intervals, where the endpoints of each interval are called breakpoints. These breakpoints are then converted to knots by imposing various continuity and smoothness conditions at each interface. Given a nondecreasing knot vector t = t0 , t1 , . . . , tn+k1 , the n basis splines of order k are dened by Bi,1 (x) = Bi,k (x) = 1, ti x < ti+1 0, else

ti+k x x ti ti Bi,k1 (x) + Bi+1,k1 (x) ti+k1 ti+k ti+1

for i = 0, . . . , n 1. The common case of cubic B-splines is given by k = 4. The above recurrence relation can be evaluated in a numerically stable way by the de Boor algorithm. If we dene appropriate knots on an interval [a,b] then the B-spline basis functions form a complete set on that interval. Therefore we can expand a smoothing function as f (x) =
i

ci Bi,k (x)

given enough (xj , f (xj )) data pairs. The coecients ci can be readily obtained from a least-squares t.

Example
The following program computes a linear least squares t to data using cubic B-spline basis functions with uniform breakpoints. The data is generated from the curve y (x) = cos(x) exp(x/10) on the interval [0, 15] with Gaussian noise added. 1 2 3 4 5 6 7 8 9 10 11 12 13 #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e #i n c l u d e < s t d i o . h> < s t d l i b . h> <math . h> < g s l / g s l b s p l i n e . h> < g s l / g s l m u l t i f i t . h> < g s l / g s l r n g . h> < g s l / g s l r a n d i s t . h> < g s l / g s l s t a t i s t i c s . h>

/ number o f d a t a p o i n t s t o f i t / #d e f i n e N 200 / number o f f i t c o e f f i c i e n t s / 1

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

#d e f i n e NCOEFFS 12 / n b r e a k = n c o e f f s + 2 k = n c o e f f s 2 s i n c e k = 4 / #d e f i n e NBREAK (NCOEFFS 2 ) int main ( void ) { const s i z e t n = N; const s i z e t n c o e f f s = NCOEFFS; const s i z e t nbreak = NBREAK; size t i , j ; g s l b s p l i n e w o r k s p a c e bw ; g s l v e c t o r B ; double dy ; gsl rng r ; g s l v e c t o r c , w ; g s l v e c t o r x , y ; g s l m a t r i x X, cov ; g s l m u l t i f i t l i n e a r w o r k s p a c e mw; double c h i s q , Rsq , dof , t s s ; gsl rng env setup (); r = gsl rng alloc ( gsl rng default ); / a l l o c a t e a c u b i c b s p l i n e worksp ace ( k = 4) / bw = g s l b s p l i n e a l l o c ( 4 , nbreak ) ; B = gsl vector alloc ( ncoeffs ); x = g s l v e c t o r a l l o c (n ) ; y = g s l v e c t o r a l l o c (n ) ; X = g s l m a t r i x a l l o c (n , nc oeff s ) ; c = gsl vector alloc ( ncoeffs ); w = g s l v e c t o r a l l o c (n ) ; cov = g s l m a t r i x a l l o c ( n c o e f f s , n c o e f f s ) ; mw = g s l m u l t i f i t l i n e a r a l l o c ( n , n c o e f f s ) ; p r i n t f ( # m=0,S=0\n ) ; / t h i s i s t h e d a t a t o be f i t t e d / f o r ( i = 0 ; i < n ; ++i ) { double sigma ; double x i = ( 1 5 . 0 / (N 1 ) ) i ; double y i = c o s ( x i ) exp ( 0.1 x i ) ; sigma = 0 . 1 y i ; dy = g s l r a n g a u s s i a n ( r , sigma ) ; y i += dy ; g s l v e c t o r s e t (x , i , xi ) ; g s l v e c t o r s e t (y , i , yi ) ; 2

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

g s l v e c t o r s e t (w, i , 1 . 0 / ( sigma sigma ) ) ; p r i n t f ( %f %f \ n , xi , y i ) ; } / use uniform b r e a k p o i n t s on [ 0 , 1 5 ] / g s l b s p l i n e k n o t s u n i f o r m ( 0 . 0 , 1 5 . 0 , bw ) ; / c o n s t r u c t t h e f i t m a t r i x X / f o r ( i = 0 ; i < n ; ++i ) { double x i = g s l v e c t o r g e t ( x , i ) ; / compute B j ( x i ) f o r a l l j / g s l b s p l i n e e v a l ( xi , B, bw ) ; / f i l l i n row i o f X / f o r ( j = 0 ; j < n c o e f f s ; ++j ) { double Bj = g s l v e c t o r g e t (B, j ) ; g s l m a t r i x s e t (X, i , j , Bj ) ; } } / do t h e f i t / g s l m u l t i f i t w l i n e a r (X, w, y , c , cov , &c h i s q , mw) ; dof = n n c o e f f s ; t s s = g s l s t a t s w t s s (w>data , 1 , y>data , 1 , y> s i z e ) ; Rsq = 1 . 0 c h i s q / t s s ; f p r i n t f ( s t d e r r , c h i s q / d o f = %e , Rsq = %f \ n , c h i s q / dof , Rsq ) ; / o u t p u t t h e smoothed c u r v e / { double xi , yi , y e r r ; p r i n t f ( # m=1,S=0\n ) ; f o r ( x i = 0 . 0 ; x i < 1 5 . 0 ; x i += 0 . 1 ) { g s l b s p l i n e e v a l ( xi , B, bw ) ; g s l m u l t i f i t l i n e a r e s t (B, c , cov , &yi , &y e r r ) ; p r i n t f ( %f %f \ n , xi , y i ) ; } } gsl gsl gsl gsl rng free (r ); b s p l i n e f r e e (bw ) ; v e c t o r f r e e (B ) ; vector free (x ); 3

114 115 116 117 118 119 120 121 122

gsl gsl gsl gsl gsl gsl

vector free (y ); m a t r i x f r e e (X ) ; vector free (c ); v e c t o r f r e e (w ) ; m a t r i x f r e e ( cov ) ; m u l t i f i t l i n e a r f r e e (mw) ;

return 0 ; } / main ( ) /

Вам также может понравиться