Skip to content

Instantly share code, notes, and snippets.

@hccho2
Created December 23, 2019 06:26
Show Gist options
  • Save hccho2/44f93fb34ff0470b8c2eec3214c84e7c to your computer and use it in GitHub Desktop.
Save hccho2/44f93fb34ff0470b8c2eec3214c84e7c to your computer and use it in GitHub Desktop.
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
tf.compat.v1.reset_default_graph()
x = np.sin(np.linspace(0,3*np.pi,30))+ np.random.randn(30)*0.1
filter = np.sin(np.linspace(np.pi/2-np.pi/6,np.pi/2+np.pi/6,5))
x = np.array([ 0.15056179, 0.30541199, 0.72807816, 0.940651 , 0.97211703,
1.01255197, 0.86129876, 0.71344136, 0.56986685, 0.24477384,
-0.01567925, -0.29397656, -0.7427353 , -0.96051523, -0.92409656,
-1.09632185, -1.03468747, -0.70415137, -0.43263368, -0.10620422,
0.00126833, 0.50176941, 0.74617082, 1.01136882, 1.01184374,
1.02194784, 0.81796091, 0.4132255 , 0.38143445, 0.07454849])
filter = np.array([0.8660254 , 0.96592583, 1. , 0.96592583, 0.8660254 ])
x_ = tf.reshape(tf.convert_to_tensor(x),[1,-1,1])
filter_ = tf.reshape(tf.convert_to_tensor(filter),[-1,1,1])
y_ = tf.squeeze(tf.nn.conv1d(x_,filter_,stride=1,padding='VALID'))
sess= tf.Session()
y=sess.run(y_)
z1, = plt.plot(np.arange(30),x,label='data',marker='o')
z2, = plt.plot(np.arange(5),filter*3,label='filter',marker='o')
z3, =plt.plot(np.arange(1,27),y*0.5,label='result',marker='o')
plt.legend(handles=[z1,z2,z3],loc='upper center')
@hccho2
Copy link
Author

hccho2 commented Dec 23, 2019

비대칭 filter인 경우, 결과가 일치하는 patten에서 최대치를 형성하지는 않는다.
예를 들어, sin곡선의 앞부분을 filter로 했을 때, 원래 sin곡선보다 약간 왼쪽으로 shift된 결과를 얻는다.

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
tf.compat.v1.reset_default_graph()

x_without_noise = np.array([ 0.00000000e+00,  3.09016994e-01,  5.87785252e-01,  8.09016994e-01,
        9.51056516e-01,  1.00000000e+00,  9.51056516e-01,  8.09016994e-01,
        5.87785252e-01,  3.09016994e-01,  1.22464680e-16, -3.09016994e-01,
       -5.87785252e-01, -8.09016994e-01, -9.51056516e-01, -1.00000000e+00,
       -9.51056516e-01, -8.09016994e-01, -5.87785252e-01, -3.09016994e-01,
       -2.44929360e-16,  3.09016994e-01,  5.87785252e-01,  8.09016994e-01,
        9.51056516e-01,  1.00000000e+00,  9.51056516e-01,  8.09016994e-01,
        5.87785252e-01,  3.09016994e-01,  3.67394040e-16])


x = np.sin(np.linspace(0,3*np.pi,31))+ np.random.randn(31)*0.1
filter = np.sin(np.linspace(np.pi/2-np.pi/6,np.pi/2+np.pi/6,5))
filter = np.sin(np.linspace(np.pi/2,np.pi,5))
x = np.array([-8.17646108e-02,  3.13663973e-01,  3.91689052e-01,  6.67779942e-01,
        8.65920525e-01,  1.20870896e+00,  1.00749242e+00,  9.11470321e-01,
        4.79132615e-01,  3.58446563e-01,  4.25711653e-02, -2.85954410e-01,
       -6.10904871e-01, -8.21197786e-01, -1.04441306e+00, -8.63673798e-01,
       -9.76416769e-01, -7.78509796e-01, -6.09985997e-01, -4.90318041e-01,
       -1.80778486e-01,  2.61383469e-01,  5.55101307e-01,  6.89418144e-01,
        8.06513299e-01,  9.04051245e-01,  8.34397936e-01,  7.39944771e-01,
        5.40850263e-01,  3.35311373e-01,  4.03139922e-04])
#filter = np.array([8.09016994e-01, 5.87785252e-01,  3.09016994e-01,  1.22464680e-16, -3.09016994e-01])
filter = np.array([0.00000000e+00,  3.09016994e-01,  5.87785252e-01,  8.09016994e-01,9.51056516e-01])
x_ = tf.reshape(tf.convert_to_tensor(x),[1,-1,1])
filter_ = tf.reshape(tf.convert_to_tensor(filter),[-1,1,1])

y_ = tf.squeeze(tf.nn.conv1d(x_,filter_,stride=1,padding='VALID'))

sess= tf.Session()
y=sess.run(y_)

z1, = plt.plot(np.arange(31),x,label='data',marker='o')
z2, = plt.plot(np.arange(5),filter+2,label='filter',marker='o')
z3, =plt.plot(np.arange(2,29),y*0.5,label='result',marker='o')

plt.legend(handles=[z1,z2,z3],loc='upper center')

@hccho2
Copy link
Author

hccho2 commented Dec 23, 2019

  • filter = np.array([0.8660254 , 0.96592583, 1. , 0.96592583, 0.8660254 ])

convolution

  • filter = np.array([0.00000000e+00, 3.09016994e-01, 5.87785252e-01, 8.09016994e-01,9.51056516e-01])

convolution2

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