File size: 5,145 Bytes
8b64896
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---

license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- error-correction
- hamming-code
---


# threshold-hamming74decoder

Hamming(7,4) decoder with single-error correction. Takes a 7-bit codeword (possibly corrupted) and outputs the corrected 4 data bits.

## Circuit Overview

```

c1 c2 c3 c4 c5 c6 c7

 β”‚  β”‚  β”‚  β”‚  β”‚  β”‚  β”‚

 └──┴──┴──┴──┴──┴──┴─────────────────────┐

 β”‚  β”‚  β”‚  β”‚  β”‚  β”‚  β”‚                     β”‚

 β–Ό  β–Ό  β–Ό  β–Ό  β–Ό  β–Ό  β–Ό                     β”‚

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  β”‚

β”‚  Syndrome Computer  β”‚                  β”‚

β”‚  s1 = c1βŠ•c3βŠ•c5βŠ•c7  β”‚                  β”‚

β”‚  s2 = c2βŠ•c3βŠ•c6βŠ•c7  β”‚                  β”‚

β”‚  s3 = c4βŠ•c5βŠ•c6βŠ•c7  β”‚                  β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β”‚

         β”‚ s1,s2,s3                      β”‚

         β–Ό                               β”‚

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  β”‚

β”‚  Error Locator      β”‚                  β”‚

β”‚  flip3 = s1∧s2∧¬s3  β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”‚  flip5 = s1∧¬s2∧s3  β”‚   β”‚ c3,c5,c6,c7

β”‚  flip6 = Β¬s1∧s2∧s3  β”‚   β”‚

β”‚  flip7 = s1∧s2∧s3   β”‚   β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚

         β”‚                β”‚

         β–Ό                β–Ό

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”

β”‚  Corrector                  β”‚

β”‚  d1 = c3 βŠ• flip3            β”‚

β”‚  d2 = c5 βŠ• flip5            β”‚

β”‚  d3 = c6 βŠ• flip6            β”‚

β”‚  d4 = c7 βŠ• flip7            β”‚

β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

         β”‚

         β–Ό

    d1 d2 d3 d4

```

## Decoding Algorithm

**Step 1: Compute Syndrome**

The syndrome is a 3-bit value that indicates the error position:

| s3 | s2 | s1 | Decimal | Meaning |
|----|----|----|---------|---------|
| 0 | 0 | 0 | 0 | No error |
| 0 | 0 | 1 | 1 | Error in c1 (parity) |
| 0 | 1 | 0 | 2 | Error in c2 (parity) |
| 0 | 1 | 1 | 3 | Error in c3 (d1) |
| 1 | 0 | 0 | 4 | Error in c4 (parity) |
| 1 | 0 | 1 | 5 | Error in c5 (d2) |
| 1 | 1 | 0 | 6 | Error in c6 (d3) |
| 1 | 1 | 1 | 7 | Error in c7 (d4) |

**Step 2: Locate and Correct**

Only data positions (3, 5, 6, 7) need correction in the output. Parity bit errors (positions 1, 2, 4) don't affect data extraction.

**Step 3: Extract Data**

Data bits are at positions 3, 5, 6, 7 of the codeword, XORed with their flip signals.

## 4-Way XOR Implementation

Each syndrome bit requires a 4-input XOR:

```

XOR(a,b,c,d) = XOR(XOR(a,b), XOR(c,d))



    a   b       c   d

    β”‚   β”‚       β”‚   β”‚

    β””β”€β”¬β”€β”˜       β””β”€β”¬β”€β”˜

      β–Ό           β–Ό

   β”Œβ”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”

   β”‚ XOR β”‚     β”‚ XOR β”‚   Layer 1-2

   β””β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”˜

      β”‚           β”‚

      β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜

            β–Ό

         β”Œβ”€β”€β”€β”€β”€β”

         β”‚ XOR β”‚         Layer 3-4

         β””β”€β”€β”€β”€β”€β”˜

            β”‚

            β–Ό

      XOR(a,b,c,d)

```

## Architecture

| Stage | Component | Neurons | Layers |
|-------|-----------|---------|--------|
| Syndrome | 3 Γ— 4-way XOR | 18 | 4 |
| Error Locator | 4 detectors | 4 | 1 |
| Corrector | 4 Γ— 2-way XOR | 12 | 2 |
| **Total** | | **34** | **6** |

Note: Syndrome computation and final XOR stages run in parallel where possible.

## Error Correction Examples

```

Original:     1011 β†’ encode β†’ 0110011

Corrupted:    0110011 β†’ flip bit 5 β†’ 0110111

Syndrome:     s1=1, s2=0, s3=1 β†’ position 5

Corrected:    d1=1, d2=0, d3=1, d4=1 βœ“



Original:     0000 β†’ encode β†’ 0000000

Corrupted:    0000000 β†’ flip bit 7 β†’ 0000001

Syndrome:     s1=1, s2=1, s3=1 β†’ position 7

Corrected:    d1=0, d2=0, d3=0, d4=0 βœ“

```

## Limitations

- Corrects **single-bit** errors only
- **Detects** double-bit errors (non-zero syndrome, wrong correction)
- Cannot distinguish 2-bit errors from 1-bit errors

For stronger protection, use Hamming(7,4) + overall parity (SECDED).

## Usage

```python

from safetensors.torch import load_file



w = load_file('model.safetensors')



def hamming74_decode(codeword):

    """Decode 7-bit Hamming codeword to 4 data bits with error correction"""

    # See model.py for full implementation

    pass



# Received corrupted codeword (error at position 3)

received = [0, 1, 0, 0, 0, 1, 1]  # Should be [0,1,1,0,0,1,1]

data = hamming74_decode(received)

# Returns [1, 0, 1, 1] (corrected)

```

## Files

```

threshold-hamming74decoder/

β”œβ”€β”€ model.safetensors

β”œβ”€β”€ model.py

β”œβ”€β”€ config.json

└── README.md

```

## License

MIT