annotate swig/aimc_data_io.py @ 226:eaeba1fd41e6

- More tree support
author tomwalters
date Wed, 29 Sep 2010 00:24:03 +0000
parents 9a98efa01965
children b8f16e8acf7b
rev   line source
hamel@150 1 #!/usr/bin/env python
hamel@150 2 #
hamel@150 3 # audio_example.py
hamel@150 4 #
hamel@150 5 #
hamel@150 6 # Created by Philippe Hamel (hamel.phil@gmail.com) on 11-01-05.
hamel@150 7 #
hamel@150 8 # methods to read and write aimc data files
hamel@150 9
hamel@150 10 from struct import *
hamel@150 11 import numpy as N
hamel@150 12
hamel@150 13 #int_size = calcsize('i')
hamel@150 14 #float_size = calcsize('d')
hamel@150 15
hamel@150 16 def readbin(type,file) :
hamel@150 17 """
hamel@150 18 used to read binary data from a file
hamel@150 19 """
hamel@150 20 return unpack(type,file.read(calcsize(type)))
hamel@150 21
hamel@150 22 def read_aimc_data(filename):
hamel@150 23 file = open(filename,'rb')
hamel@150 24
hamel@150 25 nFrames = readbin('i',file)[0]
hamel@150 26 period = readbin( 'f', file)[0] # Frame period in ms
hamel@150 27 nChannels = readbin( 'i', file)[0] # vertical axis of an AI
hamel@150 28 nSamples = readbin( 'i', file)[0] # horizontal axis of an AI
hamel@150 29 sample_rate = readbin('f', file)[0] # sample rate of each channel in Hz
hamel@150 30
hamel@150 31 print 'nFrames, period, nChannels, nSamples, sample_rate'
hamel@150 32 print nFrames, period, nChannels, nSamples, sample_rate
hamel@150 33
hamel@150 34 nData = nFrames * nChannels * nSamples
hamel@150 35 print nData
hamel@150 36 vec_data = readbin('%if'%nData,file)
hamel@150 37
hamel@150 38 file.close()
hamel@150 39 data = N.reshape(vec_data,(nFrames, nChannels, nSamples))
hamel@150 40 return data, nFrames, period, nChannels, nSamples, sample_rate
hamel@150 41
hamel@152 42 def write_aimc_data(filename, data, sample_rate, period = 0.0):
hamel@152 43
hamel@152 44 nFrames, nChannels, nSamples = data.shape
hamel@152 45
hamel@152 46 file = open(filename,'wb')
hamel@152 47
hamel@152 48 file.write(pack('i',nFrames))
hamel@152 49 file.write(pack('f',period)) #Not correctly implemented yet
hamel@152 50 file.write(pack('i',nChannels))
hamel@152 51 file.write(pack('i',nSamples))
hamel@152 52 file.write(pack('f',sample_rate))
hamel@152 53
hamel@152 54 vec_data = data.flatten()
hamel@152 55 for elem in vec_data:
hamel@152 56 file.write(pack('f',elem))
hamel@152 57
hamel@153 58 file.close()
hamel@152 59