Oscillatory Functions in AI & Machine Learning | SciPy

Explore oscillatory functions and their role in AI & Machine Learning. Learn how SciPy models wave-like behaviors and periodic phenomena for signal processing.

Oscillatory Functions

Oscillatory functions are mathematical functions that exhibit repeated fluctuations between certain values, often in a periodic manner. These functions are fundamental in modeling wave-like behaviors, vibrations, alternating currents, and various other periodic phenomena encountered in signal processing, physics, and engineering.

SciPy provides specialized tools for efficiently handling oscillatory functions, including common trigonometric functions like sine and cosine. While these functions are powerful, they can present challenges in numerical computations, particularly during integration over large intervals due to frequent sign changes that can lead to cancellation errors or convergence difficulties. SciPy offers methods designed to mitigate these issues, especially in numerical integration and solving differential equations.

Characteristics of Oscillatory Functions

Understanding the key characteristics of oscillatory functions is crucial for their effective use in scientific computations, especially in fields like signal processing, physics, and electrical engineering.

  • Periodicity: Oscillatory functions repeat their values at regular intervals. This interval is known as the period. For example, sine and cosine functions have a period of $2\pi$.
  • Alternating Signs: These functions fluctuate between positive and negative values, transitioning between peaks (maximum values) and troughs (minimum values).
  • Amplitude: The amplitude defines the maximum absolute value the function can reach. It dictates the extent of the peaks and troughs.
  • Frequency: Frequency determines the number of cycles or oscillations that occur within a unit interval. A higher frequency indicates more oscillations in the same interval.
  • Damping: Some oscillatory functions exhibit damping, where the amplitude of oscillation gradually decreases over time. This is often due to dissipative forces like resistance or friction.
  • Symmetry: Many oscillatory functions possess symmetry. For instance, sine and cosine functions exhibit symmetry around the y-axis (even function) or the origin (odd function).
  • Phase Shift: A phase shift represents a horizontal displacement of the function's graph, altering the position of its peaks and troughs.

Handling Oscillatory Functions in SciPy

SciPy offers robust tools for handling the unique challenges posed by oscillatory functions in scientific computations, particularly in numerical integration and solving differential equations.

1. Numerical Integration with scipy.integrate.quad

SciPy's quad function is a versatile tool for numerical integration that can effectively handle oscillatory integrals. While quad adapts to oscillations, it may require careful consideration or specific techniques for highly oscillatory functions.

Example: Integrating a Sine Wave

import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt

# Define an oscillatory function (sine wave)
def oscillatory_func(x):
    return np.sin(100 * x)

# Perform integration over a specific interval
result, error = quad(oscillatory_func, 0, np.pi)

print(f"Integral result: {result}")
print(f"Estimated error: {error}")

# Plotting the function
x_values = np.linspace(0, np.pi, 500)
y_values = oscillatory_func(x_values)

plt.figure(figsize=(10, 5))
plt.plot(x_values, y_values, label="sin(100x)")
plt.title("Oscillatory Function (Sine Wave)")
plt.xlabel("x")
plt.ylabel("Amplitude")
plt.grid(True)
plt.legend()
plt.show()

Output:

Integral result: 2.3480880169895062e-15
Estimated error: 2.6083051919004057e-15

2. Precision Loss in High-Frequency Oscillations

For functions with very high frequencies, precision loss can occur during numerical integration due to the rapid sign changes. In such scenarios, dividing the integration range into smaller sub-intervals can significantly improve accuracy.

3. Specialized Integration Methods

In cases of extreme oscillation, standard integration methods might not suffice. Advanced techniques like Levin integration or custom algorithms, while not directly built into SciPy, can be implemented using additional libraries or custom code.

Types of Oscillatory Functions in SciPy

SciPy supports a variety of oscillatory functions, from fundamental trigonometric forms to more complex mathematical functions used in advanced scientific applications.

  • Sine Wave: A basic periodic oscillatory function that repeats at regular intervals.
  • Cosine Wave: Similar to the sine wave, but phase-shifted by $\pi/2$.
  • Fourier Series: Used to represent complex periodic signals as a sum of sine and cosine functions.
  • Bessel Functions: Oscillatory functions that arise in solutions to wave propagation problems and other physical phenomena.
  • Airy Functions: Exhibit oscillatory behavior that decays for large input values, commonly used in quantum mechanics and optics.
  • Modified Bessel Functions: These functions also oscillate but grow exponentially for large input values.

Example: Integrating a Cosine Wave

import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt

# Define a customizable cosine wave function
def cosine_wave(x, amplitude=1, frequency=1, phase_shift=0, vertical_shift=0):
    return amplitude * np.cos(frequency * x + phase_shift) + vertical_shift

# Set parameters for the cosine wave
A = 1   # Amplitude
B = 2   # Frequency
C = 0   # Phase shift
D = 0   # Vertical shift

# Define the limits of integration
lower_limit = 0
upper_limit = 2 * np.pi

# Integrate the cosine wave over one period
integral, error = integrate.quad(cosine_wave, lower_limit, upper_limit, args=(A, B, C, D))

print(f"Integral of cosine wave from {lower_limit} to {upper_limit} is: {integral:.5f}")
print(f"Estimated error: {error:.5f}")

# Plotting the cosine wave
x_values = np.linspace(lower_limit, upper_limit, 500)
y_values = cosine_wave(x_values, A, B, C, D)

plt.figure(figsize=(10, 5))
plt.plot(x_values, y_values, label="Cosine Wave", color='green')
plt.title('Oscillatory Function (Cosine Wave)')
plt.xlabel('x')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
plt.show()

Output:

Integral of cosine wave from 0 to 6.283185307179586 is: 0.00000
Estimated error: 0.00000

Example: Fourier Series Approximation of a Square Wave

The Fourier series allows us to represent complex periodic functions, like a square wave, as a sum of simpler sine and cosine waves.

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad # quad is not directly used here for Fourier, but conceptually relevant

# Define time variable and parameters
t = np.linspace(0, 2 * np.pi, 1000)  # Time variable
A = 1  # Amplitude
frequency = 1  # Fundamental frequency

# Create a square wave
square_wave = A * np.sign(np.sin(frequency * t))

# Compute Fourier series coefficients for a square wave approximation
n_terms = 20  # Number of terms in the Fourier series
fourier_series_approx = np.zeros_like(t)

# The coefficients for a square wave are: a_0 = 0, a_n = 0 for n > 0, and b_n = (4A / (n * pi)) for odd n.
for n in range(1, n_terms + 1):
    if n % 2 != 0:  # For odd terms
        b_n = (4 * A) / (n * np.pi)
        fourier_series_approx += b_n * np.sin(n * frequency * t)

# Plot the original function and its Fourier series approximation
plt.figure(figsize=(12, 6))
plt.plot(t, square_wave, label='Square Wave', color='blue', linewidth=2)
plt.plot(t, fourier_series_approx, label=f'Fourier Series ({n_terms} terms)', color='red', linestyle='--')
plt.title('Oscillatory Fourier Series Representation of a Square Wave')
plt.xlabel('Time (t)')
plt.ylabel('Amplitude')
plt.axhline(0, color='black', lw=0.5, ls='--')
plt.axvline(0, color='black', lw=0.5, ls='--')
plt.grid(True)
plt.legend()
plt.show()