annotate trunk/swig/aimc_data_io.py @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents 63aaa0a08e74
children
rev   line source
hamel@448 1 #!/usr/bin/env python
hamel@448 2 #
hamel@448 3 # audio_example.py
hamel@448 4 #
hamel@448 5 #
hamel@448 6 # Created by Philippe Hamel (hamel.phil@gmail.com) on 11-01-05.
hamel@448 7 #
hamel@448 8 # methods to read and write aimc data files
hamel@448 9
hamel@448 10 from struct import *
hamel@448 11 import numpy as N
hamel@448 12
hamel@448 13 #int_size = calcsize('i')
hamel@448 14 #float_size = calcsize('d')
hamel@448 15
hamel@448 16 def readbin(type,file) :
hamel@448 17 """
hamel@448 18 used to read binary data from a file
hamel@448 19 """
hamel@448 20 return unpack(type,file.read(calcsize(type)))
hamel@448 21
hamel@448 22 def read_aimc_data(filename):
hamel@448 23 file = open(filename,'rb')
hamel@448 24
hamel@448 25 nFrames = readbin('i',file)[0]
hamel@448 26 period = readbin( 'f', file)[0] # Frame period in ms
hamel@448 27 nChannels = readbin( 'i', file)[0] # vertical axis of an AI
hamel@448 28 nSamples = readbin( 'i', file)[0] # horizontal axis of an AI
hamel@448 29 sample_rate = readbin('f', file)[0] # sample rate of each channel in Hz
hamel@448 30
hamel@448 31 print 'nFrames, period, nChannels, nSamples, sample_rate'
hamel@448 32 print nFrames, period, nChannels, nSamples, sample_rate
hamel@448 33
hamel@448 34 nData = nFrames * nChannels * nSamples
hamel@448 35 print nData
hamel@448 36 vec_data = readbin('%if'%nData,file)
hamel@448 37
hamel@448 38 file.close()
hamel@448 39 data = N.reshape(vec_data,(nFrames, nChannels, nSamples))
hamel@448 40 return data, nFrames, period, nChannels, nSamples, sample_rate
hamel@448 41
hamel@450 42 def write_aimc_data(filename, data, sample_rate, period = 0.0):
hamel@450 43
hamel@450 44 nFrames, nChannels, nSamples = data.shape
hamel@450 45
hamel@450 46 file = open(filename,'wb')
hamel@450 47
hamel@450 48 file.write(pack('i',nFrames))
hamel@450 49 file.write(pack('f',period)) #Not correctly implemented yet
hamel@450 50 file.write(pack('i',nChannels))
hamel@450 51 file.write(pack('i',nSamples))
hamel@450 52 file.write(pack('f',sample_rate))
hamel@450 53
hamel@450 54 vec_data = data.flatten()
hamel@450 55 for elem in vec_data:
hamel@450 56 file.write(pack('f',elem))
hamel@450 57
hamel@451 58 file.close()
hamel@450 59