2024-10-17 18:30:12 +03:00
|
|
|
import gzip
|
2024-10-17 18:19:47 +03:00
|
|
|
import numpy as np
|
|
|
|
|
2024-10-17 18:30:12 +03:00
|
|
|
def read_images(filename):
|
|
|
|
with gzip.open(filename, 'rb') as f:
|
|
|
|
f.read(16)
|
|
|
|
return np.frombuffer(f.read(), dtype=np.uint8).reshape(-1, 28, 28)
|
2024-10-17 18:19:47 +03:00
|
|
|
|
2024-10-17 18:30:12 +03:00
|
|
|
def pixel_to_stimulation(pixel_value, max_stimulation=5.0):
|
2024-10-17 19:24:48 +03:00
|
|
|
return int(pixel_value / 255.0 * max_stimulation * 51) # Convert to 0-255 range
|
2024-10-17 18:19:47 +03:00
|
|
|
|
2024-10-17 18:30:12 +03:00
|
|
|
def read_labels(filename):
|
|
|
|
with gzip.open(filename, 'rb') as f:
|
|
|
|
f.read(8)
|
|
|
|
return np.frombuffer(f.read(), dtype=np.uint8)
|
2024-10-17 18:19:47 +03:00
|
|
|
|
2024-10-17 18:30:12 +03:00
|
|
|
train_images = read_images('dataset/train-images-idx3-ubyte.gz')
|
|
|
|
train_labels = read_labels('dataset/train-labels-idx1-ubyte.gz')
|
2024-10-17 18:19:47 +03:00
|
|
|
|
2024-10-17 18:30:12 +03:00
|
|
|
indices_of_nines = np.where(train_labels == 9)[0]
|
|
|
|
image_of_nine = train_images[indices_of_nines[0]]
|
2024-10-17 18:19:47 +03:00
|
|
|
|
2024-10-17 19:24:48 +03:00
|
|
|
stimulation_pattern = np.vectorize(pixel_to_stimulation)(image_of_nine)
|
2024-10-17 18:19:47 +03:00
|
|
|
|
2024-10-17 19:24:48 +03:00
|
|
|
# Output the stimulation pattern as a compact byte array
|
|
|
|
with open('stimulation_pattern.h', 'w') as f:
|
|
|
|
f.write("const PROGMEM uint8_t stimulation_pattern[] = {")
|
|
|
|
f.write(", ".join(map(str, stimulation_pattern.flatten())))
|
|
|
|
f.write("};\n")
|
|
|
|
f.write(f"const uint16_t PATTERN_SIZE = {stimulation_pattern.size};\n")
|