38
999 BG Mobasseri 1 5/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

Embed Size (px)

Citation preview

Page 1: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 1 5/24/99

INTERPOLATION AND CURVE FITTING

Etter: pp. 164-184

June 16, ‘99

Page 2: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 2 5/24/99

DEFINING INTERPOLATION

Interpolation can be used in at least two ways– Subsampling (taking every other sample or

more) a dense signal and later filling it in– Generating new data where none was

previously available

Page 3: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 3 5/24/99

SUBSAMPLING

Say you have a large sound file taking too long to transmit or too much space to store.

One solution is to subsample it, send(store) the sparse data then fill in the gaps later

Page 4: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 4 5/24/99

EXAMPLE

Keep the circles and discard the rest. Later, fill in the gaps

Page 5: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 5 5/24/99

VIDEO COMPRESSION

Video captured at 30 frames/sec. contains a lot of redundancies

Keep only a fraction of the frames and interpolate between them later

This is implemented in the MPEG standard

Page 6: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 6 5/24/99

INTERPOLATION METHODS

There are 3 major interpolation techniques– linear– cubic-spline( a 3rd degree polynomial)– polynomial fitting (polynomial of arbitrary

order)

Page 7: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 7 5/24/99

LINEAR INTERPOLATION

Simplest of its kind, works on the following principle

error

interpolation

Page 8: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 8 5/24/99

HOW DOES MATLAB DO IT?

The main MATLAB’s routine for 1-D interpolation is interp1 with the following syntax– yi=interp1(x,y,xi,’method’)

(x,y) are the original coarse data. xi’s are the new finer positions to be interpolated, yi is the answer. See next

Page 9: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 9 5/24/99

Illustrating (xi,yi)

x xiy

yi

Page 10: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 10 5/24/99

WORKING WITH interp1

Want to interpolate a sinc function with samples originally located at [-4:1:4]

-4 -3 -2 -1 0 1 2 3 4-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 11: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 11 5/24/99

Linear interpolation:Try it! x=[-4:.1:4];%x-values y=[0 0 0 0 1 0 0 0 0];%coarsely sampled data at x=-4,-3,... xfiner=[-4:0.5:4];%inerpolate at -4,-3.5,-3,... yfiner=interp1(x,y,xfiner,’linear’);%interpolate at new grid

positions plot(x,y,xfiner,yfiner,'ro',xfiner,yfiner,'r-');

Page 12: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 12 5/24/99

LINEAR INTERPOLATION OF SINC

Interpolated at 0.5 intervals

-4 -3 -2 -1 0 1 2 3 4-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 13: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 13 5/24/99

DEFINING CUBIC-SPLINE

When data is interpolated by cubic spline, it means we pass a 3rd order polynomial through each pair of points

Since a polynomial is passed through a pair of pointsWe can interpolate at arbitrary fine positionsBetween the two

Page 14: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 14 5/24/99

Example

Let’s say we have passed y=x2 between (1,1) and (2,2).

We can read any intermediate y values by simply plugging in an x value

1 2

1

4

Page 15: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 15 5/24/99

CUBIC-SPLINE

A more accurate interpolation can be achieved by passing 3rd degree polynomials through coarse data

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 55

6

7

8

9

10

Page 16: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 16 5/24/99

INTERPOLATING AN AM SIGNAL

An amplitude modulated signal is s(t)=(1+ cos2πfmt)cos2πfct( )

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-2

-1.5

-1

-0.5

0

0.5

1

1.5

2AM SIGNAL,fm=2,fc=3

Page 17: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 17 5/24/99

Try it!

Evaluate the AM signal with fm=2,fc=3 in the range 0<t<1 in increments of.01

Subsample it 20:1. Using the kept data points, perform

linear and cubic-spline interpolation Compare your code with mine, next

page

Page 18: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 18 5/24/99

My code

Page 19: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 19 5/24/99

Output graph

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

original

interpolated

Page 20: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 20 5/24/99

LINEAR VS. CUBIC

spline gets closer to the actual curve

0 0.2 0.4 0.6 0.8 1-2

-1.5

-1

-0.5

0

0.5

1

1.5

2CUBIC-SPLINE vs. LINEAR INTERPOLATION

linear

originalspline

Page 21: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 21 5/24/99

FINER INTERPOLATION

Smoother interpolation via spline

0 0.2 0.4 0.6 0.8 1-2

-1.5

-1

-0.5

0

0.5

1

1.5

2FINER CUBIC-SPLINT INTERP.

Page 22: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 22 5/24/99

DIRECT READ-OFF interp1(x,y,2.5,’spline’)

returns spline interpolated value of 6.1 at 2.5

2.5

6.1

x=0:5; y=[5 8 6 7 9 8]; xi=0:.1:5; ylin=interp1(x,y,xi); ys=interp1(x,y,xi,'spline'); point=interp1(x,y,2.5,'spline')

Page 23: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 23 5/24/99

LEAST SQUARES CURVE FITTING

Linear and cubic spline interpolations fit curves constrained to go through the data points

A curve fitted using least squares may not pass through any data point but it will be “close” to all of them in the “least squares” sense

Page 24: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 24 5/24/99

LEAST SQUARES SENSE

MSE =yk −ˆ yk( )

2

k=1

N∑

N

data

Fit a line that on average is closets to all data points

Page 25: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 25 5/24/99

LEAST SQUARES OBJECTIVE

Find a function,e.g. a polynomial of whatever order, that minimizes the mean square error

MATLAB does this through polynomial regression

Page 26: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 26 5/24/99

DIFFERENCES WITH CUBIC SPLINE

Both are polynomials but cubic splines are 3rd order polynomials.

The big difference is that cubic spline fits separate 3rd degree polynomials per segment

Least squares fits a single polynomial through all data points

Page 27: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 27 5/24/99

DEFINING A POLYNOMIALEtter: pp. 78-86

An Nth degree polynomial is specified by N+1 coefficients

If there are N+1 data points, an Nth degree polynomial will pass through all of them

f x( ) =a0xN +a1x

N−1 +... +aN−1x+aN

Page 28: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 28 5/24/99

Example of polynomials

It takes a first degree polynomial, a straight line, to connect two points

It takes a 2nd degree polynomial to connect 3 points

Page 29: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 29 5/24/99

polyfit FUNCTION

To fit an nth degree polynomial to (x,y) data use– p=polyfit(x,y,n)

polyfit returns a vector p of n+1 coefficients in decreasing powers of x.

So if

then p=[ao,a1,a2,a3,…,aN]

f x( ) =a0xN +a1x

N−1 +... +aN−1x+aN

Page 30: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 30 5/24/99

Evaluating and Plotting the Fitted Polynomial:polyval

Once polynomial is specified through the vector p, it can be evaluated directly using– y=polyval(p,x)

coarse x valuesvector of polynomial coefficients

Page 31: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 31 5/24/99

Example using polyfit

Let x=[0 1 2 3 4 5] and y= [5 8 6 7 9 8] be the coarse data points. This is how to fit a 4th order polynomial

p=polyfit(x,y,4);%p is the coeff. vector

now let’s evaluate the polynomial at finer positions given by xfine=[0:0.1:5]

yfine=polyval(p,xfine);%plot yfine to see

Page 32: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 32 5/24/99

RESULT

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 55

5.5

6

6.5

7

7.5

8

8.5

9

9.5

10

data

Fitted polynomial does not passthrough any data point

Page 33: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 33 5/24/99

Comparing Interpolations

In the following slides we start with the following data points– x=[0 1 2 3 4 5];– y=[5 8 6 7 9 8];

We will then interpolate along x in increments of 0.1 using progressively larger order polynomials

Page 34: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 34 5/24/99

APPLYING polyfit

Let’s fit a first degree y=mx+h to data. We’ll get m=0.54,h=5.8

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 55

6

7

8

9

10

Page 35: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 35 5/24/99

3RD DEGREE POLYNOMIAL

Coefficients are [0.037 -0.349 1.407 5.460]

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 55

6

7

8

9

10

Page 36: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 36 5/24/99

4th DEGREE POLYNOMIAL

coeff=[-0.2500 2.5370 -8.0278 8.5503 5.0317]

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 55

5.5

6

6.5

7

7.5

8

8.5

9

9.5

10

Page 37: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 37 5/24/99

5th DEGREE POLYNOMIAL

Expect the polynomial to pass through all the points (why?)

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 55

5.5

6

6.5

7

7.5

8

8.5

9

9.5

Page 38: ©1999 BG Mobasseri 15/24/99 INTERPOLATION AND CURVE FITTING Etter: pp. 164-184 June 16, ‘99

©1999 BG Mobasseri 38 5/24/99

Homework

Take the data on page 33 and interpolate it in increments of 0.1 using– Linear interpolation– Cubic spline– Polynomial of 3rd degree– Superimpose your plots and show how they

compare