transform_functions¶
shortfx.fxNumeric.transform_functions
¶
Integral transforms: Laplace, Fourier, and related operations.
Numerical Laplace and Fourier transforms, DFT/IDFT, convolution, and cross-correlation from Spiegel's Mathematical Handbook.
Functions¶
abel_transform_numerical(f: callable, y: float, r_max: float = 10.0, n_points: int = 1000) -> float
¶
Numerical Abel transform: F(y) = 2 ∫_y^∞ f(r) r / √(r²-y²) dr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
callable
|
Radially-symmetric function f(r). |
required |
y
|
float
|
Impact parameter (y ≥ 0). |
required |
r_max
|
float
|
Upper integration limit (approximating ∞). |
10.0
|
n_points
|
int
|
Quadrature points. |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Abel transform value at y. |
Example
import math round(abel_transform_numerical(lambda r: math.exp(-r*r), 0.0), 4) 1.7725
Complexity: O(n_points)
Source code in shortfx/fxNumeric/transform_functions.py
auto_correlation(signal: List[float]) -> List[float]
¶
Computes the discrete auto-correlation of a signal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
List[float]
|
Input sequence. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Auto-correlation result. |
Example
auto_correlation([1, 0, 1]) [1.0, 0.0, 2.0, 0.0, 1.0]
Complexity: O(n^2)
Source code in shortfx/fxNumeric/transform_functions.py
convolution(f: List[float], g: List[float]) -> List[float]
¶
Computes the discrete linear convolution of two sequences.
(f * g)[n] = sum_k f[k] g[n-k].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
List[float]
|
First sequence. |
required |
g
|
List[float]
|
Second sequence. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Convolution result of length len(f) + len(g) - 1. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f or g are not lists. |
ValueError
|
If f or g is empty. |
Example
convolution([1, 2, 3], [1, 1]) [1.0, 3.0, 5.0, 3.0]
Complexity: O(n * m)
Source code in shortfx/fxNumeric/transform_functions.py
cross_correlation(f: List[float], g: List[float]) -> List[float]
¶
Computes the discrete cross-correlation of two sequences.
(f ⋆ g)[n] = sum_k f[k] g[k+n] (conjugate omitted for real signals).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
List[float]
|
First sequence. |
required |
g
|
List[float]
|
Second sequence. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Cross-correlation result. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f or g are not lists. |
ValueError
|
If f or g is empty. |
Example
cross_correlation([1, 2, 3], [1, 2, 3]) [3.0, 8.0, 14.0, 8.0, 3.0]
Complexity: O(n * m)
Source code in shortfx/fxNumeric/transform_functions.py
dft(signal: List[float]) -> List[Tuple[float, float]]
¶
Computes the Discrete Fourier Transform of a real signal.
X[k] = sum_{n=0}^{N-1} x[n] e^{-i 2 pi k n / N}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
List[float]
|
List of real-valued samples. |
required |
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float]]
|
List of (real, imag) tuples for each frequency bin. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If signal is not a list. |
ValueError
|
If signal is empty. |
Example
result = dft([1, 0, 1, 0]) (round(result[0][], 4), round(result[0][1], 4)) (2.0, 0.0)
Complexity: O(N^2) — use FFT for large signals.
Source code in shortfx/fxNumeric/transform_functions.py
discrete_cosine_transform(signal: list) -> list
¶
Computes the Type-II Discrete Cosine Transform (DCT-II).
X_k = Σ_{n=0}^{N-1} x_n cos(π(2n+1)k / (2N)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
list
|
Real-valued input signal. |
required |
Returns:
| Type | Description |
|---|---|
list
|
DCT coefficients. |
Example
dct = discrete_cosine_transform([1, 1, 1, 1]) round(dct[0], 4) 4.0
Complexity: O(N^2)
Source code in shortfx/fxNumeric/transform_functions.py
discrete_sine_transform(signal: list) -> list
¶
Computes the Type-I Discrete Sine Transform (DST-I).
X_k = Σ_{n=0}^{N-1} x_n sin(π(n+1)(k+1) / (N+1)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
list
|
Real-valued input signal. |
required |
Returns:
| Type | Description |
|---|---|
list
|
DST coefficients. |
Example
dst = discrete_sine_transform([1, 1, 1]) round(dst[0], 4) 2.4142
Complexity: O(N^2)
Source code in shortfx/fxNumeric/transform_functions.py
fourier_transform_numerical(f: Callable[[float], float], omega: float, t_range: Tuple[float, float] = (-50.0, 50.0), n_points: int = 2000) -> Tuple[float, float]
¶
Numerically approximates the Fourier transform F(omega).
F(w) = integral_{-inf}^{inf} f(t) e^{-i w t} dt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Time-domain function. |
required |
omega
|
float
|
Angular frequency. |
required |
t_range
|
Tuple[float, float]
|
Integration interval (t_min, t_max). |
(-50.0, 50.0)
|
n_points
|
int
|
Number of sample points. |
2000
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple (real_part, imaginary_part) of F(omega). |
Example
import math re, im = fourier_transform_numerical( ... lambda t: math.exp(-abs(t)), 0.0 ... ) round(re, 2) 2.0
Complexity: O(n_points)
Source code in shortfx/fxNumeric/transform_functions.py
hankel_transform_numerical(f: callable, k: float, nu: float = 0.0, a: float = 1e-06, b: float = 50.0, n: int = 2000) -> float
¶
Computes the Hankel transform H_ν{f}(k) = ∫_0^∞ f(r) J_ν(kr) r dr numerically.
Uses the trapezoidal rule. Requires bessel_j from special_functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
callable
|
Radial function f(r). |
required |
k
|
float
|
Transform variable. |
required |
nu
|
float
|
Order of the Bessel function (default 0). |
0.0
|
a
|
float
|
Lower limit (> 0). |
1e-06
|
b
|
float
|
Upper limit. |
50.0
|
n
|
int
|
Number of quadrature points. |
2000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate H_ν{f}(k). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or parameters not numeric. |
Example
import math round(hankel_transform_numerical(lambda r: math.exp(-r), 0, 0, 1e-6, 50, 2000), 2) 1.0
Complexity: O(n * Bessel_cost)
Source code in shortfx/fxNumeric/transform_functions.py
hartley_transform(signal: list) -> list
¶
Computes the discrete Hartley transform (DHT).
H_k = Σ_{n=0}^{N-1} x_n · cas(2πnk/N), where cas(θ) = cos(θ) + sin(θ).
The Hartley transform is its own inverse (up to 1/N normalisation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
list
|
Real-valued input signal. |
required |
Returns:
| Type | Description |
|---|---|
list
|
DHT coefficients. |
Example
dht = hartley_transform([1, 0, 0, 0]) [round(v, 4) for v in dht][1.0, 1.0, 1.0, 1.0]
Complexity: O(N^2)
Source code in shortfx/fxNumeric/transform_functions.py
hilbert_transform(signal: List[float]) -> List[float]
¶
Approximates the discrete Hilbert transform of a real signal.
The Hilbert transform produces the analytic signal's imaginary component, i.e., a 90° phase shift.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
List[float]
|
Real-valued input signal. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
Hilbert-transformed signal (imaginary part of analytic signal). |
Example
h = hilbert_transform([1, 0, -1, 0]) [round(v, 4) for v in h][0.0, -1.0, 0.0, 1.0]
Complexity: O(N^2) via DFT approach.
Source code in shortfx/fxNumeric/transform_functions.py
idft(spectrum: List[Tuple[float, float]]) -> List[float]
¶
Computes the Inverse Discrete Fourier Transform.
x[n] = (1/N) sum_{k=0}^{N-1} X[k] e^{i 2 pi k n / N}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spectrum
|
List[Tuple[float, float]]
|
List of (real, imag) tuples. |
required |
Returns:
| Type | Description |
|---|---|
List[float]
|
List of real-valued time-domain samples. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If spectrum is not a list. |
ValueError
|
If spectrum is empty. |
Example
signal = idft([(2, 0), (0, 0), (2, 0), (0, 0)]) [round(s, 4) for s in signal][1.0, 0.0, 1.0, 0.0]
Complexity: O(N^2)
Source code in shortfx/fxNumeric/transform_functions.py
inverse_discrete_cosine_transform(coefficients: list) -> list
¶
Computes the inverse Type-II DCT (DCT-III).
x_n = (2/N)(X_0/2 + Σ_{k=1}^{N-1} X_k cos(π(2n+1)k / (2N))).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefficients
|
list
|
DCT coefficients. |
required |
Returns:
| Type | Description |
|---|---|
list
|
Reconstructed signal. |
Example
idct = inverse_discrete_cosine_transform([4, 0, 0, 0]) [round(v, 4) for v in idct][1.0, 1.0, 1.0, 1.0]
Complexity: O(N^2)
Source code in shortfx/fxNumeric/transform_functions.py
inverse_hartley_transform(coefficients: list) -> list
¶
Computes the inverse discrete Hartley transform.
x_n = (1/N) Σ_{k=0}^{N-1} H_k · cas(2πnk/N).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefficients
|
list
|
DHT coefficients. |
required |
Returns:
| Type | Description |
|---|---|
list
|
Reconstructed signal. |
Example
inv = inverse_hartley_transform([1, 1, 1, 1]) [round(v, 4) for v in inv][1.0, 0.0, 0.0, 0.0]
Complexity: O(N^2)
Source code in shortfx/fxNumeric/transform_functions.py
inverse_laplace_gaver_stehfest(f_s: Callable[[float], float], t: float, n: int = 12) -> float
¶
Numerically inverts the Laplace transform using the Gaver-Stehfest algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_s
|
Callable[[float], float]
|
Laplace-domain function F(s). |
required |
t
|
float
|
Time at which to evaluate the inverse. |
required |
n
|
int
|
Algorithm order (must be even, typically 8-14). |
12
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate f(t). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If t is not numeric or n is not an integer. |
ValueError
|
If t <= 0 or n < 2 or n is odd. |
Example
round(inverse_laplace_gaver_stehfest(lambda s: 1/s, 1.0), 4) 1.0
Complexity: O(n^2) for coefficient computation + O(n) evaluations.
Source code in shortfx/fxNumeric/transform_functions.py
laplace_transform_numerical(f: Callable[[float], float], s: float, t_max: float = 50.0, n_points: int = 1000) -> float
¶
Numerically approximates the Laplace transform F(s) = integral_0^inf f(t) e^{-st} dt.
Uses the trapezoidal rule over [0, t_max].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable[[float], float]
|
Time-domain function f(t). |
required |
s
|
float
|
Complex frequency (real, > 0 for convergence). |
required |
t_max
|
float
|
Upper integration limit. |
50.0
|
n_points
|
int
|
Number of sample points. |
1000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate F(s). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If s is not numeric. |
ValueError
|
If s <= 0 or t_max <= 0 or n_points < 2. |
Example
round(laplace_transform_numerical(lambda t: 1, 1.0), 2) 1.0
Complexity: O(n_points)
Source code in shortfx/fxNumeric/transform_functions.py
mellin_transform_numerical(f: callable, s: float, a: float = 1e-06, b: float = 100.0, n: int = 2000) -> float
¶
Computes the Mellin transform M{f}(s) = ∫_0^∞ x^(s-1) f(x) dx numerically.
Uses the trapezoidal rule on a log-spaced grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
callable
|
Integrand function f(x). |
required |
s
|
float
|
Complex frequency parameter (real). |
required |
a
|
float
|
Lower integration limit (> 0, approximation of 0). |
1e-06
|
b
|
float
|
Upper integration limit (approximation of ∞). |
100.0
|
n
|
int
|
Number of quadrature points. |
2000
|
Returns:
| Type | Description |
|---|---|
float
|
Approximate M{f}(s). |
Raises:
| Type | Description |
|---|---|
TypeError
|
If f is not callable or s not numeric. |
Example
import math round(mellin_transform_numerical(lambda x: math.exp(-x), 2), 4) 1.0
Complexity: O(n)
Source code in shortfx/fxNumeric/transform_functions.py
z_transform_eval(x: List[float], z_real: float, z_imag: float) -> Tuple[float, float]
¶
Evaluates the Z-transform X(z) = sum_{n=0}^{N-1} x[n] z^{-n} at a given z.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
List[float]
|
Discrete-time signal [x[0], x[1], ...]. |
required |
z_real
|
float
|
Real part of z. |
required |
z_imag
|
float
|
Imaginary part of z. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple (real_part, imag_part) of X(z). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If z == 0 (except for x of length 1). |
Example
z_transform_eval([1, 1, 1], 2.0, 0.0) (1.75, 0.0)
Complexity: O(n)