<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="m0041">

  <name>Fourier Series Approximation of a Square Wave</name>

  <metadata>
  <md:version>2.15</md:version>
  <md:created>2000/07/24</md:created>
  <md:revised>2007/07/15 16:49:07.024 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="mrshawn">
      <md:firstname>Shawn</md:firstname>
      
      <md:surname>Stewart</md:surname>
      <md:email>mrshawn@alumni.rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="bfite">
      <md:firstname>Benjamin</md:firstname>
      
      <md:surname>Fite</md:surname>
      <md:email>bfite@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>approximation by Fourier series</md:keyword>
    <md:keyword>equality of Fourier series</md:keyword>
    <md:keyword>Fourier series</md:keyword>
    <md:keyword>Gibb's phenomenon</md:keyword>
    <md:keyword>mean square equality</md:keyword>
    <md:keyword>pointwise equality</md:keyword>
    <md:keyword>rms</md:keyword>
    <md:keyword>square wave</md:keyword>
    <md:keyword>step function</md:keyword>
  </md:keywordlist>

  <md:abstract>Shows how to use Fourier series to approximate a square wave, as opposed to the sinusoidal waves seen previously.</md:abstract>
</metadata>
  <content>


    <para id="introduction">
      The Fourier series representation of a signal, as expressed for
      a square wave by

      <equation id="seriesrepresent">
	<m:math>
	  <m:apply>
	    <m:eq/>
	    <m:apply>
	      <m:ci type="fn">s</m:ci>
	      <m:ci>t</m:ci>
	    </m:apply>
	    <m:apply>
	      <m:plus/>
	      <m:ci>
		<m:msub>
		  <m:mi>a</m:mi>
		  <m:mn>0</m:mn>
		</m:msub>
	      </m:ci>
	      <m:apply>
		<m:sum/>
		<m:bvar>
		  <m:ci>k</m:ci>
		</m:bvar>
		<m:lowlimit>
		  <m:cn>1</m:cn>
		</m:lowlimit>
		<m:uplimit>
		  <m:infinity/>
		</m:uplimit>
		<m:apply>
		  <m:times/>
		  <m:ci>
		    <m:msub>
		      <m:mi>a</m:mi>
		      <m:mi>k</m:mi>
		    </m:msub>
		  </m:ci>
		  <m:apply>
		    <m:cos/>
		    <m:apply>
		      <m:divide/>
		      <m:apply>
			<m:times/>
			<m:cn>2</m:cn>
			<m:pi/>
			<m:ci>k</m:ci>
			<m:ci>t</m:ci>
		      </m:apply>
		      <m:ci>T</m:ci>
		    </m:apply>
		  </m:apply>
		</m:apply>
	      </m:apply>
	      <m:apply>
		<m:sum/>
		<m:bvar>
		  <m:ci>k</m:ci>
		</m:bvar>
		<m:lowlimit>
		  <m:cn>1</m:cn>
		</m:lowlimit>
		<m:uplimit>
		  <m:infinity/>
		</m:uplimit>
		<m:apply>
		  <m:times/>
		  <m:ci>
		    <m:msub>
		      <m:mi>b</m:mi>
		      <m:mi>k</m:mi>
		    </m:msub>
		  </m:ci>
		  <m:apply>
		    <m:sin/>
		    <m:apply>
		      <m:divide/>
		      <m:apply>
			<m:times/>
			<m:cn>2</m:cn>
			<m:pi/>
			<m:ci>k</m:ci>
			<m:ci>t</m:ci>
		      </m:apply>
		      <m:ci>T</m:ci>
		    </m:apply>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:apply>
	  </m:apply>
	</m:math>
      </equation>

      says that the left and right sides are "equal". We need to
      investigate equality through an example.
    </para>

    <example id="onlyexample">
      <figure id="fourpointfour">
	<name>Fourier series approximation of a square wave</name>
	<media type="image/png" src="fourier4.png"/>
	<caption>
	  Fourier series approximation to
	  <m:math>
	    <m:apply>
	      <m:ci type="fn">sq</m:ci>
	      <m:ci>t</m:ci>
	    </m:apply>
	  </m:math>.  
	  The number of terms in the Fourier sum is indicated in each
	  plot, and the square wave is shown as a dashed line over two
	  periods.
	</caption>
      </figure>

      <para id="exampleparagraph">
	Let's find the spectrum of the square wave
	<m:math>
	  <m:apply>
	    <m:ci type="fn">sq</m:ci>
	    <m:ci>t</m:ci>
	  </m:apply>
	</m:math>. 	
	The expressions for the Fourier coefficients have the common
	form

	<equation id="fouriercoefficients"><m:math>
	    <m:apply>
	      <m:eq/>
	      <m:vector>
		<m:ci>
		  <m:msub>
		    <m:mi>a</m:mi>
		    <m:mi>k</m:mi>
		  </m:msub>
		</m:ci>
		<m:ci>
		  <m:msub>
		    <m:mi>b</m:mi>
		    <m:mi>k</m:mi>
		  </m:msub>
		</m:ci>
	      </m:vector>
	      <m:apply>
		<m:minus/>
		<m:apply>
		  <m:times/>
		  <m:apply>
		    <m:divide/>
		    <m:cn>2</m:cn>
		    <m:ci>T</m:ci>
		  </m:apply>
		  <m:apply>
		    <m:int/>
		    <m:bvar>
		      <m:ci>t</m:ci>
		    </m:bvar>
		    <m:lowlimit>
		      <m:cn>0</m:cn>
		    </m:lowlimit>
		    <m:uplimit>
		      <m:apply>
			<m:divide/>
			<m:ci>T</m:ci>
			<m:cn>2</m:cn>
		      </m:apply>
		    </m:uplimit>
		    <m:vector>
		      <m:apply><m:cos/>
                        <m:apply><m:divide/>
			<m:apply>
			  <m:times/>
			  <m:cn>2</m:cn>
			  <m:pi/>
			  <m:ci>k</m:ci>
			  <m:ci>t</m:ci>
			</m:apply>
			<m:ci>T</m:ci>
		      </m:apply>
                      </m:apply>
		      <m:apply><m:sin/>
			<m:apply><m:divide/>
			<m:apply>
			  <m:times/>
			  <m:cn>2</m:cn>
			  <m:pi/>
			  <m:ci>k</m:ci>
			  <m:ci>t</m:ci>
			</m:apply>
			<m:ci>T</m:ci>
		      </m:apply>
                      </m:apply>
		    </m:vector>
		  </m:apply>
		</m:apply>
		<m:apply>
		  <m:times/>
		  <m:apply>
		    <m:divide/>
		    <m:cn>2</m:cn>
		    <m:ci>T</m:ci>
		  </m:apply>
		  <m:apply>
		    <m:int/>
		    <m:bvar>
		      <m:ci>t</m:ci>
		    </m:bvar>
		    <m:lowlimit>
		      <m:apply>
			<m:divide/>
			<m:ci>T</m:ci>
			<m:cn>2</m:cn>
		      </m:apply>
		    </m:lowlimit>
		    <m:uplimit>
		      <m:ci>T</m:ci>
		    </m:uplimit>
		    <m:vector>
		      <m:apply><m:cos/>
			<m:apply><m:divide/>
			<m:apply>
			  <m:times/>
			  <m:cn>2</m:cn>
			  <m:pi/>
			  <m:ci>k</m:ci>
			  <m:ci>t</m:ci>
			</m:apply>
			<m:ci>T</m:ci>
		      </m:apply>
                      </m:apply>
		      <m:apply><m:sin/>
			<m:apply><m:divide/>
			<m:apply>
			  <m:times/>
			  <m:cn>2</m:cn>
			  <m:pi/>
			  <m:ci>k</m:ci>
			  <m:ci>t</m:ci>
			</m:apply>
			<m:ci>T</m:ci>
		      </m:apply>
                      </m:apply>
		    </m:vector>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:apply>
	  </m:math>
	</equation>

	The cosine coefficients
	<m:math>
	  <m:ci>
	    <m:msub>
	      <m:mi>a</m:mi>
	      <m:mi>k</m:mi>
	    </m:msub>
	  </m:ci>
	</m:math>
	are all zero, and the sine coefficients are

	<equation id="sinecoefficients">
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:ci>
		<m:msub>
		  <m:mi>b</m:mi>
		  <m:mi>k</m:mi>
		</m:msub>
	      </m:ci>
	      <m:piecewise>
		<m:piece>
		  <m:apply>
		    <m:divide/>
		    <m:cn>4</m:cn>
		    <m:apply>
		      <m:times/>
		      <m:pi/>
		      <m:ci>k</m:ci>
		    </m:apply>
		  </m:apply>
		  <m:mtext>k is odd</m:mtext>
		</m:piece>
		<m:piece>
		  <m:cn>0</m:cn>
		  <m:mtext>k is even</m:mtext>
		</m:piece>
	      </m:piecewise>
	    </m:apply>
	  </m:math>
	</equation>

	Thus, the Fourier series for the square wave is

	<equation id="fourierseriessquarewave">
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:apply>
		<m:ci type="fn">sq</m:ci>
		<m:ci>t</m:ci>
	      </m:apply>
	      <m:apply>
		<m:sum/>
		<m:bvar>
		  <m:ci>k</m:ci>
		</m:bvar>
		<m:condition>
		  <m:apply>
		    <m:in/>
		    <m:ci>k</m:ci>
		    <m:set>
		      <m:cn>1</m:cn>
		      <m:cn>3</m:cn>
		      <m:ci>…</m:ci>
		    </m:set>
		  </m:apply>
		</m:condition>
		<m:apply>
		  <m:times/>
		  <m:apply>
		    <m:divide/>
		    <m:cn>4</m:cn>
		    <m:apply>
		      <m:times/>
		      <m:pi/>
		      <m:ci>k</m:ci>
		    </m:apply>
		  </m:apply>
		  <m:apply>
		    <m:sin/>
		    <m:apply>
		      <m:divide/>
		      <m:apply>
			<m:times/>
			<m:cn>2</m:cn>
			<m:pi/>
			<m:ci>k</m:ci>
			<m:ci>t</m:ci>
		      </m:apply>
		      <m:ci>T</m:ci>
		    </m:apply>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:apply>
	  </m:math>
	</equation>

	As we see in <cnxn target="fourpointfour" strength="9"/>, the
	Fourier series requires many more terms to provide the same
	quality of approximation as we found with the 
	<cnxn document="m0061" target="fourier" strength="9">half-wave
	  rectified sinusoid</cnxn>. 
	We can verify that more terms are needed by considering the
	power spectrum and the approximation error shown in
	<cnxn target="fourpointfive" strength="9"/>.
      </para>


      <figure id="fourpointfive">
	<name>Power spectrum and rms error</name> 
	<media type="image/png" src="fourier5.png"/> 
	<caption>
	  The upper plot shows the power spectrum of the square wave,
	  and the lower plot the rms error of the finite-length
	  Fourier series approximation to the square wave. The
	  asterisk denotes the rms error when the number of terms
	  <m:math> 
	    <m:ci>K</m:ci>
	  </m:math>
	  in the Fourier series equals 99.
	</caption>
      </figure>


      <para id="conclusion">
	This difference between the two Fourier series results because
	the half-wave rectified sinusoid's Fourier coefficients are
	proportional to
	<m:math>
	  <m:apply>
	    <m:divide/>
	    <m:cn>1</m:cn>
	    <m:apply>
	      <m:power/>
	      <m:ci>k</m:ci>
	      <m:cn>2</m:cn>
	    </m:apply>
	  </m:apply>
	</m:math>
	while those of the square wave are proportional to
	<m:math>
	  <m:apply>
	    <m:divide/>
	    <m:cn>1</m:cn>
	    <m:ci>k</m:ci>
	  </m:apply>
	</m:math>. 
	In short, the square wave's coefficients decay more slowly
	with increasing frequency. Said another way, the square-wave's
	spectrum contains more power at higher frequencies than does
	the half-wave-rectified sinusoid.
      </para>
    </example>


    <exercise id="exer1">
      <problem>
	<para id="problem_1">
	  Calculate the harmonic distortion for the square wave.
	</para>
      </problem>
      <solution>
	<para id="solutiontoproblem_1">
	  Total harmonic distortion in the square wave is
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:apply>
		<m:minus/>
		<m:cn>1</m:cn>
		<m:apply>
		  <m:power/>
		  <m:apply>
		    <m:divide/>
		    <m:cn>4</m:cn>
		    <m:pi/>
		  </m:apply>
		  <m:cn>2</m:cn>
		</m:apply>
	      </m:apply>
	      <m:apply>
		<m:times/>
		<m:cn>20</m:cn>
		<m:ci>%</m:ci>
	      </m:apply>
	    </m:apply>
	  </m:math>.
	</para>
      </solution>
    </exercise>


    <figure id="fig4.4">
      <media type="image/png" src="fourier4.png"/>
      <caption>
	Fourier series approximation to
	<m:math>
	  <m:apply>
	    <m:ci type="fn">sq</m:ci>
	    <m:ci>t</m:ci>
	  </m:apply>
	</m:math>. 
	The number of terms in the Fourier sum is indicated in each
	plot, and the square wave is shown as a dashed line over two
	periods.
      </caption>
    </figure>

    <para id="para1">
      When comparing the square wave to its Fourier series
      representation it is not clear that the two are equal. The fact
      that the square wave's Fourier series requires more terms for a
      given representation accuracy is not important. However, close
      inspection of <cnxn target="fig4.4" strength="9"/> does reveal a
      potential issue: Does the Fourier series really equal the square
      wave at <emphasis>all</emphasis> values of
      <m:math display="inline">
	<m:ci>t</m:ci> 
      </m:math>?       
      In particular, at each step-change in the square wave, the
      Fourier series exhibits a peak followed by rapid
      oscillations. As more terms are added to the series, the
      oscillations seem to become more rapid and smaller, but the
      peaks are not decreasing. Consider this mathematical question
      intuitively: Can a discontinuous function, like the square wave,
      be expressed as a sum, even an infinite one, of continuous ones?
      One should at least be suspicious, and in fact, it can't be thus
      expressed. This issue brought

      <link src="http://www-groups.dcs.st-and.ac.uk/~history/Mathematicians/Fourier.html">Fourier</link> 

      much criticism from the French Academy of Science (Laplace,
      Legendre, and Lagrange comprised the review committee) for
      several years after its presentation on 1807.  It was not
      resolved for also a century, and its resolution is interesting
      and important to understand from a practical viewpoint.
    </para>


    <para id="para2">
      The extraneous peaks in the square wave's Fourier series
      <emphasis>never</emphasis> disappear; they are termed
      <term>Gibb's phenomenon</term> after the American physicist
      Josiah Willard Gibbs. They occur whenever the signal is
      discontinuous, and will always be present whenever the signal
      has jumps. Let's return to the question of equality; how can the
      equal sign in the 

      <cnxn target="sine" document="m0039" strength="6">definition of
	the Fourier series </cnxn> 

      be justified? The partial answer is that pointwise—each and
      every value of
      <m:math>
	<m:ci>t</m:ci> 
      </m:math>—equality 
      is <emphasis>not</emphasis> guaranteed. What mathematicians
      later in the nineteenth century showed was that the rms error of
      the Fourier series was always zero.

      <equation id="eqn1">
	<m:math display="block">
	  <m:apply>
	    <m:eq/>
	    <m:apply>
	      <m:limit/>
	      <m:bvar>
		<m:ci>K</m:ci>
	      </m:bvar>
	      <m:lowlimit>
		<m:infinity/>
	      </m:lowlimit>
	      <m:apply>
		<m:ci type="fn">rms</m:ci>
		<m:ci>
		  <m:msub>
		    <m:mi>ε</m:mi>
		    <m:mi>K</m:mi>
		  </m:msub>
		</m:ci>
	      </m:apply>
	    </m:apply>
	    <m:cn>0</m:cn>
	  </m:apply>
	</m:math>
      </equation>

      What this means is that the difference between an actual signal
      and its Fourier series representation may not be zero, but the
      square of this quantity has <emphasis>zero</emphasis> integral!
      It is through the eyes of the rms value that we define equality:
      Two signals
      <m:math>
	<m:apply>
	  <m:ci type="fn">
	    <m:msub>
	      <m:mi>s</m:mi>
	      <m:mn>1</m:mn>
	    </m:msub>
	  </m:ci>
	  <m:ci>t</m:ci>
	</m:apply>
      </m:math>,

      <m:math>
	<m:apply>
	  <m:ci type="fn">
	    <m:msub>
	      <m:mi>s</m:mi>
	      <m:mn>2</m:mn>
	    </m:msub>
	  </m:ci>
	  <m:ci>t</m:ci>
	</m:apply>
      </m:math>
      are said to be equal in the <term>mean square</term> if
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:ci type="fn">rms</m:ci>
	    <m:apply>
	      <m:minus/>
	      <m:ci>
		<m:msub>
		  <m:mi>s</m:mi>
		  <m:mn>1</m:mn>
		</m:msub>
	      </m:ci>
	      <m:ci>
		<m:msub>
		  <m:mi>s</m:mi>
		  <m:mn>2</m:mn>
		</m:msub>
	      </m:ci>
	    </m:apply>
	  </m:apply>
	  <m:cn>0</m:cn>
	</m:apply>
      </m:math>. 
      These signals are said to be equal <term>pointwise</term> if
      <m:math>
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:ci type="fn">
	      <m:msub>
		<m:mi>s</m:mi>
		<m:mn>1</m:mn>
	      </m:msub>
	    </m:ci>
	    <m:ci>t</m:ci>
	  </m:apply>
	  <m:apply>
	    <m:ci type="fn">
	      <m:msub>
		<m:mi>s</m:mi>
		<m:mn>2</m:mn>
	      </m:msub>
	    </m:ci>
	    <m:ci>t</m:ci>
	  </m:apply>
	</m:apply>
      </m:math>
      for all values of <m:math><m:ci>t</m:ci></m:math>. For Fourier
      series, Gibb's phenomenon peaks have finite height and zero
      width: The error differs from zero only at isolated
      points--whenever the periodic signal contains
      discontinuities--and equals about 9% of the size of the
      discontinuity. The value of a function at a finite set of points
      does not affect its integral. This effect underlies the reason
      why defining the value of a discontinuous function, like we
      refrained from doing in defining the <cnxn document="m0004" target="stepdef" strength="5">step function</cnxn>, at its
      discontinuity is meaningless. Whatever you pick for a value has
      no practical relevance for either the signal's spectrum or for
      how a system responds to the signal.  The Fourier series value
      "at" the discontinuity is the average of the values on either
      side of the jump.
    </para>

  </content>
</document>
