CURVE FITTING WITH SINUSOIDAL FUNCTIONS

clear-cut convention for choosing either function, and in any case, the results will be iden- tical because the two functions are simply offset in time by π2 radians. For this chapter, we will use the cosine, which can be expressed generally as f t = A + C 1 cosω t + θ 16.2 Inspection of Eq. 16.2 indicates that four parameters serve to uniquely characterize the sinusoid Fig. 16.2a: • The mean value A sets the average height above the abscissa. • The amplitude C 1 specifies the height of the oscillation. • The angular frequency ω characterizes how often the cycles occur. • The phase angle or phase shift θ parameterizes the extent to which the sinusoid is shifted horizontally. yt θ C 1 t , s ω t , rad 1 1 π 2π 3π 2 2 A B 1 sin ω t A 1 cos ω t A T a b 1 ⫺1 2 FIGURE 16.2 a A plot of the sinusoidal function yt = A + C 1 cos␻ t + ␪. For this case, A = 1.7, C 1 = 1, ␻ = 2␲T = 2␲1.5 s, and ␪ = ␲3 radians = 1.0472 = 0.25 s. Other parameters used to describe the curve are the frequency f = ␻ 2␲, which for this case is 1 cycle1.5 s = 0.6667 Hz and the period T = 1.5 s. b An alternative expression of the same curve is yt = A + A 1 cos␻ t + B 1 sin␻ t. The three components of this function are depicted in b, where A 1 = 0.5 and B 1 = –0.866. The summation of the three curves in b yields the single curve in a. Note that the angular frequency in radianstime is related to the ordinary frequency f in cyclestime 1 by ω = 2π f 16.3 and the ordinary frequency in turn is related to the period T by f = 1 T 16.4 In addition, the phase angle represents the distance in radians from t = 0 to the point at which the cosine function begins a new cycle. As depicted in Fig. 16.3a, a negative value is referred to as a lagging phase angle because the curve cosω t − θ begins a new cycle ␪ radians after cosω t . Thus, cosω t − θ is said to lag cosω t . Conversely, as in Fig. 16.3b, a positive value is referred to as a leading phase angle. Although Eq. 16.2 is an adequate mathematical characterization of a sinusoid, it is awkward to work with from the standpoint of curve fitting because the phase shift is included in the argument of the cosine function. This deficiency can be overcome by invoking the trigonometric identity: C 1 cosω t + θ = C 1 [cosω t + θcosθ − sinω t + θsinθ] 16.5 16.1 CURVE FITTING WITH SINUSOIDAL FUNCTIONS 383 cos ω t cos ω t t a b t θ cos ω t π 2 ⫺ cos ω t π 2 ⫹ FIGURE 16.3 Graphical depictions of a a lagging phase angle and b a leading phase angle. Note that the lagging curve in a can be alternatively described as cos␻ t + 3␲2. In other words, if a curve lags by an angle of ␣, it can also be represented as leading by 2␲ – ␣. 1 When the time unit is seconds, the unit for the ordinary frequency is a cycles or Hertz Hz. Substituting Eq. 16.5 into Eq. 16.2 and collecting terms gives Fig. 16.2b f t = A + A 1 cosω t + B 1 sinω t 16.6 where A 1 = C 1 cosθ B 1 = −C 1 sinθ 16.7 Dividing the two parts of Eq. 16.7 gives θ = arctan − B 1 A 1 16.8 where, if A 1 ⬍ 0, add ␲ to ␪. Squaring and summing Eq. 16.7 leads to C 1 = A 2 1 + B 2 1 16.9 Thus, Eq. 16.6 represents an alternative formulation of Eq. 16.2 that still requires four pa- rameters but that is cast in the format of a general linear model [recall Eq. 15.7]. As we will discuss in the next section, it can be simply applied as the basis for a least-squares fit. Before proceeding to the next section, however, we should stress that we could have employed a sine rather than a cosine as our fundamental model of Eq. 16.2. For example, f t = A + C 1 sinω t + δ could have been used. Simple relationships can be applied to convert between the two forms: sinω t + δ = cos ω t + δ − π 2 and cosω t + δ = sin ω t + δ + π 2 16.10 In other words, ␪ = ␦ ⫺ ␲兾2. The only important consideration is that one or the other format should be used consistently. Thus, we will use the cosine version throughout our discussion.

16.1.1 Least-Squares Fit of a Sinusoid

Equation 16.6 can be thought of as a linear least-squares model: y = A + A 1 cosω t + B 1 sinω t + e 16.11 which is just another example of the general model [recall Eq. 15.7] y = a z + a 1 z 1 + a 2 z 2 + · · · + a m z m + e where z = 1, z 1 = cosω t, z 2 = sinω t , and all other z’s = 0. Thus, our goal is to determine coefficient values that minimize S r = N i =1 {y i − [A + A 1 cosω t + B 1 sinω t ] } 2 The normal equations to accomplish this minimization can be expressed in matrix form as [recall Eq. 15.10] N cosω t sinω t cosω t cos 2 ω t cosω t sinω t sinω t cosω t sinω t sin 2 ω t A B 1 B 1 = y y cosω t y sinω t 16.12 These equations can be employed to solve for the unknown coefficients. However, rather than do this, we can examine the special case where there are N observations equi- spaced at intervals of t and with a total record length of T = N − 1t . For this situa- tion, the following average values can be determined see Prob. 16.3: sinω t N = 0 cosω t N = 0 sin 2 ω t N = 1 2 cos 2 ω t N = 1 2 cosω t sinω t N = 0 16.13 Thus, for equispaced points the normal equations become N N 2 N 2 A B 1 B 2 = y y cosω t y sinω t The inverse of a diagonal matrix is merely another diagonal matrix whose elements are the reciprocals of the original. Thus, the coefficients can be determined as A B 1 B 2 = 1N 2N 2N y y cosω t y sinω t or A = y N 16.14 A 1 = 2 N y cosω t 16.15 B 1 = 2 N y sinω t 16.16 Notice that the first coefficient represents the function’s average value. EXAMPLE 16.1 Least-Squares Fit of a Sinusoid Problem Statement. The curve in Fig. 16.2a is described by y = 1.7 + cos4.189t + 1.0472. Generate 10 discrete values for this curve at intervals of t = 0.15 for the range t = 0 to 1.35. Use this information to evaluate the coefficients of Eq. 16.11 by a least- squares fit. 16.1 CURVE FITTING WITH SINUSOIDAL FUNCTIONS 385 Solution. The data required to evaluate the coefficients with ω = 4.189 are t y y cos ω t y sin ω t 2.200 2.200 0.000 0.15 1.595 1.291 0.938 0.30 1.031 0.319 0.980 0.45 0.722 ⫺0.223 0.687 0.60 0.786 ⫺0.636 0.462 0.75 1.200 ⫺1.200 0.000 0.90 1.805 ⫺1.460 ⫺1.061 1.05 2.369 ⫺0.732 ⫺2.253 1.20 2.678 0.829 ⫺2.547 1.35 2.614 2.114 ⫺1.536 ⌺ = 17.000 2.502 ⫺4.330 These results can be used to determine [Eqs. 16.14 through 16.16] A = 17.000 10 = 1.7 A 1 = 2 10 2.502 = 0.500 B 1 = 2 10 −4.330 = −0.866 Thus, the least-squares fit is y = 1.7 + 0.500 cosω t − 0.866 sinω t The model can also be expressed in the format of Eq. 16.2 by calculating [Eq. 16.8] θ = arctan −0.866 0.500 = 1.0472 and [Eq. 16.9] C 1 = 0.5 2 + −0.866 2 = 1.00 to give y = 1.7 + cosω t + 1.0472 or alternatively, as a sine by using [Eq. 16.10] y = 1.7 + sinω t + 2.618 The foregoing analysis can be extended to the general model f t = A + A 1 cosω t + B 1 sinω t + A 2 cos2ω t + B 2 sin2ω t + · · · + A m cosmω t + B m sinmω t where, for equally spaced data, the coefficients can be evaluated by A = y N A j = 2 N y cos j ω t B j = 2 N y sin j ω t ⎫ ⎪ ⎬ ⎪ ⎭ j = 1, 2, . . . , m Although these relationships can be used to fit data in the regression sense i.e., N ⬎ 2m + 1, an alternative application is to employ them for interpolation or collocation—that is, to use them for the case where the number of unknowns 2m + 1 is equal to the number of data points N. This is the approach used in the continuous Fourier series, as described next.

16.2 CONTINUOUS FOURIER SERIES

In the course of studying heat-flow problems, Fourier showed that an arbitrary periodic function can be represented by an infinite series of sinusoids of harmonically related frequencies. For a function with period T, a continuous Fourier series can be written f t = a + a 1 cosω t + b 1 sinω t + a 2 cos2ω t + b 2 sin2ω t + · · · or more concisely, f t = a + ∞ k =1 [a k coskω t + b k sinkω t ] 16.17 where the angular frequency of the first mode ω = 2πT is called the fundamental frequency and its constant multiples 2ω , 3ω , etc., are called harmonics. Thus, Eq. 16.17 expresses ft as a linear combination of the basis functions: 1, cosω t, sinω t, cos2ω t, sin2ω t, . . . . The coefficients of Eq. 16.17 can be computed via a k = 2 T T f t coskω t dt 16.18 and b k = 2 T T f t sinkω t dt 16.19 for k = 1, 2, . . . and a = 1 T T f t dt 16.20 EXAMPLE 16.2 Continuous Fourier Series Approximation Problem Statement. Use the continuous Fourier series to approximate the square or rec- tangular wave function Fig. 16.1a with a height of 2 and a period T = 2πω : f t = −1 −T2 t 1 −T4 t −1 T 4 t −T4 T 4 T 2 16.2 CONTINUOUS FOURIER SERIES 387 Solution. Because the average height of the wave is zero, a value of a = 0 can be obtained directly. The remaining coefficients can be evaluated as [Eq. 16.18] a k = 2 T T 2 −T2 f t coskω t dt = 2 T − −T4 −T2 coskω t dt + T 4 −T4 coskω t dt − T 2 T 4 coskω t dt The integrals can be evaluated to give a k = 4kπ for k = 1, 5, 9, ... −4kπ for k = 3, 7, 11, ... for k = even integers cos ω t 4 π cos 3ω t 4 3π cos 5ω t 4 5π a b c FIGURE 16.4 The Fourier series approximation of a square wave. The series of plots shows the summation up to and including the a first, b second, and c third terms. The individual terms that were added or subtracted at each stage are also shown.