Finding Coefficients of Fourier Series

Rifat Aras, Duc T. Nguyen
Old Dominion University
United States of America
dnguyen@odu.edu
Website: http://numericalmethods.eng.usf.edu
Version: Mathematica 8; Last Revised: September, 20, 2011

Initialization

Clearing the definitions of all symbols in the current context:

ClearAll[Evaluate[Context[]<>"*"]];
coefA0={};
coefAk={};
coefBk={};
nSegments=1;
period=2*Pi;

Introduction

The following worksheet demonstrates how to compute unknown Fourier coefficients of a continuous Fourier series representation of a function with period T. The user will choose the number of segments that form the function and enter the period of the function along with the symbolic equations of each segment. The final input of the user is the bounds of the segments such that the difference between the last segment and the first segment shall be equal to the period (T) of the function.

Section 1: Function Definitions

Find the first coefficient A0. Input parameters are: period of the function, number of segments that compose the function, equations of the segments, boundary values, and symbolic representation of the independent variable.

findA0[iperiod_, inSegments_, iequations_, its_, ivar_]:=
(1/iperiod)*
(Total[Table[Integrate[iequations[[i]],{ivar,its[[i]],its[[i+1]]}],{i,inSegments}]]);

Find the coefficient Ak. Input parameters are: period of the function, number of segments that compose the function, equations of the segments, boundary values, symbolic representation of the independent variable, and the index of the coefficient that the user wants to compute.

findAk[iperiod_, inSegments_, iequations_, its_, ivar_, k_]:=
(2/iperiod)*
(Total[Table[Integrate[iequations[[i]]*Cos[k*2*Pi*(1/iperiod)*ivar],{ivar,its[[i]],its[[i+1]]}],{i,inSegments}]]);

Find the coefficient Bk. Input parameters are: period of the function, number of segments that compose the function, equations of the segments, boundary values, symbolic representation of the independent variable, and the index of the coefficient that the user wants to compute.

findBk[iperiod_, inSegments_, iequations_, its_, ivar_, k_]:=
(2/iperiod)*
(Total[Table[Integrate[iequations[[i]]*Sin[k*2*Pi*(1/iperiod)*ivar],{ivar,its[[i]],its[[i+1]]}],{i,inSegments}]]);

Call the previously defined functions and generate the output coefficients / print them. Input parameters are: period of the function, number of segments that compose the function, equations of the segments, boundary values, and the symbolic representation of the independent variable.

findCoefficients[iperiod_, inSegments_, iequations_, its_, ivar_]:=
{
Print["This code was written by Rifat Aras (rifat.aras@gmail.com) with the algorithm specified by Duc T. Nguyen (dnguyen@odu.edu) and freely released to the public domain."],
Print["When entering equations and other variables, follow Mathematica (version 8) rules.\ne.g. Sin[2*t], E^(-t), and so on.\n"],
Print["\nThe Fourier coefficients for a0, ak, and bk are given as (for k = 1,2,...,8):\n"],
coefA0=findA0[iperiod, inSegments, iequations, its, ivar];,
Print["a0 = ",ScientificForm[coefA0]," or ",N[coefA0]],Do[{tempAk=findAk[iperiod, inSegments, iequations, its, ivar,i];,Print["a"<>ToString[i]<>" = ",ScientificForm[tempAk]," or ",N[tempAk]],AppendTo[coefAk,tempAk]},{i,8}],
Do[{tempBk=findBk[iperiod, inSegments, iequations, its, ivar,i];,
Print["b"<>ToString[i]<>" = ",ScientificForm[tempBk]," or ",N[tempBk]],AppendTo[coefBk,tempBk]},{i,8}]
};

Section 2: Graphical User Interface Definition

userInterface = Panel[
Dynamic[
Column[
{
"Discrete Fourier Series",
Row[{"Number of segments:",SetterBar[Dynamic[nSegments],Range[4]]}],
Row[{"Period (T):",InputField[Dynamic[period]]}],
Column[{
Row[{"Segment 1:", InputField[Dynamic[equation1]]}],
Row[{"Segment 2:", InputField[Dynamic[equation2],Enabled->If[nSegments>1,True,False]]}],
Row[{"Segment 3:", InputField[Dynamic[equation3],Enabled->If[nSegments>2,True,False]]}],
Row[{"Segment 4:", InputField[Dynamic[equation4],Enabled->If[nSegments>3,True,False]]}]}
],
Column[{
Row[{"t0:",InputField[Dynamic[t0]]}],
Row[{"t1:",InputField[Dynamic[t1]]}],
Row[{"t2:",InputField[Dynamic[t2],Enabled->If[nSegments>1,True,False]]}],
Row[{"t3:",InputField[Dynamic[t3],Enabled->If[nSegments>2,True,False]]}],
Row[{"t4:",InputField[Dynamic[t4],Enabled->If[nSegments>3,True,False]]}]
}],
Button["Compute Discrete Fourier Series",
findCoefficients[period,
nSegments,
Table[Symbol["equation"<>ToString[i]],{i,nSegments}],
Table[Symbol["t"<>ToString[i-1]],{i,nSegments+1}],
t]
]
}
]
]
];

Section 3: Dynamic Module for Coefficient Computation

Dynamic[userInterface]

Section 4: Graphical Representation

segmentedPlots =Table[Plot[Symbol["equation"<>ToString[i]],{t,Symbol["t"<>ToString[i-1]],Symbol["t"<>ToString[i]]}],{i,1,nSegments}];
fourierApproximation[k_]:=coefA0+Sum[coefAk[[i]]*Cos[i*2*Pi*(1/period)*t]+coefBk[[i]]*Sin[i*2*Pi*(1/period)*t],{i,1,k}];

Manipulate[Show[segmentedPlots,Plot[fourierApproximation[k],{t,t0,Symbol["t"<>ToString[nSegments]]},PlotStyle->Red],PlotRange->Automatic],{k,1,8,1}]

Conclusion

This worksheet shows how to compute the unknown coefficients of continuous Fourier series. The effect of the number of terms in the Fourier series is also visualized interactively.

References

CEE 305 course notes by Duc T. Nguyen, http://www.lions.odu.edu/~skadi002/fft1.1.pdf

Legal Notice: The copyright for this application is owned by the author(s). Neither Wolfram Research nor the author(s) are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.