Skip to content

Instantly share code, notes, and snippets.

@m-tmatma
Created November 7, 2021 12:00
Show Gist options
  • Save m-tmatma/dbae9a27d4ec9427035b56cdee0d8406 to your computer and use it in GitHub Desktop.
Save m-tmatma/dbae9a27d4ec9427035b56cdee0d8406 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import math
def valid_convolve(x, y, n):
b = np.ones(n)/ n
y2 = np.convolve(y, b, mode="valid")
return (x[n-1:], y2)
def convolve_gradient(x, y, n, dx = 1):
x2, y2 = valid_convolve(x, y, n)
y3 = np.gradient(y2, dx)
return (x2, y3)
x = np.linspace(0, 10, 500)
y1 = x * x + 3 * x + 10
y2 = y1 + np.random.randn(500)*0.3 # ノイズを混ぜる
fig = plt.figure()
ax = []
ax.append(fig.add_subplot(1, 3, 1))
ax[-1].plot(x, y1 ,linewidth=1)
ax[-1].plot(x, y2 ,linewidth=1)
#ax.append(fig.add_subplot(1, 3, 2))
X, Y = valid_convolve(x, y2, 100)
ax[-1].plot(X, Y ,linewidth=1)
ax.append(fig.add_subplot(1, 3, 2))
X, Y = convolve_gradient(x, y2, 100)
ax[-1].plot(X, Y ,linewidth=1)
ax.append(fig.add_subplot(1, 3, 3))
X2, Y2 = convolve_gradient(X, Y, 100)
ax[-1].plot(X2, Y2 ,linewidth=1)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment