Guest User

Untitled

a guest
Mar 6th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import sys
  2. import os
  3.  
  4. NUMBER_OF_CHANNELS = 16
  5. SAMPLE_LENGTH = NUMBER_OF_CHANNELS // 8
  6.  
  7. def getBit(byteArray, bitNumber):
  8.     "This returns bit with bitNumber position from byteArray"
  9.     byte = byteArray[bitNumber // 8]
  10.     return (byte >> (bitNumber % 8)) & 1
  11.  
  12. try:
  13.     if len(sys.argv) < 2:
  14.         raise ValueError('Too few input arguments.')
  15.  
  16.     sampleCounter = 0
  17.     inputFileName = sys.argv[1]
  18.     outputDirName = os.path.splitext(inputFileName)[0]
  19.    
  20.     inputFileSize = os.path.getsize(inputFileName)
  21.     if inputFileSize % SAMPLE_LENGTH != 0:
  22.         raise ValueError('Input file has wrong length.')
  23.    
  24.     try:
  25.         inputFile = open(inputFileName, 'rb')
  26.         outputFiles = []
  27.        
  28.         if not os.path.exists(outputDirName):
  29.             os.mkdir(outputDirName)
  30.        
  31.         for channel in range(0, NUMBER_OF_CHANNELS):
  32.             outputFiles.append(open(outputDirName + "/" + str(channel) + ".dat", 'w'))
  33.            
  34.         while True:
  35.             multiChannelSample = inputFile.read(SAMPLE_LENGTH)
  36.             if multiChannelSample == b'':
  37.                 break
  38.            
  39.             for channel in range(0, NUMBER_OF_CHANNELS):
  40.                 singleChannelSample = '+1' if (getBit(multiChannelSample, channel)) else '-1'
  41.                 outputFiles[channel].write(singleChannelSample + '\n')
  42.                
  43.             sampleCounter += 1
  44.            
  45.     finally:
  46.         inputFile.close()
  47.         for channel in range(0, NUMBER_OF_CHANNELS):
  48.             outputFiles[channel].close()
  49.            
  50.     print(str(sampleCounter), " samples were processed.\n")
  51.  
  52. except IOError as error:
  53.     print(error, "\n")
  54.    
  55. except ValueError as error:
  56.     print(error, "\n")
  57.    
  58. input("Press ENTER to quit.")
Advertisement
Add Comment
Please, Sign In to add comment