Ma · Mathematics

How to Make Any Function Repeat Periodically

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose a period

    Pick a positive period T and a base interval of length T.

  2. 2. Reduce the input

    Map every input x back into the base interval using the remainder modulo T.

  3. 3. Evaluate the base function

    Apply the original function to that reduced input.

  4. 4. Check the join

    Match endpoint values and derivatives if continuity or smoothness matters.

A function defined on all of R\mathbb{R} is periodic if there is a number T>0T>0 such that

F(x+T)=F(x)F(x+T)=F(x)

for every x∈Rx\in\mathbb{R}. The number TT is called a period; the smallest positive period, when one exists, is the fundamental period.

The standard way to make a function repeat is to define it on one interval of length TT, then use the remainder modulo TT to select the corresponding point in that interval. This produces a periodic extension. It does not, however, preserve an arbitrary function already defined on all of R\mathbb{R}: if the original values conflict with periodicity, some values must be replaced.

The basic construction

Suppose ff is defined on a base interval [a,a+T)[a,a+T), where T>0T>0. Define

F(x)=f(a+((x−a) mod T)).F(x)=f\left(a+((x-a)\bmod T)\right).

Here (x−a) mod T(x-a)\bmod T is the remainder in the interval [0,T)[0,T). Thus the argument of ff always lies in [a,a+T)[a,a+T).

For example, if a=0a=0 and T=2πT=2\pi, then

F(x)=f(x mod 2π).F(x)=f(x\bmod 2\pi).

The notation x mod Tx\bmod T is sometimes written using the floor function:

x mod T=x−T⌊xT⌋.x\bmod T=x-T\left\lfloor\frac{x}{T}\right\rfloor.

Therefore an equivalent formula is

F(x)=f(a+x−a−T⌊x−aT⌋).F(x)=f\left(a+x-a-T\left\lfloor\frac{x-a}{T}\right\rfloor\right).

The simpler modulo notation is usually clearer.

Why the construction is periodic

The defining property of the remainder is

((x−a)+T) mod T=(x−a) mod T.((x-a)+T)\bmod T=(x-a)\bmod T.

Consequently,

F(x+T)=f(a+((x+T−a) mod T))=f(a+((x−a) mod T))=F(x).\begin{aligned} F(x+T) &=f\left(a+((x+T-a)\bmod T)\right)\\ &=f\left(a+((x-a)\bmod T)\right)\\ &=F(x). \end{aligned}

So FF is periodic with period TT, regardless of the formula used for ff on the base interval.

Worked example: repeating a parabola

Let the base function be

f(t)=t2,0≤t<2,f(t)=t^2,\qquad 0\le t<2,

and choose period T=2T=2. Its periodic extension is

F(x)=(x mod 2)2.F(x)=(x\bmod 2)^2.

For several inputs:

xxx mod 2x\bmod 2F(x)F(x)
0.50.50.50.50.250.25
1.51.51.51.52.252.25
2.52.50.50.50.250.25
−0.5-0.51.51.52.252.25
4.54.50.50.50.250.25

The graph on every interval [2k,2k+2)[2k,2k+2) is a copy of the graph of t2t^2 on [0,2)[0,2). In particular, F(x+2)=F(x)F(x+2)=F(x).

At the joining points, this example has a jump. As xx approaches 22 from the left, F(x)F(x) approaches 44, but F(2)=0F(2)=0 because 2 mod 2=02\bmod 2=0. The function is periodic, but discontinuous.

Making the periodic extension continuous

For the extension to be continuous at a join, assume that ff is continuous on [a,a+T)[a,a+T) in the one-sided sense at aa, and that the finite left-hand limit at a+Ta+T exists. Under these assumptions, the extension is continuous at the join exactly when the values at the two ends agree:

f(a)=lim⁡t→(a+T)−f(t).f(a)=\lim_{t\to(a+T)^-}f(t).

If ff is defined and continuous on the closed interval [a,a+T][a,a+T], the condition is simply

f(a)=f(a+T).f(a)=f(a+T).

For example, take

f(t)=t(2−t),0≤t≤2.f(t)=t(2-t),\qquad 0\le t\le 2.

Since f(0)=f(2)=0f(0)=f(2)=0, repeating this function with period 22 gives a continuous periodic function. One can write it as

F(x)=r(2−r),r=x mod 2.F(x)=r(2-r),\qquad r=x\bmod 2.

Although the formula uses a remainder and changes at every even integer, the function itself has no jump there.

Making it differentiable or smoother

Matching endpoint values is enough for continuity, but not for differentiability. Assume that ff has continuous derivatives through the desired order in the interior, with one-sided derivatives extending to both endpoints. Endpoint quantities at a+Ta+T below are understood as left-hand endpoint values. To obtain a continuously differentiable periodic extension, require matching first derivatives as well:

f(a)=f(a+T),f′(a)=f′(a+T).f(a)=f(a+T), \qquad f'(a)=f'(a+T).

For a twice continuously differentiable extension, also require

f′′(a)=f′′(a+T).f''(a)=f''(a+T).

More generally, under this regularity assumption, to obtain a CkC^k periodic extension, the derivatives through order kk should match at the endpoints:

f(j)(a)=f(j)(a+T),j=0,1,…,k.f^{(j)}(a)=f^{(j)}(a+T), \qquad j=0,1,\ldots,k.

Here j=0j=0 means the function values themselves. These conditions are sufficient when the corresponding derivatives extend continuously to both endpoints.

A familiar example is the trigonometric function. The function f(t)=sin⁡tf(t)=\sin t on [0,2π][0,2\pi] satisfies

f(0)=f(2π)=0,f′(0)=f′(2π)=1,f(0)=f(2\pi)=0, \qquad f'(0)=f'(2\pi)=1,

and all higher derivatives match too. Its periodic extension is already the usual smooth function sin⁡x\sin x.

If the original function is defined everywhere

Suppose you start with a function g:R→Rg:\mathbb{R}\to\mathbb{R} and want a periodic function that agrees with gg everywhere. This is possible only if gg is already periodic with the desired period. Indeed, agreement would imply

g(x+T)=F(x+T)=F(x)=g(x).g(x+T)=F(x+T)=F(x)=g(x).

If agreement is required only on one interval, restrict gg to that interval and apply the periodic-extension formula:

F(x)=g(a+((x−a) mod T)).F(x)=g\left(a+((x-a)\bmod T)\right).

This is the usual meaning of “make a function repeat”: preserve one chosen period of data and copy it outside that interval.

Common mistakes

Choosing the wrong interval length

In this construction, to copy a base interval every TT units, choose a base interval whose length is exactly TT. Repeating data from an interval of length 2T2T gives a function with period 2T2T; it also has period TT only if the two length-TT halves agree.

Ignoring negative inputs

The mathematical remainder is conventionally chosen in [0,T)[0,T). Some programming languages return a negative remainder for negative inputs, so an implementation may need

((x mod T)+T) mod T((x\bmod T)+T)\bmod T

to guarantee a value in [0,T)[0,T).

Assuming periodic means continuous

Modulo repetition always creates a periodic function, but it can have jumps at the boundaries. Check endpoint values—and derivatives if needed—before claiming continuity or smoothness.

Including both endpoints without a convention

The points aa and a+Ta+T represent the same location in the repeated pattern. Using the half-open interval [a,a+T)[a,a+T) avoids assigning two potentially different values to that location. If the endpoint values agree, either endpoint convention gives the same pointwise function; when ff is continuous on the base interval, that function is continuous at the joins.

References

  1. Walter Rudin, Principles of Mathematical Analysis
  2. Elias M. Stein and Rami Shakarchi, Fourier Analysis: An Introduction

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.