import sys import os NUMBER_OF_CHANNELS = 16 SAMPLE_LENGTH = NUMBER_OF_CHANNELS // 8 def getBit(byteArray, bitNumber): "This returns bit with bitNumber position from byteArray" byte = byteArray[bitNumber // 8] return (byte >> (bitNumber % 8)) & 1 try: if len(sys.argv) < 2: raise ValueError('Too few input arguments.') sampleCounter = 0 inputFileName = sys.argv[1] outputDirName = os.path.splitext(inputFileName)[0] inputFileSize = os.path.getsize(inputFileName) if inputFileSize % SAMPLE_LENGTH != 0: raise ValueError('Input file has wrong length.') try: inputFile = open(inputFileName, 'rb') outputFiles = [] if not os.path.exists(outputDirName): os.mkdir(outputDirName) for channel in range(0, NUMBER_OF_CHANNELS): outputFiles.append(open(outputDirName + "/" + str(channel) + ".dat", 'w')) while True: multiChannelSample = inputFile.read(SAMPLE_LENGTH) if multiChannelSample == b'': break for channel in range(0, NUMBER_OF_CHANNELS): singleChannelSample = '+1' if (getBit(multiChannelSample, channel)) else '-1' outputFiles[channel].write(singleChannelSample + '\n') sampleCounter += 1 finally: inputFile.close() for channel in range(0, NUMBER_OF_CHANNELS): outputFiles[channel].close() print(str(sampleCounter), " samples were processed.\n") except IOError as error: print(error, "\n") except ValueError as error: print(error, "\n") input("Press ENTER to quit.")