// Path: DigitalVoices-ASK-StopCollaborateListen/src/com/jonas/reedsolomon/CRCGen.java
// public class CRCGen implements Settings {
// 
// 	/*
// 	 * Reads in a sequence of bytes and returns its 16 bit Cylcic Redundancy
// 	 * Check (CRC-CCIIT 0xFFFF).
// 	 * 
// 	 * 1 + x + x^5 + x^12 + x^16 is irreducible polynomial.
// 	 * 
// 	 * Copyright 2000Ð2011, Robert Sedgewick and Kevin Wayne
// 	 * source: http://introcs.cs.princeton.edu/java/51data/CRC16CCITT.java.html
// 	 */
// 	public static int crc_16_ccitt(byte[] msg, int len) {
// 		int crc = 0xFFFF; // initial value
// 		int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
// 
// 		boolean bit, c15;
// 		for (int b = 0; b < len; b++) {
// 			for (int i = 0; i < 8; i++) {
// 				bit = ((msg[b] >> (7 - i) & 1) == 1);
// 				c15 = ((crc >> 15 & 1) == 1);
// 				crc <<= 1;
// 				if (c15 ^ bit)
// 					crc ^= polynomial;
// 			}
// 		}
// 		crc &= 0xffff;
// 		
// 		return crc;
// 	}
// 	
// 	/* Computes the CRC-8-CCITT checksum on array of byte data, length len */
// 	public static byte crc_8_ccitt(byte[] msg, int len) {
// 		int crc = (byte) 0xFF; // initial value
// 		int polynomial = 0x07; // (0, 1, 2) : 0x07 / 0xE0 / 0x83
// 
// 		boolean bit, c7;
// 		for (int b = 0; b < len; b++) {
// 			for (int i = 0; i < 8; i++) {
// 				bit = ((msg[b] >> (7 - i) & 1) == 1);
// 				c7 = ((crc >> 7 & 1) == 1);
// 				crc <<= 1;
// 				if (c7 ^ bit)
// 					crc ^= polynomial;
// 			}
// 		}
// 		crc &= 0xffff;
// 
// 		return (byte) crc;
// 	}
// 	
// 	/* Computes the CRC-CCITT checksum on array of byte data, length len */
// 	public static int crc_ccitt(byte[] msg, int len) {
// 		int i, acc = 0;
// 
// 		for (i = 0; i < len; i++) {
// 			acc = crchware((0xFF & (int)msg[i]), 0x1021, acc);
// 		}
// 		
// 		return acc;
// 	}
// 	
// 	/* models crc hardware (minor variation on polynomial division algorithm) */
// 	public static int crchware(int data, int genpoly, int accum) {
// 		int i;
// 		data <<= 8;
// 		for (i = 8; i > 0; i--) {
// 			if (((data ^ accum) & 0x8000) == 1)
// 				accum = (((accum << 1) ^ genpoly) & 0xFFFF);
// 			else
// 				accum = ((accum << 1) & 0xFFFF);
// 			data = ((data << 1) & 0xFFFF);
// 		}
// 		return accum;
// 	}
// }


import java.io.ByteArrayOutputStream;
import com.jonas.reedsolomon.CRCGen;
		    complexDetect(durationInput, Encoder.getFrequency(j));
		/*
		if (j == 0) 
		  System.out.println("\nsignal[" + j + "][" + i + "]=" + signal [j][i]);
		else
		  System.out.println("signal[" + j + "][" + i + "]=" + signal [j][i]);
		*/
	    }
	}
	return signal;
    }

    public static void getKeySignalStrengths(byte[] signal, double[] signalStrengths){
	byte[] partialSignal = ArrayUtils.subarray(signal, 0, kSamplesPerDuration);
	for(int j = 1; j < kBitsPerByte * kBytesPerDuration; j += 2){
	    signalStrengths[j] = complexDetect(partialSignal, Encoder.getFrequency(j));
	}
	
	byte[] partialSignal2 = ArrayUtils.subarray(signal, kSamplesPerDuration, kSamplesPerDuration);
	for(int j = 0; j < kBitsPerByte * kBytesPerDuration; j += 2){
	    signalStrengths[j] = complexDetect(partialSignal2, Encoder.getFrequency(j));
	    //System.out.println(signalStrengths[j]);
	}
    }
    
    /**
     * @param input array of bytes with CRC appended at the end
     * @return true if appended CRC == calculated CRC, false otherwise
     */
    public static boolean crcCheckOk(byte[] input) {
// Path: DigitalVoices-ASK-StopCollaborateListen/src/com/jonas/reedsolomon/CRCGen.java
// public class CRCGen implements Settings {
// 
// 	/*
// 	 * Reads in a sequence of bytes and returns its 16 bit Cylcic Redundancy
// 	 * Check (CRC-CCIIT 0xFFFF).
// 	 * 
// 	 * 1 + x + x^5 + x^12 + x^16 is irreducible polynomial.
// 	 * 
// 	 * Copyright 2000Ð2011, Robert Sedgewick and Kevin Wayne
// 	 * source: http://introcs.cs.princeton.edu/java/51data/CRC16CCITT.java.html
// 	 */
// 	public static int crc_16_ccitt(byte[] msg, int len) {
// 		int crc = 0xFFFF; // initial value
// 		int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
// 
// 		boolean bit, c15;
// 		for (int b = 0; b < len; b++) {
// 			for (int i = 0; i < 8; i++) {
// 				bit = ((msg[b] >> (7 - i) & 1) == 1);
// 				c15 = ((crc >> 15 & 1) == 1);
// 				crc <<= 1;
// 				if (c15 ^ bit)
// 					crc ^= polynomial;
// 			}
// 		}
// 		crc &= 0xffff;
// 		
// 		return crc;
// 	}
// 	
// 	/* Computes the CRC-8-CCITT checksum on array of byte data, length len */
// 	public static byte crc_8_ccitt(byte[] msg, int len) {
// 		int crc = (byte) 0xFF; // initial value
// 		int polynomial = 0x07; // (0, 1, 2) : 0x07 / 0xE0 / 0x83
// 
// 		boolean bit, c7;
// 		for (int b = 0; b < len; b++) {
// 			for (int i = 0; i < 8; i++) {
// 				bit = ((msg[b] >> (7 - i) & 1) == 1);
// 				c7 = ((crc >> 7 & 1) == 1);
// 				crc <<= 1;
// 				if (c7 ^ bit)
// 					crc ^= polynomial;
// 			}
// 		}
// 		crc &= 0xffff;
// 
// 		return (byte) crc;
// 	}
// 	
// 	/* Computes the CRC-CCITT checksum on array of byte data, length len */
// 	public static int crc_ccitt(byte[] msg, int len) {
// 		int i, acc = 0;
// 
// 		for (i = 0; i < len; i++) {
// 			acc = crchware((0xFF & (int)msg[i]), 0x1021, acc);
// 		}
// 		
// 		return acc;
// 	}
// 	
// 	/* models crc hardware (minor variation on polynomial division algorithm) */
// 	public static int crchware(int data, int genpoly, int accum) {
// 		int i;
// 		data <<= 8;
// 		for (i = 8; i > 0; i--) {
// 			if (((data ^ accum) & 0x8000) == 1)
// 				accum = (((accum << 1) ^ genpoly) & 0xFFFF);
// 			else
// 				accum = ((accum << 1) & 0xFFFF);
// 			data = ((data << 1) & 0xFFFF);
// 		}
// 		return accum;
// 	}
// }


// Path: DigitalVoices-ASK-StopCollaborateListen/src/com/jonas/stopcollaboratelisten/Decoder.java
import java.io.ByteArrayOutputStream;
import com.jonas.reedsolomon.CRCGen;

		    complexDetect(durationInput, Encoder.getFrequency(j));
		/*
		if (j == 0) 
		  System.out.println("\nsignal[" + j + "][" + i + "]=" + signal [j][i]);
		else
		  System.out.println("signal[" + j + "][" + i + "]=" + signal [j][i]);
		*/
	    }
	}
	return signal;
    }

    public static void getKeySignalStrengths(byte[] signal, double[] signalStrengths){
	byte[] partialSignal = ArrayUtils.subarray(signal, 0, kSamplesPerDuration);
	for(int j = 1; j < kBitsPerByte * kBytesPerDuration; j += 2){
	    signalStrengths[j] = complexDetect(partialSignal, Encoder.getFrequency(j));
	}
	
	byte[] partialSignal2 = ArrayUtils.subarray(signal, kSamplesPerDuration, kSamplesPerDuration);
	for(int j = 0; j < kBitsPerByte * kBytesPerDuration; j += 2){
	    signalStrengths[j] = complexDetect(partialSignal2, Encoder.getFrequency(j));
	    //System.out.println(signalStrengths[j]);
	}
    }
    
    /**
     * @param input array of bytes with CRC appended at the end
     * @return true if appended CRC == calculated CRC, false otherwise
     */
    public static boolean crcCheckOk(byte[] input) {
    	return input[input.length - 1] == CRCGen.crc_8_ccitt(input, input.length - 1);