Skip to content

Commit 1e546cb

Browse files
Github action: auto-update.
1 parent 7843e34 commit 1e546cb

87 files changed

Lines changed: 3348 additions & 503 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
.. _fourier_diff :
3+
4+
Fourier Differentiation
5+
========================================================
6+
An example of usage of our Fourier Differentiation Function on 1d data.
7+
"""
8+
9+
# %%
10+
# Import the library
11+
# ------------------
12+
# We first import our `neuralop` library and required dependencies.
13+
import torch
14+
import numpy as np
15+
import matplotlib.pyplot as plt
16+
from neuralop.losses.fourier_diff import fourier_derivative_1d
17+
18+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19+
20+
21+
22+
# %%
23+
# Creating an example of periodic 1D curve
24+
# --------------------
25+
# Here we consider sin(x) and cos(x), which are periodic on the interval [0,2pi]
26+
L = 2*torch.pi
27+
x = torch.linspace(0, L, 101)[:-1]
28+
f = torch.stack([torch.sin(x), torch.cos(x)], dim=0)
29+
x_np = x.cpu().numpy()
30+
31+
# %%
32+
# Differentiate the signal
33+
# -----------------------------------------
34+
# We use the Fourier differentiation function to differentiate the signal
35+
dfdx = fourier_derivative_1d(f, order=1, L=L)
36+
df2dx2 = fourier_derivative_1d(f, order=2, L=L)
37+
df3dx3 = fourier_derivative_1d(f, order=3, L=L)
38+
39+
40+
# %%
41+
# Plot the results for sin(x)
42+
# ----------------------
43+
plt.figure()
44+
plt.plot(x_np, dfdx[0].squeeze().cpu().numpy(), label='Fourier dfdx')
45+
plt.plot(x_np, np.cos(x_np), '--', label='dfdx')
46+
plt.plot(x_np, df2dx2[0].squeeze().cpu().numpy(), label='Fourier df2dx2')
47+
plt.plot(x_np, -np.sin(x_np), '--', label='df2dx2')
48+
plt.plot(x_np, df3dx3[0].squeeze().cpu().numpy(), label='Fourier df3dx3')
49+
plt.plot(x_np, -np.cos(x_np), '--', label='df3dx3')
50+
plt.xlabel('x')
51+
plt.legend()
52+
plt.show()
53+
54+
# %%
55+
# Plot the results for cos(x)
56+
# ----------------------
57+
plt.figure()
58+
plt.plot(x_np, dfdx[1].squeeze().cpu().numpy(), label='Fourier dfdx')
59+
plt.plot(x_np, -np.sin(x_np), '--', label='dfdx')
60+
plt.plot(x_np, df2dx2[1].squeeze().cpu().numpy(), label='Fourier df2dx2')
61+
plt.plot(x_np, -np.cos(x_np), '--', label='df2dx2')
62+
plt.plot(x_np, df3dx3[1].squeeze().cpu().numpy(), label='Fourier df3dx3')
63+
plt.plot(x_np, np.sin(x_np), '--', label='df3dx3')
64+
plt.xlabel('x')
65+
plt.legend()
66+
plt.show()
67+
68+
69+
70+
# %%
71+
# Creating an example of non-periodic 1D curve
72+
# --------------------
73+
# Here we consider sin(16x)-cos(8x) and exp(-0.8x)+sin(x)
74+
L = 2*torch.pi
75+
x = torch.linspace(0, L, 101)[:-1]
76+
f = torch.stack([torch.sin(3*x) - torch.cos(x), torch.exp(-0.8*x)+torch.sin(x)], dim=0)
77+
x_np = x.cpu().numpy()
78+
79+
# %%
80+
# Differentiate the signal
81+
# -----------------------------------------
82+
# We use the Fourier differentiation function with Fourier continuation to differentiate the signal
83+
dfdx = fourier_derivative_1d(f, order=1, L=L, use_FC='Legendre', FC_d=4, FC_n_additional_pts=30, FC_one_sided=False)
84+
df2dx2 = fourier_derivative_1d(f, order=2, L=L, use_FC='Legendre', FC_d=4, FC_n_additional_pts=30, FC_one_sided=False)
85+
86+
87+
# %%
88+
# Plot the results for sin(16x)-cos(8x)
89+
# ----------------------
90+
plt.figure()
91+
plt.plot(x_np, dfdx[0].squeeze().cpu().numpy(), label='Fourier dfdx')
92+
plt.plot(x_np, 3*torch.cos(3*x) + torch.sin(x), '--', label='dfdx')
93+
plt.plot(x_np, df2dx2[0].squeeze().cpu().numpy(), label='Fourier df2dx2')
94+
plt.plot(x_np, -9*torch.sin(3*x) + torch.cos(x), '--', label='df2dx2')
95+
plt.xlabel('x')
96+
plt.legend()
97+
plt.show()
98+
99+
# %%
100+
# Plot the results for exp(-0.8x)+sin(x)
101+
# ----------------------
102+
plt.figure()
103+
plt.plot(x_np, dfdx[1].squeeze().cpu().numpy(), label='Fourier dfdx')
104+
plt.plot(x_np, -0.8*torch.exp(-0.8*x)+torch.cos(x), '--', label='dfdx')
105+
plt.plot(x_np, df2dx2[1].squeeze().cpu().numpy(), label='Fourier df2dx2')
106+
plt.plot(x_np, 0.64*torch.exp(-0.8*x)-torch.sin(x), '--', label='df2dx2')
107+
plt.xlabel('x')
108+
plt.legend()
109+
plt.show()

0 commit comments

Comments
 (0)