Tuesday, 1 January 2013

Fourier series, conditions and recursive functions

This script uses recursive functions to plot a square pulse function and its Fourier series expansion, truncated to some maximum Nmax.  The terms of the series itself must be found analytically, but once found this script allows one to visualize the convergence of the series.  The resulting plot is shown on the right.

The result of the Fourier series is found by summing over the terms of the series, each of which have a simple analytic dependence on the summation variable.  To do this, the gnuplot script makes use of the Ternary operator ?, which can be used for conditional assignment.  The syntax for this can be described as:
y(x) = (cond) ? [true] : [false]
The function y(x) takes the value [true] if condition (cond) is true, and [false] if condition (cond) is false.  The Ternary operator itself is ? and a colon : is used to separate the possible results [true] and [false].  Both [true] and [false] may be further conditional statements, meaning the statements can be nested, e.g.,
y(x) = (cond1) ? (cond2) ? [true2] : [false2] : [false1]
If (cond1) is true, then the second condition (cond2) is tested.  If both are true, y(x) takes the value [true2].  If (cond1) is true but (cond2) is false, y(x) has the value [false2].  Finally if (cond1) is false y(x) takes the value [false1].

The step function for the present example is defined as:
f(x) = -2 (-π≤x<0)
     =  1 (0≤x<+π)
and is periodic with period 2π.  The terms of the Fourier series can be evaluated analytically.  The gnuplot code to produce the plot is:
set terminal png enhanced size 800,600 font "sans, 18"
set output "fourier.png"

set tics nomirror
set border 2
set xzeroaxis lt -1 lw 2
set xtics axis

pi = 3.14159265359

# f(x) is the step function that is being expanded
f(x) = (x<-pi) ? f(x+2*pi) : (x>pi) ? f(x-2*pi) : (x<0) ? -2 : 1

# g(n,x) is the nth term in the Fourier series
g(n,x) = (n==0) ? -0.5 : (1-(-1)**n)*3*sin(n*x)/(n*pi)

# s(n,x) is the Fourier series up to the nth term
s(n,x) = (n>=0) ? g(n,x) + s(n-1,x) : 0

set yrange[-3.5:2.5]
set xlabel "x"
set ylabel "f(x)"

set samples 400

# set the maximum term in the series
nmax1=3
nmax2=10

plot f(x) title "Step pulse",\
     s(nmax1,x) title "Fourier Series, N_{max}=".nmax1,\
     s(nmax2,x) title "Fourier Series, N_{max}=".nmax2
The first two lines set the png terminal options and the output file name.  f(x) is the step function itself.  The first two conditions (x<-pi) and (x>pi) move the value of x into the range -π to +π, and the last condition (x<0) gives the function the value -2 or 1. g(n,x) is the nth term in the Fourier series, determined analytically, and s(n,x) gives the full Fourier series up to a maximum value of n, nmax. The condition in s(n,x) makes sure the recursion stops when n=0.  The script will plot the exact step function and the Fourier series truncated to maximum order nmax1 and nmax2.  By varying nmax, the convergence of the series can be seen.

There are a couple other neat tricks in there too.  The second group of set commands remove the normal borders and place the x-axis at y=0, and in the last line the string concatenation operator "." is used to append the gnuplot variable nmax onto the title for the Fourier series.

1 comment:

  1. Very neat plot!
    Very elegant programming style
    Thanks

    A ortiz

    ReplyDelete