Пожалуйста, посмотрите, поможет ли это,
# unpadded list
listA = [1, 2, 3, 4, 5, 6]
# 1x3 kernel
kernelA = [2, 4, 2]
# 1x5 kernel
kernelB = [1, 1, 1, 1, 1]
#kernel = kernelA
kernel = kernelB
loopVal = len(kernel) >> 1
#print(loopVal)
"""
# padded list on both sides
listB = listA.copy()
for i in range(0, loopVal):
listB.insert(0, 0)
listB.append(0)
print(listB)
print()
"""
# Full padded list on one side
listB = listA.copy()
for i in range(0, len(kernel) - 1):
listB.append(0)
print(listB)
print()
## Does not use padded list
#sample = listA
## uses padded list
sample = listB
output = []
for i in range(loopVal, len(sample) - loopVal):
tmp = 0
for j in range(0, len(kernel)):
print(sample[i - loopVal + j], kernel[j])
tmp += sample[i - loopVal + j] * kernel[j]
output.append(tmp)
print(tmp)
print()
print("1D convolution output:")
print(output)
Вывод:
[1, 2, 3, 4, 5, 6, 0, 0, 0, 0]
1 1
2 1
3 1
4 1
5 1
15
2 1
3 1
4 1
5 1
6 1
20
3 1
4 1
5 1
6 1
0 1
18
4 1
5 1
6 1
0 1
0 1
15
5 1
6 1
0 1
0 1
0 1
11
6 1
0 1
0 1
0 1
0 1
6
1D convolution output:
[15, 20, 18, 15, 11, 6]