Package pl.mkaczara.bch

Source Code of pl.mkaczara.bch.BCH

package pl.mkaczara.bch;

import pl.mkaczara.bch.math.BinPolynomial;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import pl.mkaczara.bch.code.BCHCode;
import pl.mkaczara.bch.decoder.BCHDecoder;
import pl.mkaczara.bch.encoder.BCHEncoder;
import pl.mkaczara.bch.exception.UncorrectableErrorsException;

/**
* Glowna klasa (test kodera i dekodera)
*
* @author Michał
*/
public class BCH {
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        if(0 == args.length){
            System.out.println("No text for coding provided, exiting.");
            System.exit(1);
        }
        byte[] bytes = args[0].getBytes("UTF-8");
       
        System.out.println("To encode:\t" + args[0]);
       
        BCHEncoder encoder = new BCHEncoder(BCHCode.BCH_255_131);
       
        long start = System.currentTimeMillis();
        byte[] encoded = encoder.encode(bytes);
        long stop = System.currentTimeMillis();
       
        System.out.println("Encoding:\t" + (stop - start) + " [ms]");
       
        //Transmisja z 18 bledamiw
        //Wektor bledu w postaci 0-1
        //Testy automatyczne właściwości kodu
        //Podobne kody (<> rozmiar slowa)
        //Status tego co się udalo zrobić w dokumencie
//        TransmissionSimulator ts = new TransmissionSimulator(18, TransmissionSimulator.ErrorScope.CORRECTION_PART_SIZE, BCHCode.BCH_255_131);
//        encoded = ts.transmitData(encoded);
               
        BCHDecoder decoder = new BCHDecoder(BCHCode.BCH_255_131, true);
       
        start = System.currentTimeMillis();
        byte[] decoded;
        try {
            decoded = decoder.decode(encoded);
        } catch (UncorrectableErrorsException ex) {
            System.err.println("Uncorrectable errors found in received code words!");
            return;
        }
        stop = System.currentTimeMillis();
       
        System.out.println("Decoding:\t" + (stop - start) + " [ms]");
       
        System.out.println("Decoded:\t" + new String(decoded, Charset.forName("UTF-8")));
    }

    /**
     * Konwertuje tablice bajtow na wielomiany informacji
     *
     * @param byteArray tablica bajtow
     * @param infoPartDegree maksymalny stopien wielomianu informacji
     * @return lista wielomianow informacji
     */
    public static ArrayList<BinPolynomial> bytesToPolynomials(byte[] byteArray, int infoPartDegree){
        BigInteger mask = BigInteger.ZERO;
        for(int i = 0; i <= infoPartDegree; i++){
            mask = mask.setBit(i);
        }
       
        BigInteger testi = new BigInteger(byteArray);
        ArrayList<BinPolynomial> ret = new ArrayList<>();
       
        while(testi.bitLength() > 0){
            ret.add(new BinPolynomial(testi.and(mask), infoPartDegree));
            testi = testi.shiftRight(infoPartDegree + 1);
        }
        return ret;
    }
   
    /**
     * Konwertuje wielomiany slow kodowych na tablice bajtow
     * @param polynomialList lista wielomianow(slow kodowych)
     * @param infoPartDegree maksymalny stopien wielomianu informacji
     * @return tablica bajtow
     */
    public static byte[] polynomialsToBytes(List<BinPolynomial> polynomialList, int infoPartDegree){       
        BigInteger result = polynomialList.get(polynomialList.size() - 1).getPolyVector();
        for (int i = polynomialList.size() - 2; i >= 0; i--) {
            result = result.shiftLeft(infoPartDegree+1);
            result = result.or(polynomialList.get(i).getPolyVector());
        }
//        byte[] tmp =  result.toByteArray();
//        if(tmp[0] == 0){
//            tmp = Arrays.copyOfRange(tmp, 1, tmp.length);
//        }
//        return tmp;
        return result.toByteArray();
    }
   
    /**
     * Konwertuje tablice bajtow na lancuch tekstowy
     *
     * @param byteArray tablica bajtow
     * @return lancuch tekstowy zawierajacy bity w postaci 0 i 1
     */
    private static String bytesToString(byte[] byteArray){
        StringBuilder binary = new StringBuilder();
        for(byte b : byteArray){
            int val = b;
            for (int i = 0; i < 8; i++) {
                binary.append((val & 128) == 0 ? 0 : 1);
                val <<= 1;
            }
        }
        return binary.toString();
    }
}
TOP

Related Classes of pl.mkaczara.bch.BCH

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.