def getPulseWidth(data, verbose=False, header=False):
	"""
	Given the data from a transmission, this function will attempt to retrieve
	the number of samples taken per LED on cycle. This method assumes that the 
	sensor is over-, rather than under-, sampling. It will also deterimine the
	goodness of the data passed in and will suggest a retransmission if the 
	data is of poor quality. Currently, this suggestion is visible to the user
	but does not actually transmit a request for a retransmission. IMPORTANT:
	due to changes in integer division and print statements in python 3, this
	file is NOT backwards compatibile with python 2!
	Arguments:

	data = the data to analyze as a list
	verbose = True for a full accounting of data goodness, defaults
			     to false
	header = True if the data being passed in the known_sample_header 
	Returns: the determined pulse width of the transmission
	"""
	#Get a list of the three most common lengths of zeros,
	#and the two most common lengths of ones
	if verbose: print("Initial data string: " + str(data));
	data = cleanData([int(i) for i in data]);
	data = Utilities.trimZeros(data)
	if verbose: print("Cleaned data string: " + str(data));
	pulseLengthZeros,pulseLengthOnes = getOccuranceFrequencies(data, header);
	if verbose: print("Modes from zeros list: " + str(pulseLengthZeros));
	if verbose: print("Modes from ones list: " + str(pulseLengthOnes));
	
	#Compile a list of the potential pulse widths based on the known relations of morse code
	potentialWidths = [];

	if not header:
		if(len(pulseLengthZeros)>0):potentialWidths.append(pulseLengthZeros[0]);	#Based on symbol spaces
		if(len(pulseLengthZeros)>1):potentialWidths.append(pulseLengthZeros[1]//3);	#Based on character spaces
		#if(len(pulseLengthZeros)>2):potentialWidths.append(pulseLengthZeros[2]//7);	#Based on word spaces OMITTED BECAUSE THIS CAUSES DATA ISSUES
		if(len(pulseLengthOnes)>0): potentialWidths.append(pulseLengthOnes[0]);	        #Based on dot length
		if(len(pulseLengthOnes)>1): potentialWidths.append(pulseLengthOnes[1]//3);	#Based on dash length
	else:
		if(len(pulseLengthZeros)>0): potentialWidths.append(pulseLengthZeros[0]//5);
		if(len(pulseLengthOnes)>0): potentialWidths.append(pulseLengthOnes[0]//5);
		
	pulse_width, data_goodness = findPulseWidth([round(width,0) for width in potentialWidths]);

	if(verbose):
		print("Pulse width determined to be " +str(pulse_width) +" with a  data goodness of " + str(data_goodness) +
		      " (measured from 0 to 1) which is based on " + str(len(pulseLengthOnes)+len(pulseLengthZeros)) + " evaluations out of a maximum 4.");
		print("Note: data goodness is simply a measure of the uniformity of the most common  binary occurances " +
		      "in the code for the purposes of obtaining a pulse width and should not be taken to mean that "+
		      "the message has transmitted without errors.")
		if(data_goodness<=.6): print("Recorded pulses are irregular - consider requesting repeat of message.");

	return pulse_width;
Example #2
0
def trans2Mess(trans_string):
    """
    Translates transmission code into a human readable message.
    Arguments: trans_string - transmission code (1s and 0s) to translate as string.
                              Assumes that the transmission code is already compressed
                              1:1.
    Returns: message as a string
    """

    #Change strings to a list for the next step
    if isinstance(trans_string, str):
        trans_array = [i for i in trans_string];
    else:
        trans_array = trans_string;

    #Trims off leading and trailing zeros
    trans_array = Utilities.trimZeros(trans_array);

    #Translates the message
    return transMess2Word(''.join(trans_array));