Home > Python > Fourier Series

Ch. 16.1 Square Wave

Chapters Index
Program 1

Square Wave

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Fourier Series

The Fourier series is a way to represent a periodic function as a sum of simple sine and cosine waves. It breaks down any periodic function or signal into the sum of an infinite series of sines and cosines. This concept is widely used in fields such as signal processing, acoustics, and heat transfer, as it provides a tool for analyzing the frequency components of signals.

Key Concepts

  • Periodic Function: A function \( f(x) \) is called periodic with period \( T \) if \( f(x + T) = f(x) \) for all \( x \). In a Fourier series, we are interested in expressing this periodic function as a combination of simpler periodic functions (sines and cosines).
  • Orthogonal Basis: The sines and cosines form an orthogonal basis for the space of periodic functions. This means that any periodic function can be expressed as a linear combination of sine and cosine functions.
  • Harmonics: The sine and cosine functions are often called the harmonics of the original periodic function. The fundamental harmonic corresponds to the lowest frequency \( \frac{1}{T} \), while higher harmonics correspond to integer multiples of this frequency.

Fourier Series Mathematical Form

For a periodic function \( f(x) \) with period \( T \), the Fourier series is expressed as:

\[ \small f(x) = a_0 + \sum_{n=1}^{\infty} \left( a_n \cos \left( \frac{2\pi n}{T} x \right) + b_n \sin \left( \frac{2\pi n}{T} x \right) \right) \]

Where:

  • \( a_0 \) is the average (DC component) of the function over one period.
  • \( a_n \) and \( b_n \) are the Fourier coefficients for the cosine and sine terms, respectively.
  • \( n \) is the harmonic number, representing the frequency multiple of the base frequency.

Fourier Coefficients

The Fourier coefficients \( a_0 \), \( a_n \), and \( b_n \) are calculated as follows:

\( a_0 \) (the DC term):

\[ a_0 = \frac{1}{T} \int_0^T f(x) \, dx \]

\( a_n \) (cosine coefficients):

\[ a_n = \frac{2}{T} \int_0^T f(x) \cos \left( \frac{2\pi n}{T} x \right) \, dx \]

\( b_n \) (sine coefficients):

\[ b_n = \frac{2}{T} \int_0^T f(x) \sin \left( \frac{2\pi n}{T} x \right) \, dx \]

Program 2

Square Wave

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Fourier series approximation of a square wave signal (without using scipy.signal)
Theoretical Principles & Computational Physics Guide

Square Wave: Computational Physics Modeling & Analysis

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Core Concept: Computational formulation and simulation of Square Wave within the study of Fourier Series.
Algorithmic Translation: Translates theoretical governing equations into clean, discrete Python procedures and verified numerical solvers.
Numerical Verification: Evaluates numerical accuracy, parameter sensitivity, and error bounds for reliable computational results.

2. Mathematical Formulation & Algorithmic Pipeline

Governing computational formulation for Square Wave:

$$ \mathcal{F}_{\text{model}}(\mathbf{x}, t; \mathbf{p}) = 0 \implies \mathbf{y}_{n+1} = \mathcal{S}(\mathbf{y}_n, \Delta t, \mathbf{p}) $$
1. Define Physical Parameters & Boundaries → 2. Execute Numerical Simulation Steps → 3. Evaluate Observables & Plot Results

3. Numerical Mechanics & Implementation Logic

  • Parameter Initialization: Set physical constants, initial states, and coordinate boundary bounds for Square Wave.
  • Step Execution: Advance the computational state using optimized vector operations and scientific libraries.
  • Convergence Control: Monitor stability and ensure simulation metrics remain physically meaningful.

4. Relevant Computational Practice Exercises

Exercise 1
Implement Square Wave and observe how the output responds to variations in primary physical parameters.
Exercise 2
Refine the calculation grid or step size to observe convergence and reduction in numerical approximation error.
Exercise 3
Compare the simulated computational results against an analytical benchmark or limiting physical case.
Standard Literature & References:
  • Mark Newman, "Computational Physics", University of Michigan (2013).
  • Hans Petter Langtangen, "A Primer on Scientific Programming with Python", Springer (2016).