Skip to content

Instantly share code, notes, and snippets.

@circuit4u-medium
Created December 14, 2019 14:09
Show Gist options
  • Save circuit4u-medium/8ba8eb915ffe705458a86fad4075356b to your computer and use it in GitHub Desktop.
Save circuit4u-medium/8ba8eb915ffe705458a86fad4075356b to your computer and use it in GitHub Desktop.
J code to read NEV and NS5 file
require 'jmf'
require 'plot'
NB. turn bytes into unsigned ints (little endian)
touint =: 256 #. a. i. |.
NB. turn bytes into signed ints
toint2 =: _1 & ic
toint4 =: _2 & ic
NB. top level API
read_nev_file =: 3 : 0
JCHAR map_jmf_ 'dnev'; y NB. y = NEV file name
p =. read_nev dnev
unmap_jmf_ 'dnev'
p NB. return a pair of data_address and Electrode ID
)
read_ns_file =: 3 : 0
JCHAR map_jmf_ 'dns'; y
r =. read_ns dns
unmap_jmf_ 'dns'
r
)
NB. plot NEUEVWAV file x=data_addr, y=filename
plot_nev_file =: 4 : 0
JCHAR map_jmf_ 'dnev'; y
plot ,toint2"1 (x + (((112 - 8) % 2), 2) $ i.112) { dnev
unmap_jmf_ 'dnev'
)
NB. plot NEURALCD file x= multiple data_addr, y=filename
plot_ns_file =: 4 : 0
JCHAR map_jmf_ 'dns'; y
plot ,toint2"1 (x +/ i.2) { dns
unmap_jmf_ 'dns'
)
NB. inner functions
read_nev =: 3 : 0
smoutput 'FILE SIZE:'; (# y);'BYTES' NB. Y = MAPPED FILE TO VAR
smoutput 'PACKET ID: '; (i.8){y NB. NEURALEV
pt =. touint (12 + i.4) {y
smoutput 'DATA PT: '; pt
ptsize =. touint (16 + i.4) {y
smoutput 'BYTES/PACKET: '; ptsize
smoutput 'SAMPLE RATE: '; touint (24 + i.4) {y
smoutput 'APP: '; (44 + i.32) { y
hd_cnt =. touint (332 + i.4) { y
smoutput 'EXT HEADER #: '; hd_cnt
NB. ext. header of 32-byte each
pid_addr =. 336 + 32*i.hd_cnt
pid =. (pid_addr +/ i.8) { y
smoutput ((;/(~. pid)) ,. ;/ +/"1 =pid)
NB. PACKET ID for NEUEVWAV
wav_addr =. ('NEUEVWAV' -:"1 pid) # pid_addr
eid =. (touint"1) ( (wav_addr + 8) +/ i.2) { y
smp =. touint"0 (wav_addr + 21) { y
smoutput 'NEUEVWAV EID: ';eid
smoutput 'NEUEVWAV BYTES/SAMPLE: ';smp
NB. data of ptsize-byte each
cnt =. ((#y) - pt) % ptsize NB. total # of data packet of fixed width size
data_addr =. pt + ptsize * i.cnt
data_pid =. (touint"1) ((data_addr + 4) +/ i.2) { y
data_addr,. data_pid
)
read_ns =: 3 : 0
smoutput 'FILE SIZE:'; (# y); 'BYTES' NB. Y=MAPPED FILE TO VAR
smoutput 'PACKET ID: '; (i.8){y
pt =. touint (10 + i.4) {y
smoutput 'DATA PT: '; pt
smoutput 'APP: '; (230 + i.52) { y
ch_cnt =: touint (310 + i.4) {y
smoutput 'CHANNEL #: '; ch_cnt
NB. ext. header of 66-byte each
pid_addr =. 314 + 66*i.ch_cnt
eid =. (touint"1) ( (pid_addr + 2) +/ i.2) { y
smoutput 'eid'; eid
NB. data section
total =. touint (pt + 5 + i.4) { y
smoutput 'DATA POINTS:' ; total
(pt + 9); total; eid
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment