import numpy as np import matplotlib.pyplot as plt Generate a 1D curve npoints =…
- User
import numpy as np import matplotlib.pyplot as plt
Generate a 1D curve
n_points = np.random.randint(10, 51) x = np.linspace(0, 10, n_points) y = np.sin(x) + np.random.normal(0, 0.1, size=n_points) # Example curve with added noise
Plot the original curve
plt.plot(x, y, marker='o', linestyle='-', label='Original Curve') plt.title('Original 1D Curve') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() plt.show()
def filter_function(size): return np.ones(size) / size
filter_size = np.random.randint(3, 6) filter_weights = filter_function(filter_size)
Apply convolution
convolved_y = np.convolve(y, filter_weights, mode='same')
Plot the convolved curve
plt.plot(x, y, marker='o', linestyle='-', label='Original Curve') plt.plot(x, convolved_y, marker='x', linestyle='-', label=f'Convolved Curve (Filter Size: {filter_size})') plt.title('Convolved 1D Curve') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() plt.show()
stride = 2 convolved_y_stride = np.convolve(y, filter_weights, mode='same')[::stride]
Plot the convolved curve with stride
plt.plot(x, y, marker='o', linestyle='-', label='Original Curve') plt.plot(x[::stride], convolved_y_stride, marker='x', linestyle='-', label=f'Convolved Curve (Stride: {stride})') plt.title('Convolved 1D Curve with Stride') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() plt.show()
pool_size = 3 max_pooled_y = np.array([np.max(convolved_y[i:i+pool_size]) for i in range(0, len(convolved_y)-pool_size+1)])
Plot the max-pooled curve
plt.plot(x, y, marker='o', linestyle='-', label='Original Curve') plt.plot(x, convolved_y, marker='x', linestyle='-', label=f'Convolved Curve (Filter Size: {filter_size})') plt.plot(x[:len(max_pooled_y)], max_pooled_y, marker='^', linestyle='-', label=f'Max-Pooled Curve (Pool Size: {pool_size})') plt.title('Max-Pooled 1D Curve') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() plt.show()
- Assistant