Sunaina 2018 Eur. J. Phys. 39 065806

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

European Journal of Physics

PAPER

Calculating numerical derivatives using Fourier transform: some pitfalls


and how to avoid them
To cite this article: Sunaina et al 2018 Eur. J. Phys. 39 065806

View the article online for updates and enhancements.

This content was downloaded from IP address 103.27.8.49 on 26/11/2019 at 04:45


European Journal of Physics
Eur. J. Phys. 39 (2018) 065806 (9pp) https://doi.org/10.1088/1361-6404/aadda6

Calculating numerical derivatives using


Fourier transform: some pitfalls and how to
avoid them
Sunaina, Mansi Butola and Kedar Khare
Department of Physics, Indian Institute of Technology Delhi, New Delhi, India 110016

E-mail: kedark@physics.iitd.ac.in

Received 17 May 2018, revised 6 August 2018


Accepted for publication 30 August 2018
Published 3 October 2018

Abstract
Numerical differentiation is commonly used by a number of science students
and researchers for data analysis. The differentiation of vectors of data points
representing discrete samples of some underlying signal can be implemented
in a computer using the central differencing scheme or the fast Fourier
transform (FFT)-based approach. We point out that a simple extension of the
continuous Fourier transform derivative identity to the discrete case, however,
gives rise to results that are inconsistent with the central differencing scheme.
In particular, for functions with step-like discontinuities, the numerical deri-
vative computed with the FFT-based approach is corrupted by ringing arti-
facts. We describe the idea of a modified wave number for FFT-based
numerical differentiation which leads to results that are consistent with the
central differencing scheme. Modified wave number identities for numerical
computation of both first and second order derivatives are described. The
pitfalls of using a simplistic extension of the derivative identity for continuous
Fourier transform to the discrete case and the methodology to avoid them are
illustrated with numerical examples.

Keywords: numerical methods, numerical differentiation, central differencing,


fast Fourier transform

(Some figures may appear in colour only in the online journal)

1. Introduction

In the age of big data, numerical derivatives are very important for understanding trends in a
given data stream. Numerical derivatives are also required for solving sophisticated problems
involving systems of coupled partial differential equations, e.g. Maxwell’s equations for

0143-0807/18/065806+09$33.00 © 2018 European Physical Society Printed in the UK 1


Eur. J. Phys. 39 (2018) 065806 Sunaina et al

electromagnetic field calculations [1]. Computation of numerical derivatives is, therefore, one
of the most basic operations taught in every numerical methods course for science and
engineering students.
Given a data vector g(nΔx) for n=0, 1, 2, 3, ... (N−1) representing the samples of a
continuous function g(x), the numerical differentiation of g(x) can be performed by the central
differencing formula [2]:
d g (x + D x ) - g (x - D x )
g (x ) » + O ((Dx )2 ). (1)
dx 2Dx
The quantity Δx represents the regular sampling interval and O ((Dx )2 ) reminds us that the
error in this numerical differencing scheme is of the order of (Dx )2 .
An alternate way to do this—which seems attractive to students—is to use the derivative
property of the Fourier transform [3]:
⎡d ⎤
F ⎢ g (x )⎥ = (i2pfx ) G ( fx ) , (2)
⎣ dx ⎦
where G( fx) is the Fourier transform of g(x) defined as
¥
G ( fx ) = ò-¥ dx g (x) exp(-i2pfx x). (3)

It is natural for students to assume that the discrete version of this identity should hold.
However, as we will see below a simplistic extension of this identity to the discrete case leads
to results that are highly inconsistent with those obtained using equation (1). The discrete
Fourier expansion of the vector g (nDx ) is given by
N-1
g (nDx ) = å G (mDfx ) exp (i2pnmDxDfx ) , (4)
m= 0

with n=0, 1, 2, ..., (N−1). The natural way to extend the derivative identity from the
continuous Fourier transform case appears to be
N-1
g ¢ ( nD x ) = å (i2pmDfx ) G (mDfx ) exp(i2pnmDxDfx ). (5)
m= 0

In order to implement Fourier transform-based numerical differentiation on a computer, one


may use the commonly available fast Fourier transform (FFT) routines in software such as
Octave, Python or MATLAB. We illustrate the use of this identity with a discrete version of
the rect (or rectangle) function defined as
g (nDx ) = 1 ∣n - N 2∣ < w 2
=0 otherwise, (6)
where ‘w’ is an integer (<N) representing the width of the rectangle. For the numerical
illustration, we use N=255, Δx=1 and w=100. The function g(n) is plotted in
figure 1(a). The numerical derivative of this function, computed using the central differencing
formula in equation (1), is shown in figure 1(b). We observe that for a version of this function
defined over the continuous real x-axis defined as
gc (x ) = rect (x / w) = 1 ∣ x∣ < w / 2
=0 otherwise, (7)

2
Eur. J. Phys. 39 (2018) 065806 Sunaina et al

Figure 1. (a) A rect function of width w=100 units, defined over a vector of 256
elements. (b) Numerical differentiation of the rect function calculated using the central
differencing formula and (c) using the Fourier transform-based approach as in
equation (5). (d) The gradient of g(x), as in equation (10), which is much closer to its
numerical gradient.

the derivative dgc (x)/dx is given by [3, 4] (see appendix)


d ⎛ w⎞ ⎛ w⎞
gc (x ) = d ⎜x + ⎟ - d ⎜x - ⎟. (8)
dx ⎝ 2 ⎠ ⎝ 2⎠
Here δ(x) denotes the Dirac delta or impulse function. The result, as shown in figure 1(b), is
seen to mimic this continuous domain result very well. An alternative way to compute the
numerical derivative using equation (5) involves the following steps:
(i) Compute the discrete Fourier transform of the data vector g(n) using an FFT routine.
(ii) Multiply the Fourier transform G(mΔfx) element by element with the vector (i2πmΔfx).
(iii) Compute the inverse Fourier transform of the product above in order to estimate the
derivative g′(n).
The result of this three step procedure is shown in figure 1(c). Surprisingly the derivative
computed using the Fourier transform-based procedure has several highly oscillating ringing
artifacts near the steps or edges. This inconsistency in the form of ringing artifacts leaves a
student confused, as it is generally taught that the continuous version of Fourier transform
identities should simply carry over in the discrete case. An error metric defined to check the
mismatch between the two results is given as

3
Eur. J. Phys. 39 (2018) 065806 Sunaina et al

¢ (n) - gFT
gnum ¢ (n ) 
E= . ( 9)
¢ (n ) 
gnum
¢ (n ) and gFT
Here gnum ¢ (n ) are the numerical derivatives of g(n) in figure 1(a) calculated using
equations (1) and (5), respectively, and the notation ∣∣...∣∣ represents the L2-norm. The
computed error with the Fourier-based approach is almost 100% due to the severity of the
ringing artifacts. As we will explain in the next section, the Fourier domain multiplier (i2πfx)
needs to be modified in order to obtain differentiation results that are consistent with the
central differencing scheme.

2. Modified wave number for FFT-based differentiation

The reason for the inconsistency between the results in figures 1(b) and (c) is that the
numerical differentiation using the central difference method is not equivalent to the Fourier
identity (equation (5)). This can be readily seen by evaluating the Fourier transform of the
expression for the central difference formula and using the Fourier shift theorem.
⎡ D g (x ) ⎤ exp (i2pfx Dx ) - exp ( - i2pfx Dx )
F⎢ ⎥ = G ( fx )
⎣ Dx ⎦ 2D x
sin (2pfx Dx )
= iG ( fx ) . (10)
Dx
For the continuous case in the limit Δx → 0 we readily get back equation (2). In order to
obtain a result similar to the central differencing formula with the Fourier method, we
therefore need to use the multiplier i sin (2pfx Dx ) / Dx instead of (i2πfx) in the Fourier
transform domain. These two functions apart from the constant multiplier i = -1 are
plotted in figure 2. When the Fourier domain multiplier as in equation (10) is used for
numerical differentiation, the corresponding result is plotted in figure 1(d). For this result the
normed relative error is observed to be 6.3×10−16, which is of the order of the double-
precision floating point accuracy. In the literature, the sin (2pfx Dx ) Dx multiplier, as in
equation (10), has been referred to as the ‘modified wave number’ [5], however, it is almost
never mentioned in most textbooks on numerical methods and is virtually never taught in a
classroom.
In the above illustration we have chosen a rect function which has two discontinuities on
the real axis. The use of equation (5) for calculating the numerical derivative of the
corresponding function defined on a discrete grid shows severe ringing artifacts as seen in
figure 1(c). We point out that the use of equation (5) gives incorrect results for seemingly
smooth functions as well and not just in the case of functions with discontinuities. For a case
in point, we take a Gaussian function defined as
g (nDx ) = exp ( - (nDx )2 2s 2) , n = - (N 2) , ..., 0, ...(N 2). (11)
Figure 3(a) shows a plot of the above Gaussian function with σ=0.85 defined over a grid of
161 points with Δx=1. The numerical derivative of this function calculated using the central
differencing scheme is shown in figure 3(b). The use of identity as in equation (5), to evaluate
the numerical derivative, leads to ringing artifacts as well as increased magnitude of the
peaks, as is clearly visible in figure 3(c). The numerical derivative computed using the correct
Fourier identity as in equation (10), which is consistent with the central differencing scheme,
is shown in figure 3(d). The relative L2-norm errors for the derivative plots in figures 3(c) and
(d) with respect to that in figure 3(b) are 0.49 and 1.58×10−16, respectively. A narrow

4
Eur. J. Phys. 39 (2018) 065806 Sunaina et al

Figure 2. The multiplier functions for derivative calculation in the Fourier domain
corresponding to the continuous (equation (2)) and the discrete (equation (10)) cases,
respectively.

Gaussian function was used here since it has a wide extent in the Fourier domain. The
significant error in the numerical derivative evaluation is therefore a result of the difference in
the two Fourier multipliers in the high frequency range (see figure 2).
The Parseval’s theorem for the gradient of a function in the Fourier theory using
equation (2) can be stated as
¥ d 2 ¥
ò-¥ dx
g (x ) dx = ò-¥ ∣i2pfx G ( fx )∣2 dfx. (12)

It can be verified with numerical computation that a relation equivalent to the above equation
for the discrete case is given by
N-1 N-1
sin (2pmDfx Dx ) 2
å ∣g¢(nDx)∣2 Dx = å Dx
G (mDfx ) Dfx . (13)
n=0 m= 0
Here, as before, g¢ (nDx ) and G(mΔfx) are to be evaluated using the central difference formula
and the FFT routines, respectively.
Similarly the differencing operation for the second order derivative has the form
D2g (x ) g (x + Dx ) - 2g (x ) + g (x - Dx )
= + O ((Dx )2 ). (14)
Dx 2 (Dx ) 2
As a result the equivalent Fourier relation for the second order central differencing formula is
given by
⎡ D2 ⎤ - G ( fx ) sin2 (2pfx Dx )
F ⎢ 2 g ( x )⎥ = (15)
⎣ Dx ⎦ (D x ) 2

5
Eur. J. Phys. 39 (2018) 065806 Sunaina et al

Figure 3. (a) A Gaussian function defined over 161 samples. The numerical derivative
of the Gaussian function calculated by (b) the central differencing scheme, (c) Fourier
identity with the multiplier i2π fx equation (5), and (d) Fourier identity having modified
wave number as a multiplier.

Once again in the limit Δx → 0 we get the continuous case:


- G ( fx ) sin2 (2pfx Dx )
lim = - 4p 2f x2 G ( fx ). (16)
Dx  0 Dx 2
The second order numerical differentiation operation is illustrated in figure 4. Figure 4(a)
shows the second order numerical differentiation result for the rect function using the central
differencing formula. The corresponding results using the Fourier transform approach with
Fourier domain multipliers given by -4pf x2 and -sin2 (2pfx Dx ) (Dx )2 are shown in
figures 4(b) and (c), respectively. Once again we observe ringing artifacts in figure 4(b) and
that the result in figure 4(c) is almost the same as in figure 4(a). The relative L2-normed errors
for plots in figures 4(b) and (c) with respect to figure 4(a) are found to be 4.3 and
8.1×10−16, respectively.

3. Illustration of numerical differentiation for a QR code

In this section, we illustrate the numerical differentiation methods in relation to the everyday
use application of QR code scanning. Almost everyone with a smartphone has surely scanned
a QR code in order to access the information through the online link encoded in it or for

6
Eur. J. Phys. 39 (2018) 065806 Sunaina et al

Figure 4. (a) The numerical second order derivative of the rect function calculated
using equation (14). (b) The second order derivative of the rect function evaluated
using the FT-based approach as in equation (16) and (c) is the modified second order
derivative using equation (15).

shopping. QR codes are two-dimensional (2D) sequences of step-like functions as shown in


figure 5(a). The QR code reader typically uses edges in the step function pattern for decoding
or accessing the desired information. The detection of edges in such patterns is actually
achieved by calculating the numerical derivative of the QR code image. It is clear that if the
QR code is focused appropriately on a camera sensor, the numerical derivative will be
significant (as in figure 1(b)) only at the transition regions. Based on the discussion in the
previous section, the correct Fourier identity for the numerical derivative calculation for the
2D function g(x, y) is given by

⎡ D g (x , y ) D g (x , y ) ⎤
F [g (x , y)] = F ⎢xˆ + yˆ ⎥
⎣ Dx Dy ⎦
⎡ sin (2pf Dx ) sin (2pfy Dy) ⎤
= iG ( fx , fy ) ⎢xˆ x
+ yˆ ⎥. (17)
⎣ Dx Dy ⎦

Here, x̂ and ŷ represent unit vectors in the x and y directions, respectively (remember that the
2D gradient has components in the x and y directions). In figure 5(a) we show a 2D code
represented as a 300×300 matrix (or image) and a 100×100 region of the QR code is
shown in figure 5(b) that we have used for the illustration. We first calculate the numerical
gradient of the image in figure 5(b) by finding x and y derivatives by the central differencing

7
Eur. J. Phys. 39 (2018) 065806 Sunaina et al

Figure 5. (a) A QR code of size 300×300 and (b) its zoom-in portion (inset). (c) The
derivative of the QR code in (b) evaluated by the central differencing scheme (d) The
derivative of the QR code evaluated using the FT-based scheme as in equation (5) and
(e) is the correct FT-based scheme derivative of the QR code using equation (17).

scheme. The gradient magnitude is calculated from the two components as

∣g (x , y)∣ = ∣x g (x , y)∣2 + ∣y g (x , y)∣2 . (18)

The sharp edges of the QR code in figure 5(b) are clearly visible in figure 5(c). In figure 5(d)
we show the numerical gradient magnitude calculated using a 2D version of the Fourier
identity given in equation (5). In this figure, one can clearly see the artifacts present near the
edges of the gradient image and this is inconsistent with the gradient image we obtained in
figure 5(c). The correct Fourier identity as given in equation (17) leads to the gradient image
as shown in figure 5(e), which matches very well with the gradient image in figure 5(c). The
relative L2-norm errors for the results in figures 5(d) and (e) with respect to the result in
figure 5(c) are 0.96 and 4.5×10−16, respectively, again clearly illustrating the accuracy
achieved by using the formulation in equation (17).

4. Summary

In summary, it is important to convey to students that when performing numerical differ-


entiation using the Fourier transform approach, one needs to be careful in choosing the correct
Fourier domain multiplier to obtain results that are consistent with the commonly used central
differencing formula. The direct extension of the derivative identities for the continuous
Fourier transform to the discrete case can lead to errors such as severe ringing artifacts. We
have also shown a practical example of QR code scanning where the use of correct Fourier
domain multiplier is essential for determining the edge locations in the bar-code accurately.
The methodology described here is fairly general and can find multiple applications where
numerical derivatives need to be evaluated.

8
Eur. J. Phys. 39 (2018) 065806 Sunaina et al

Appendix. The Delta function and the derivative of a unit step

The numerical derivative of a discontinuous function involving a step is not usually defined in
elementary calculus texts. The derivative for a rect function as described in equation (8)
therefore demands a brief discussion. The Delta function is usually defined in terms of its
sampling or sifting property:
¥
ò-¥ f (x ) d (x - a) dx = f (a). (19)

It is important to note that the Delta function is not a function in the usual sense, which takes a
number as an input and generates another number as an output. The Delta function is an
example of a class known as ‘generalized functions’ [4], which take a function as an input and
generate a number as an output. The Delta function can also be defined as the limit of a
function sequence. For example we may define
1 ⎛x⎞
d (x ) = lim rect ⎜ ⎟. (20)
L0 L ⎝L⎠
As L → 0 (or 1 L  ¥), the height of the rect function increases in such a way that the area
under the curve is always equal to 1. The definition in equation (19) is to be understood in this
sense. An interesting property of the Delta function is its relation to the unit step function U
(x), which is equal to 1 for x>0 and is equal to 0 for x<0. We observe that
x
U (x ) = ò-¥ d (t ) dt. (21)

The above relation is easy to understand. When x<0 we have not yet crossed the Delta peak
and as a result the integral remains equal to zero. However, when x>0 we have integrated
the Delta peak making the function equal to 1 for all x>0. From this relation it is clear that
the derivative of a unit step function is a Dirac delta function. The rect function has one rising
unit step and a falling unit step and as result, its derivative is equal to a sum of a positive and
negative Delta peak as in equation (8).

ORCID iDs

Kedar Khare https://orcid.org/0000-0002-3104-3850

References

[1] Taflove A and Hagness S C 2005 Computational Electrodynamics: The Finite-Difference Time-
Domain Method (Boston, MA: Artech House)
[2] Cheney E W and Kincaid D 2012 Numerical Mathematics and Computing (Toronto: Nelson
Education)
[3] Bracewell R N 1986 The Fourier Transform and Its Applications (New York: McGraw-Hill)
[4] Lighthill M J 1958 An Introduction to Fourier Analysis and Generalized Functions (London:
Cambridge University Press)
[5] Moin P 2010 Fundamentals of Engineering Numerical Analysis (London: Cambridge University
Press)

You might also like