Skip to content

Instantly share code, notes, and snippets.

@hexgnu
Created March 20, 2023 18:08
Show Gist options
  • Save hexgnu/050b32838d27a1291d391d950b77820f to your computer and use it in GitHub Desktop.
Save hexgnu/050b32838d27a1291d391d950b77820f to your computer and use it in GitHub Desktop.
import math
def conv1d(input_array, input_filter):
window_size = len(input_filter)
window_norm = math.sqrt(sum([x**2 for x in input_filter]))
output_array = []
for i in range(window_size, len(input_array) + 1):
cur_window = input_array[(i - window_size):i]
cur_norm = math.sqrt(sum([x**2 for x in cur_window]))
cur_dot = sum(
[
(x / cur_norm) * (y / window_norm)
for x, y in zip(cur_window, input_filter)
]
)
output_array.append(cur_dot)
return output_array
def match(input_array, input_filter):
return any(
map(
lambda x: (x - 1) ** 2 < 1e-10,
conv1d(input_array, input_filter)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment