private int[] EncodeMessage(int[,] parityMatrix, int[] message, int gap, int oldGap) { int[,] p1 = null; int[,] messageArray = new int[1, message.Length]; for (int i = 0; i < message.Length; i++) { messageArray[0, i] = message[i]; } if (gap != 0) { int startRow = parityMatrix.GetLength(0) - gap; int[,] DMatrix = new int[gap, gap]; int[,] CMatrix = new int[gap, parityMatrix.GetLength(1) - DMatrix.GetLength(1) - parityMatrix.GetLength(0) + gap]; for (int i = startRow; i < parityMatrix.GetLength(0); i++) { for (int j = 0; j < parityMatrix.GetLength(1) - parityMatrix.GetLength(0) + gap; j++) { if (j < CMatrix.GetLength(1)) { CMatrix[i % startRow, j] = parityMatrix[i, j]; } else { DMatrix[i % startRow, j % CMatrix.GetLength(1)] = parityMatrix[i, j]; } } } int[,] DCMatrix = MatrixOperation.LogicMultiplicateMatrixes(MatrixOperation.LogicGetInverseMatrix(DMatrix), CMatrix); p1 = MatrixOperation.Transponse(MatrixOperation.LogicMultiplicateMatrixes(DCMatrix, MatrixOperation.Transponse(messageArray))); } else { p1 = new int[1, oldGap]; } int[,] BMatrix = new int[parityMatrix.GetLength(0) - gap, oldGap]; int[,] TMatrix = new int[parityMatrix.GetLength(0) - gap, parityMatrix.GetLength(0) - gap]; int[,] AMatrix = new int[BMatrix.GetLength(0), parityMatrix.GetLength(1) - BMatrix.GetLength(1) - TMatrix.GetLength(1)]; for (int i = 0; i < AMatrix.GetLength(0); i++) { for (int j = 0; j < parityMatrix.GetLength(1); j++) { if (j < AMatrix.GetLength(1)) { AMatrix[i, j] = parityMatrix[i, j]; } else if (j < AMatrix.GetLength(1) + BMatrix.GetLength(1)) { BMatrix[i, j % AMatrix.GetLength(1)] = parityMatrix[i, j]; } else { TMatrix[i, j % (AMatrix.GetLength(1) + BMatrix.GetLength(1))] = parityMatrix[i, j]; } } } int[,] AuMatrix = MatrixOperation.LogicMultiplicateMatrixes(AMatrix, MatrixOperation.Transponse(messageArray)); int[,] addedMatrixes = null; int[,] Bp1Matrix = MatrixOperation.LogicMultiplicateMatrixes(BMatrix, MatrixOperation.Transponse(p1)); addedMatrixes = MatrixOperation.LogicAddUpMatrixes(AuMatrix, Bp1Matrix); int[,] p2 = MatrixOperation.Transponse(MatrixOperation.LogicMultiplicateMatrixes(MatrixOperation.LogicGetInverseMatrix(TMatrix), addedMatrixes)); int[] codeword = new int[message.Length + p1.GetLength(1) + p2.GetLength(1)]; for (int i = 0; i < codeword.Length; i++) { if (i < message.Length) { codeword[i] = message[i]; } else if (i < message.Length + p1.GetLength(1)) { codeword[i] = p1[0, i % message.Length]; } else { codeword[i] = p2[0, i % (message.Length + p1.GetLength(1))]; } } return(codeword); }
private static int[,] GetGaussMatrix(int[,] parityMatrix, int gap) { int[,] resultMatrix = new int[parityMatrix.GetLength(0), parityMatrix.GetLength(0)]; int[,] EMatrix = new int[gap, parityMatrix.GetLength(0) - gap]; int[,] TMatrix = new int[parityMatrix.GetLength(0) - gap, parityMatrix.GetLength(0) - gap]; int matrixRow = 0; for (int row = 0; row < parityMatrix.GetLength(0); row++) { int matrixColumn = 0; for (int column = parityMatrix.GetLength(1) - TMatrix.GetLength(1); column < parityMatrix.GetLength(1); column++) { if (row < parityMatrix.GetLength(0) - gap) { TMatrix[row, matrixColumn] = parityMatrix[row, column]; } else { EMatrix[matrixRow, matrixColumn] = parityMatrix[row, column]; } matrixColumn++; } if (row >= parityMatrix.GetLength(0) - gap) { matrixRow++; } } int[,] ETMatrix = MatrixOperation.LogicMultiplicateMatrixes(EMatrix, MatrixOperation.LogicGetInverseMatrix(TMatrix)); matrixRow = 0; for (int row = 0; row < resultMatrix.GetLength(0); row++) { for (int column = 0; column < resultMatrix.GetLength(1); column++) { if (row < (resultMatrix.GetLength(0) - gap)) { if (row == column) { resultMatrix[row, column] = 1; } } else { if (column < resultMatrix.GetLength(0) - gap) { resultMatrix[row, column] = ETMatrix[matrixRow, column]; } else { if (row == column) { resultMatrix[row, column] = 1; } } } } if (row >= (resultMatrix.GetLength(0) - gap)) { matrixRow++; } } return(resultMatrix); }