-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpolation.py
More file actions
50 lines (44 loc) · 2.01 KB
/
interpolation.py
File metadata and controls
50 lines (44 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
''' Several interpolating functions, Π(x), Λ(x) and ϕ(x) or sinc(x) (a.k.a. ξ(x)) for that matter...
Whatchamacallit this thingamajig:
* The window/rectangular and the hat/tent/triangular and Keys & sinc (thus not anonymous!) functions.
* The Keys' cubic interpolating function (b'cause: https://realpython.com/python-lambda/#syntax)'''
def Π(x, l = -.5, r = .5): return (x >= l) * (x < r)
def Λ(x): return (1 - abs(x)) * Π(x, -1, 1)
def ϕ(x):
x, α = abs(x), -.5
return (((α + 2) * x**3 - (α + 3) * x**2 + 1) * Π(x, 0, 1)
+ ( α * x**3 - 5*α * x**2 + 8*α * x - 4*α) * Π(x, 0, 2) * (1 - Π(x, 0, 1)))
# When our intuition goes astray... [3B1B] https://www.youtube.com/watch?v=851U557j6HE
from numpy import sin, ones_like, pi as π
def ξ(x, m = π):
x = x * m; sinc = ones_like(x)
sinc[x != 0] = sin(x[x != 0])/(x[x != 0])
return sinc # sinc for syncope?
## Or, simply...
#from numpy import sinc
# ... instead!
from numpy import arange, linspace
# An interpolation routine: f(n) ➞ f(x) using φ = Π, Λ, ϕ or ξ
def Φ(x, f, φ):
n = arange(len(f))
ψ = φ(x - n)
return ψ @ f
# An actual interpolation Φ: Fn ➞ Fx
def interpolate(fn, N, φ = [Π, Λ, ϕ, ξ]):
X = linspace(0, len(fn), N)
return [[Φ(x, fn, ψ) for x in X] for ψ in φ] if type(φ) is list else [Φ(x, fn, φ) for x in X]
# A source image...
from random import randrange as RR
from numpy import array
ζ = RR(0b10); δ = ζ ^ 0b1
eddie = array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, δ, 1, 0, 0, 1, δ, δ, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 1, ζ, 1, 1, 1, ζ, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, δ, δ, δ, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])