Skip to article frontmatterSkip to article content

Trigonometric interpolation

Up to this point, all of our global approximating functions have been polynomials. While they are versatile and easy to work with, they are not always the best choice.

Suppose we want to approximate a function ff that is periodic, with one period represented by the standard interval [1,1][-1,1]. Mathematically, periodicity means that f(x+2)=f(x)f(x+2)=f(x) for all real xx. We could use polynomials to interpolate or project ff. However, it seems more reasonable to replace polynomials by functions that are also periodic.

It turns out that trigonometric interpolation allows us to return to equally spaced nodes without any problems. We therefore define N=2n+1N=2n+1 equally spaced nodes inside the interval [1,1][-1,1] by

tk=2kN,k=n,,n. t_k = \frac{2k}{N}, \quad k=-n,\ldots,n.

The formulas in this section require some minor but important adjustments if NN is even instead. We have modified our standard indexing scheme here to make the symmetry within [1,1][-1,1] about x=0x=0 more transparent. Note that the endpoints ±1\pm 1 are not among the nodes.

As usual, we have sample values yn,,yny_{-n},\ldots,y_n, perhaps representing values of a function f(x)f(x) at the nodes. We also now assume that the sample values can be extended periodically forever in both directions, so that yk+mN=yky_{k+mN}=y_k for any integer mm.

9.5.1Cardinal functions

We can explicitly state the cardinal function basis for equispaced trigonometric interpolation. It starts with

τ(x)=2N(12+cosπx+cos2πx++cosnπx)=sin(Nπx/2)Nsin(πx/2).\tau(x) = \frac{2}{N} \left( \frac{1}{2} + \cos \pi x + \cos 2\pi x + \cdots + \cos n\pi x\right) = \frac{\sin(N\pi x/2)}{N\sin(\pi x/2)}.

You can directly check the following facts. (See Exercise 3.)

Because the functions τn,,τn\tau_{-n},\ldots,\tau_n form a cardinal basis, the coefficients of the interpolant are just the sampled function values, i.e., the interpolant of points (tk,yk)(t_k,y_k) is

p(x)=k=nnykτk(x).p(x) = \sum_{k=-n}^n y_k \tau_k(x).

The convergence of a trigonometric interpolant is spectral, i.e., exponential as a function of NN in the max-norm.

9.5.2Implementation

Function 9.5.1 is an implementation of trigonometric interpolation based on (9.5.4). The function accepts an NN-vector of equally spaced nodes. Although we have not given the formulas above, the case of even NN is included in the code.

9.5.3Fast Fourier transform

Although the cardinal form of the interpolant is useful and stable, there is a fundamental alternative. It begins with an equivalent complex form of the trigonometric interpolant (9.5.1),

p(x)=k=nnckeikπx. p(x) = \sum_{k=-n}^n c_k e^{ik\pi x}.

The connection is made through Euler’s formula,

eiθ=cos(θ)+isin(θ), e^{i\theta} = \cos(\theta) + i\sin(\theta),

and the resultant identities

cosθ=eiθ+eiθ2,sinθ=eiθeiθ2i. \cos \theta = \frac{e^{i \theta}+e^{-i\theta}}{2}, \qquad \sin \theta = \frac{e^{i \theta}-e^{-i\theta}}{2i}.

Specifically, we have

ck={a02,k=0,12(ak+ibk),k>0,ck,k<0.c_k = \begin{cases} \frac{a_0}{2}, & k=0, \\[1mm] \frac{1}{2}(a_k + i b_k), & k> 0, \\[1mm] \overline{c_{-k}}, & k < 0. \end{cases}

While working with an all-real formulation seems natural when the data are real, the complex-valued version leads to more elegant formulas and is standard.

The N=2n+1N=2n+1 coefficients ckc_k are determined by interpolation nodes at the NN nodes within [1,1][-1,1]. By evaluating the complex exponential functions at these nodes, we get the N×NN\times N linear system

Fc=y,F=[eisπtr]r=n,,n,s=n,,n,\mathbf{F}\mathbf{c} = \mathbf{y}, \qquad \mathbf{F} = \bigl[ e^{\,is\pi t_r} \bigr]_{\, r=-n,\ldots,n,\, s=-n,\ldots,n,}

to be solved for the coefficients. Up to a scalar factor, the matrix F\mathbf{F} is unitary, which implies that the system can be solved in O(N2)O(N^2) operations simply by a matrix-vector multiplication.

However, one of the most important (though not entirely original) algorithmic observations of the 20th century was that the linear system can be solved in just O(NlogN)O(N\log N) operations by an algorithm now known as the fast Fourier transform, or FFT.

The FFTW package provides a function fft to perform this transform, but its conventions are a little different from ours. Instead of nodes in (1,1)(-1,1), it expects the nodes to be defined in [0,2)[0,2), and it returns the trig polynomial coefficients in the order

[c0,c1,cn,cn,c1].\begin{bmatrix} c_0, & c_1, & \cdots & c_n, & c_{-n}, & \cdots & c_{-1} \end{bmatrix}.

The theoretical and computational aspects of Fourier analysis are vast and far-reaching. We have given only the briefest of introductions.

9.5.4Exercises

  1. ⌨ Each of the following functions is 2-periodic. Use Function 9.5.1 to plot the function together with its trig interpolants with n=3,6,9n=3,6,9. Then, for n=2,3,,30n=2,3,\ldots,30, compute the max-norm error in the trig interpolant by sampling at 1000 or more points, and make a convergence plot on a semi-log scale.

    (a) f(x)=esin(2πx)f(x) = e^{\sin (2\pi x)}\qquad (b) f(x)=log[2+sin(3πx)]f(x) = \log [2+ \sin (3 \pi x ) ]\qquad (c) f(x)=cos12[π(x0.2)]f(x) = \cos^{12}[\pi (x-0.2)]

  2. (a) ✍ Show that the functions sin(rπx)\sin(r\pi x) and sin(sπx)\sin(s\pi x) are identical at all of the nodes given in (9.5.2) if rs=mNr-s=mN for an integer mm. This important fact is called aliasing, and it implies that only finitely many frequencies can be distinguished on a fixed node set.

    (b) ⌨ Demonstrate part (a) with a graph for the case N=11N=11, s=2s=2, r=9r=-9. Specifically, plot the two functions on one graph, and plot points to show that they intersect at all of the interpolation nodes.

  1. ✍ Verify that the cardinal function given in Equation (9.5.3) is (a) 2-periodic, (b) satisfies τ(tk)=0\tau(t_k)=0 for k0k\neq 0 at the nodes (9.5.2), and (c) satisfies limx0τ(x)=1\lim_{x\to0}\tau(x)=1.

  2. ✍ Prove the equality of the two expressions in (9.5.3). (Hint: Set z=eiπx/2z=e^{i\pi x/2} and rewrite the sum using zz by applying Euler’s identity.)

  3. ⌨ Spectral convergence is predicated on having infinitely many continuous derivatives. At the other extreme is a function with a jump discontinuity. Trigonometric interpolation across a jump leads to a lack of convergence altogether, a fact famously known as the Gibbs phenomenon.

    (a) Define f(x) = sign(x+eps()). This function jumps from -1 to 1 at x=ϵmachx=-\epsilon_\text{mach}. Plot the function over 0.05x0.15-0.05\le x \le 0.15.

    (b) Let n=30n=30 and N=2n+1N=2n+1. Using Function 9.5.1, add a plot of the trigonometric interpolant to ff to the graph from part (a).

    (c) Repeat part (b) for n=80n=80 and n=180n=180.

    (d) You should see that the interpolants overshoot and oscillate near the step. The widths of the overshoots decrease with nn but the heights approach a limiting value. By zooming in to the graph, find the height of the overshoot to two decimal places.

  4. ⌨ Let f(x)=xf(x)=x. Plot ff and its trigonometric interpolants of length N=2n+1N=2n+1 for n=6,20,50n=6,20,50 over 1x1-1\le x \le 1. What feature of the function is causing large errors?