from numpy import *
from scipy import linalg
from scipy.linalg import norm
from matplotlib.pyplot import *
from prettytable import PrettyTable
from timeit import default_timer as timer
import sys
sys.path.append('fncbook/')
import fncbook as FNC
# This (optional) block is for improving the display of plots.
# from IPython.display import set_matplotlib_formats
# set_matplotlib_formats("svg","pdf")
# %config InlineBackend.figure_format = 'svg'
rcParams["figure.figsize"] = [7, 4]
rcParams["lines.linewidth"] = 2
rcParams["lines.markersize"] = 4
rcParams['animation.html'] = "jshtml" # or try "html5"
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 f that is periodic, with one period represented by the standard interval [−1,1]. Mathematically, periodicity means that f(x+2)=f(x) for all real x. We could use polynomials to interpolate or project f. 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+1 equally spaced nodes inside the interval [−1,1] by
The formulas in this section require some minor but important adjustments if N is even instead. We have modified our standard indexing scheme here to make the symmetry within [−1,1] about x=0 more transparent. Note that the endpoints ±1 are not among the nodes.
As usual, we have sample values y−n,…,yn, perhaps representing values of a function 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=yk for any integer m.
You can directly check the following facts. (See Exercise 9.5.3.)
Because the functions τ−n,…,τn form a cardinal basis, the coefficients of the interpolant are just the sampled function values, i.e., the interpolant of points (tk,yk) is
Function 9.5.1 is an implementation of trigonometric interpolation based on (9.5.4). The function accepts an N-vector of equally spaced nodes. Although we have not given the formulas above, the case of even N is included in the code.
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),
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+1 coefficients ck are determined by interpolation nodes at the N nodes within [−1,1]. By evaluating the complex exponential functions at these nodes, we get the N×N linear system
to be solved for the coefficients. Up to a scalar factor, the matrix F is unitary, which implies that the system can be solved in O(N2) 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) 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), it expects the nodes to be defined in [0,2), and it returns the trig polynomial coefficients in the order