-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
175 lines (133 loc) · 3.29 KB
/
encode.py
File metadata and controls
175 lines (133 loc) · 3.29 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
Photodiode-based encoding of experimental metadata using pulse-duration modulation.
This script demonstrates how to embed experiment metadata into a recorded
photodiode channel by encoding characters as sequences of light pulses
with different durations.
"""
from psychopy import visual, core
import random
# =========================
# Window setup
# =========================
win = visual.Window(
size=[1920, 1080],
color="#FFFFFF",
fullscr=True,
monitor="LG",
screen=1,
waitBlanking=False,
)
# =========================
# Background
# =========================
background = visual.Rect(
win,
size=(10, 10),
fillColor="#FFFFFF",
pos=(0, 0),
)
# =========================
# Photodiode square setup
# =========================
DIODE_SIZE = (0.3, 0.3)
DIODE_POSITION = (0.9, -0.9)
diode_off = visual.Rect(
win,
size=DIODE_SIZE,
pos=DIODE_POSITION,
fillColor="#000000",
lineColor="#000000",
)
diode_on = visual.Rect(
win,
size=DIODE_SIZE,
pos=DIODE_POSITION,
fillColor="#FFFFFF",
lineColor="#FFFFFF",
)
# =========================
# Experimental metadata
# =========================
experiment_id = "D"
stimuli_pairs = [
("stim_a", "stim_b"),
("stim_c", "stim_d"),
("stim_e", "stim_f"),
]
order = list(range(len(stimuli_pairs)))
random.shuffle(order)
order_string = "".join(str(i) for i in order)
# =========================
# Encoding function
# =========================
def encode_word_via_diode(
word,
win,
square_off,
square_on,
short_duration=0.02,
long_duration=0.08,
gap_duration=0.1,
):
"""
Encode a string as a sequence of photodiode light pulses.
Encoding scheme:
- Each character is converted to 8-bit ASCII.
- Bit '0' -> short pulse
- Bit '1' -> long pulse
- OFF state is represented by square_off.
- Start and stop markers are long pulses.
Parameters
----------
word : str
String to encode.
win : psychopy.visual.Window
PsychoPy window object.
square_off : psychopy.visual.Rect
Photodiode square in OFF state.
square_on : psychopy.visual.Rect
Photodiode square in ON state.
short_duration : float
Duration (s) of short pulse.
long_duration : float
Duration (s) of long pulse.
gap_duration : float
Inter-pulse gap duration (s).
"""
# Start marker
square_on.draw()
win.flip()
core.wait(long_duration)
square_off.draw()
win.flip()
core.wait(gap_duration)
for char in word:
binary_str = format(ord(char), "08b")
for bit in binary_str:
square_on.draw()
win.flip()
core.wait(short_duration if bit == "0" else long_duration)
square_off.draw()
win.flip()
core.wait(gap_duration)
# Stop marker
square_on.draw()
win.flip()
core.wait(long_duration)
square_off.draw()
win.flip()
# =========================
# Run encoding
# =========================
background.draw()
diode_off.draw()
win.flip()
core.wait(2)
encode_word_via_diode(experiment_id, win, diode_off, diode_on)
core.wait(1)
encode_word_via_diode(order_string, win, diode_off, diode_on)
diode_off.draw()
win.flip()
core.wait(5)
win.close()
core.quit()