File size: 4,504 Bytes
29db051 |
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 170 171 172 173 174 175 176 177 178 179 180 181 |
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- error-correction
- hamming-code
---
# threshold-hamming74encoder
Hamming(7,4) encoder. Transforms 4 data bits into a 7-bit codeword with single-error correction capability.
## Circuit Overview
```
d1 d2 d3 d4
β β β β
βββββΌββββΌββββ€
β β β β
β β β ββββββββββββββββββββββββββββββΊ c7 = d4
β β βββββββββββββββββββββββββΊ c6 = d3
β ββββββββββββββββββββΊ c5 = d2
βββββββββββββββΊ c3 = d1
β β β β
βΌ βΌ β βΌ
βββββββββββββββββ
β d1 XOR d2 XOR ββββββββΊ c1 = p1
β d4 β
βββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββ
β d1 XOR d3 XOR ββββββββΊ c2 = p2
β d4 β
βββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββ
β d2 XOR d3 XOR ββββΊ c4 = p3
β d4 β
βββββββββββββββββ
```
## The Hamming(7,4) Code
Richard Hamming invented this code in 1950. It encodes 4 data bits into 7 bits such that any single-bit error can be detected and corrected.
**Codeword structure:**
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|----------|---|---|---|---|---|---|---|
| Bit | p1 | p2 | d1 | p3 | d2 | d3 | d4 |
| Type | parity | parity | data | parity | data | data | data |
**Parity equations:**
- p1 = d1 β d2 β d4 (covers positions 1,3,5,7)
- p2 = d1 β d3 β d4 (covers positions 2,3,6,7)
- p3 = d2 β d3 β d4 (covers positions 4,5,6,7)
## 3-Way XOR Implementation
Each parity bit requires a 3-input XOR. In threshold logic:
```
XOR(a,b,c) = XOR(XOR(a,b), c)
a b
β β
βββ¬ββ
βΌ
βββββββββ
β XOR β (2 layers)
βββββββββ
β
β c
βββ¬ββ
βΌ
βββββββββ
β XOR β (2 more layers)
βββββββββ
β
βΌ
XOR(a,b,c)
```
Total depth: 4 layers per parity. All three parities compute in parallel.
## Code Properties
| Property | Value |
|----------|-------|
| Data bits (k) | 4 |
| Codeword bits (n) | 7 |
| Parity bits (n-k) | 3 |
| Minimum distance | 3 |
| Error correction | 1 bit |
| Error detection | 2 bits |
## Example Encoding
```
Data: 1011
p1 = 1 β 0 β 1 = 0
p2 = 1 β 1 β 1 = 1
p3 = 0 β 1 β 1 = 0
Codeword: 0110011
ββ β
p1p2p3
```
## Architecture
| Component | Neurons | Parameters |
|-----------|---------|------------|
| p1 (3-way XOR) | 6 | 22 |
| p2 (3-way XOR) | 6 | 22 |
| p3 (3-way XOR) | 6 | 22 |
| d1-d4 pass-through | 4 | 20 |
| **Total** | **22** | **86** |
**Layers: 4** (two cascaded XOR stages)
## All 16 Codewords
| Data | Codeword | HW |
|------|----------|-----|
| 0000 | 0000000 | 0 |
| 1000 | 1110000 | 3 |
| 0100 | 1001100 | 3 |
| 1100 | 0111100 | 4 |
| 0010 | 0101010 | 3 |
| 1010 | 1011010 | 4 |
| 0110 | 1100110 | 4 |
| 1110 | 0010110 | 3 |
| 0001 | 1101001 | 4 |
| 1001 | 0011001 | 3 |
| 0101 | 0100101 | 3 |
| 1101 | 1010101 | 4 |
| 0011 | 1000011 | 3 |
| 1011 | 0110011 | 4 |
| 0111 | 0001111 | 4 |
| 1111 | 1111111 | 7 |
Note: Minimum Hamming distance between any two codewords is 3.
## Usage
```python
from safetensors.torch import load_file
w = load_file('model.safetensors')
def hamming74_encode(d1, d2, d3, d4):
"""Encode 4 data bits to 7-bit Hamming codeword"""
# See model.py for full implementation
pass
# Encode data word 1011
codeword = hamming74_encode(1, 0, 1, 1)
# Returns [0, 1, 1, 0, 0, 1, 1]
```
## Files
```
threshold-hamming74encoder/
βββ model.safetensors
βββ model.py
βββ config.json
βββ README.md
```
## License
MIT
|