-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhammingCode.R
More file actions
89 lines (70 loc) · 1.54 KB
/
hammingCode.R
File metadata and controls
89 lines (70 loc) · 1.54 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
num="10011010"
sp=as.numeric(strsplit(num, split="")[[1]])
code=c()
count=1
ind=1
check=2
while(ind<=length(sp)){
if(count==1 | count==2){
code=c(code, NA)
}else if(check*2==count){
check=check*2
code=c(code,NA)
}else{
code=c(code, sp[ind])
ind=ind+1
}
count=count+1
}
slots=which(is.na(code))
count=1
while(count<=length(slots)){
s=slots[count]
val=checker(code, s)
code[s]=val
count=count+1
}
code
########################checking correctness
#correct: 011100101010
tocheck="011100101110"
stc=as.numeric(strsplit(tocheck, split="")[[1]])
#check parity bits
count=1
incorrect=c()
while(count<=length(stc)){
curval=stc[count]
par=checker(stc, count)
if(curval==1){ #i.e. if curval is incorrectly influencing par
if(par==1){par=0}else{par=1}
}
if(par!=curval){
incorrect=c(incorrect, count)
}
count=count*2
}
wrongind=sum(incorrect)
if(stc[wrongind]==1){stc[wrongind]=0}else{stc[wrongind]=1}
stc
########################################functions
checker=function(vals, checknum){
l=length(vals)
if(checknum==1){
checkinds=seq(from=1, to=l, by=2)
}else{
checkinds=c()
count=0
while(checknum+count<=l & count<checknum){
theseinds=seq(from=checknum+count, to=l, by=2*checknum)
checkinds=c(checkinds,theseinds)
count=count+1
}
}
checkinds=unique(sort(checkinds))
p=sum(vals[checkinds], na.rm = T)
if(p %% 2 == 0){
return(0)
}else{
return(1)
}
}