3

Here's matlab's answer:

x=[1,0,1,1]

x_encode=encode(x,7,4)

>>x_encode [1,0,0,1,0,1,1]

I calculated the hamming code from definition of hamming code, here's my calculation process:

2^r≥k+r+1
Here,k=4
2^r≥5+r
r=3
n=k+r=7
encode:[r1,r2,1,r3,0,1,1]
r1->[r1,1,0,1]、r2->[r2,1,1,1]、r3->[r3,0,1,1]

If I use odd parity bit, so it would be like:

r1=1,r2=0,r3=1
>>x_encode [1,0,1,1,0,1,1]

But else if I use even parity bit, it would be like:

r1=0,r2=1,r3=0
>>x_encode [0,1,1,0,0,1,1]

none of above equals the result that matlab calculated. I wonder why, I will appericate it if someone can answer my question. Thanks!

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
Jay Kwok
  • 31
  • 2
  • Here is a detailed link of encoding and decoding with Matlab codes : https://medium.com/@4ravind/hamming-7-4-efficient-implementation-matlab-octave-1605a73429f7 – m2016b Dec 15 '20 at 13:12

1 Answers1

3

You can open the encode function in Matlab. Accordingly, the function computes the following steps:

m = n-k;
h = hammgen(m);
gen = gen2par(h);
code = rem(x * gen, 2);

Try to check step-by-step what those commands do and where they differ with respect to the classical Hamming code.

Alessandro
  • 764
  • 3
  • 8
  • 22
  • 2
    I didn't know 'open' function, thanks! you really open my eyes. But I still wonder why matlab uses this encode method instead of definition. I doubt if my knowledge is wrong. – Jay Kwok Dec 15 '20 at 13:02
  • 1
    It seems that Matlab uses a different parity-check matrix. Give a look at `hammgen` function. It returns a matrix in which the parity bits are computed by taking into account (x1,x3,x4), (x1,x2,x3), (x2,x3,x4), while the parity-check matrix as reported in https://en.wikipedia.org/wiki/Hamming(7,4) is designed by considering other combinations. – Alessandro Dec 15 '20 at 14:08