Skip to content

Instantly share code, notes, and snippets.

@ahwillia
Created October 28, 2023 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahwillia/9cf797ce2e43dfd720546ffd09d83102 to your computer and use it in GitHub Desktop.
Save ahwillia/9cf797ce2e43dfd720546ffd09d83102 to your computer and use it in GitHub Desktop.
raised cosine basis functions
import matplotlib.pyplot as plt
import numpy as np
def raised_cos(t, loc, scale, amp):
"""
Raised, 1d-cosine basis functions tiling [0, 2 * pi)
These functions have the property of summing to a
constant amplitude at all points (i.e. uniform
tiling of space).
Parameters
----------
t : array
Points to evaluate function.
loc : float
Peak of the basis function.
scale : float
Scaling factor for t, larger values result in more
sharply peaked basis functions.
amp : float
Amplitude of the tuninf
"""
x = ((t - loc) + np.pi) % (2 * np.pi) - np.pi
x = np.clip(scale * x, -np.pi, np.pi)
return 0.5 * amp * (1 + np.cos(x))
if __name__ == "__main__":
fig, axes = plt.subplots(3, 2, sharey=True)
tt = np.linspace(-np.pi, np.pi, 200)
for ax, scale in zip(axes, [1, 2, 3]):
locs = np.linspace(-np.pi, np.pi, 2 * scale + 1)[:-1]
z = np.zeros_like(tt)
for loc in locs:
ax[0].plot(raised_cos(tt, loc, scale, 1))
z += raised_cos(tt, loc, scale, 1)
ax[1].plot(z)
axes[0, 0].set_title("basis functions")
axes[0, 1].set_title("sum of basis functions")
fig.tight_layout()
@ahwillia
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment