NB. Some key-word definitions

from         =. {
alphabet     =. a.
index_of     =. i.
transpose    =. |:
shape        =. $
antibase     =. #:
ravel        =. ,
reverse      =. |.
rank         =. "
append_items =. ,
not_equal    =. ~:
insert       =. /
append       =. ,.
residue      =. |
dot_product  =. .
table        =. /
times        =. *
integers     =. i.
drop         =. }.
take         =. {.
shape_of     =. $
member       =. e.
and          =. *.
copy         =. #
link         =. ;
power        =. ^:
exponential  =. ^
open         =. >
box          =. <
write        =. 1!:2
read_script  =. (0!:2)&<

NB. ascii_to_char

NB. This function converts ascii code values to characters

ascii_to_char =. 3 : 0
y. from alphabet
:
''
)

NB. char_to_ascii

NB. This function converts a character vector msg to a
NB. vector of ascii codes

char_to_ascii =. 3 : 0
alphabet index_of y.
:
''
)

NB. ascii_to_binary

NB. This function converts a vector of ascii codes to their
NB. binary representation.  The first ascii value is represented
NB. in column 1, with the least significant bit at the bottom
NB. of the column, the second column represents the second ascii
NB. value, etc.

ascii_to_binary =. 3 : 0
transpose (7 shape 2) antibase , y.
:
''
)

NB. char_to_ascii_bitstream

char_to_ascii_bitstream =. 3 : 0
ravel reverse rank 1 transpose 0 append_items ascii_to_binary char_to_ascii y.
:
''
)

NB. Some definitions for parity

even_parity =. 0
odd_parity  =. 1

NB. vrc

NB. This function computes the vertical redundancy check bits.
NB. This function adds a first row of VRC (ordinary parity) bits
NB. to a table of binary representations of ascii values.
NB. We assume that the columns of binary represent the bits,
NB. most significant to least significant, top to bottom.  This
NB. is the form of the result of ascii_to_binary.  The vrc bits
NB. are added as an 8th row at the top of y.  The argument x.
NB. is a parity code which indicates the type of parity.
NB. x. =. 0 ==> even parity, x. =. 1 ==> odd parity

vrc =. 3 : 0
''
:
(x. not_equal not_equal insert y.) append_items y.
)

NB. lrc

NB. This function computes the longitudinal redundancy check bits
NB. The function adds a column of lrc bits to a table of binary
NB. representations of ascii values.  We assume that the columns
NB. represent the bits most significant to least significant, top
NB. to bottom.  This is the form of the result of ascii_to_binary.
NB. The lrc bits are added as a last column of y.  The argument x.
NB. is a parity code which indicates the type of parity.
NB. x. =. 0 ==> even parity, x. =. 1 ==> odd parity

lrc =. 3 : 0
''
:
y. append x. not_equal not_equal insert rank 1 y.
)

NB. residue_table
 
NB. This function computes the operation table for the integers
NB. residue y. (excluding zero of course) under the operation of
NB. multiplication.  That is, the entries in the table are formed
NB. by computing all products (excluding zero) of elements in
NB. integers y. and reducing them by y. residue.

residue_table =. 3 : 0
y. residue (1 drop integers y.) times table 1 drop integers y.
:
''
)

NB. generator

generator =. 3 : 0
t =. 1 drop integers y.
(and insert rank 1 t member rank 1 y. residue t exponential table t) copy t
:
''
)

NB. a sample message to work with

msg_bits =. char_to_ascii_bitstream 'Watson, come quickly'

NB. crc16
NB. This function computes the crc16 check bits for a message y.

crc16 =. 3 : 0
bcc =. 16 shape 0
while. 0 not_equal shape y. do.
  ser_quo =. (1 take bcc) not_equal 1 take y.
  bcc =. (0 from bcc),(ser_quo not_equal 1 from bcc),(2 3 4 5 6 7 8 9 10 11 12 13 from bcc),(ser_quo not_equal 14 from bcc),15 from bcc
  bcc =. (1 drop bcc), ser_quo
NB.  bcc write 2
  y. =. 1 drop y.
end.
bcc
)

