|
|
---
|
|
|
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
|
|
|
|