上一个博客说明了将原始图片转化感受野的图片
这一节说明如何将图片对应的灰度值转化为脉冲序列
import numpy as np
from numpy import interp
T = 150 # 时钟周期为150,在150个cycle中产生脉冲
# 定义将图片像素值转化为脉冲的函数encode
def encode(pot):
# 假设所有的点对应的像素点对应一个二元数组train
train = []
for i in range(pot.shape[0]):
for j in range(pot.shape[1]):
# 初始化一个像素点对应的序列
temp = np.zeros([(T+1),])
# 一个像素点的灰度值经过感受野区间在[-1.069, 2.78]
# 灰度值和人脑的神经元发放频率[1, 20]Hz成正比
freq = interp(pot[i][j], [-1.069,2.781], [1,20])
assert freq > 0
# 计算每隔多少cycle发放一次
freq1 = math.ceil(600/freq)
k = freq1
if(pot[i][j]>0):
while k<(T+1):
temp[int(k)] = 1
k = k + freq1
train.append(temp)
return train
把对应的输入层图片传递给encode函数
train = np.array(encode(pot))
print(train.shape)
print(train[1].shape)
print(train[1])