-
Notifications
You must be signed in to change notification settings - Fork 1
/
ECC.py
127 lines (103 loc) · 3.5 KB
/
ECC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import random
import time
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.pyplot import imshow, show
from PIL import Image
# ======= #
# Globals #
# ======= #
# Single Event Upsets in ms
# exagerated for teaching purposes
SEU_per_ms = 1
SEU_multi = 10
# if ECC is enabled or not
ECC_enabled = False
# ECC correction opportunties
ECC_correction_rate = 500*SEU_multi
ECC_wait_count = 0
# the source image to be tested
img_src = "img/carl3.png"
# the figure for display
fig = plt.figure()
# load the initial image
im0 = Image.open(img_src) # Can be many different formats.
im1 = Image.open(img_src) # Can be many different formats.
im2 = Image.open(img_src) # Can be many different formats.
im_total = Image.open(img_src) # Can be many different formats.
pix0 = im0.load()
pix1 = im1.load()
pix2 = im2.load()
# to display the total flips
pix_total = im_total.load()
print 'loaded ' + str(img_src) + ' of size: ' + str(im_total.size) # Get the width and hight of the image for iterating over
ani_im = plt.imshow(im_total, animated=True)
# =================================================== #
# EDIT THIS CODE #
# =================================================== #
# Make the parity array
parity_array = []
# Do parity array here
def ECC():
global pix_total, parity_array
# Basically just ignore pix0, pix1, and pix2
# and just focus on pix_total, make a parity bit
# system for this pixel array
print "Running ECC ..."
# from: https://matplotlib.org/examples/animation/dynamic_image.html
def updatefig(*args):
global ECC_enabled, ECC_wait_count
global pix0, pix1, pix2, pix_total
for x in range(0,SEU_multi):
# find the location to flip a bit
x_flip = random.randint(0,im_total.size[0]-1)
y_flip = random.randint(0,im_total.size[1]-1)
i_f = random.randint(0,2)
# choose the image that will have a bit flipped
color = pix_total[x_flip,y_flip]
if (i_f == 0):
color = pix0[x_flip,y_flip]
elif (i_f == 1):
color = pix1[x_flip,y_flip]
elif (i_f == 2):
color = pix2[x_flip,y_flip]
# this is wher the ECC can occur
if (ECC_wait_count == ECC_correction_rate):
ECC_wait_count = 0
ECC()
else:
ECC_wait_count += 1
# find where to flip the bit
c_flip = random.randint(0,2)
partial_color = 0
n = random.randint(0,7)
val = 2**n
# flip the bits
if (int(color[c_flip]) + val > 255):
partial_color = color[c_flip] - val
else:
partial_color = color[c_flip] + val
# the 3 flip options
if c_flip == 0:
color = (c_flip,color[1],color[2])
elif c_flip == 1:
color = (color[0],c_flip,color[2])
elif c_flip == 2:
color = (color[0],color[1],c_flip)
# now actually change the color!
if (i_f == 0):
pix0[x_flip,y_flip] = color
elif (i_f == 1):
pix1[x_flip,y_flip] = color
elif (i_f == 2):
pix2[x_flip,y_flip] = color
pix_total[x_flip,y_flip] = (color)
color = pix_total[x_flip,y_flip]
# set and return for the animation loop
ani_im.set_array(im_total)
return ani_im,
# show the animation
ani = animation.FuncAnimation(fig, updatefig, interval=SEU_per_ms, blit=True)
# ani.save('img/animation.gif', writer='imagemagick', fps=60)
plt.show()