By Peter Smit

2011-03-28 07:57:03 8 Comments

I have the following code for reading HTK feature files. The code below is working completely correct (verified it with unit tests and the output of the original HTK toolkit).

from HTK_model import FLOAT_TYPE
from numpy import array
from struct import unpack

def feature_reader(file_name):
    with open(file_name, 'rb') as in_f:
        #There are four standard headers. Sample period is not used
        num_samples = unpack('>i',[0]
        sample_period = unpack('>i',[0]
        sample_size = unpack('>h',[0]
        param_kind = unpack('>h',[0]

        compressed = bool(param_kind & 02000)

        #If compression is used, two matrices are defined. In that case the values are shorts, and the real values are:
        # (x+B)/A
        A = B = 0
        if compressed:
            A = array([unpack('>f',[0] for _ in xrange(sample_size/2)], dtype=FLOAT_TYPE)
            B = array([unpack('>f',[0] for _ in xrange(sample_size/2)], dtype=FLOAT_TYPE)
            #The first 4 samples were the matrices
            num_samples -= 4

        for _ in xrange(0,num_samples):
            if compressed:
                yield ((array( unpack('>' + ('h' * (sample_size//2)), ,dtype=FLOAT_TYPE) + B) / A)
                yield (array( unpack('>' + ('f' * (sample_size//4)),, dtype=FLOAT_TYPE))

How can I speed up this code, are there things I should improve in the code?


@blaze 2011-03-28 12:27:11

    data =
    num_samples, sample_period, sample_size, param_kind = unpack('>iihh', data)
    A = B = 0
    if compressed:
        A = array('f')
        A.fromfile(in_f, sample_size/2)
        B = array('f')
        B.fromfile(in_f, sample_size/2)
        #The first 4 samples were the matrices
        num_samples -= 4

And so on

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Converting string of hex values to numpy array of specific shape

1 Answered Questions

[SOLVED] Generating Latin hypercube samples with numpy

1 Answered Questions

[SOLVED] Fast loop to create an array of values

3 Answered Questions

[SOLVED] File handling in C

0 Answered Questions

Snell's law using Zoeppritz equation by matrices

1 Answered Questions

[SOLVED] Custom compression tool

5 Answered Questions

[SOLVED] Create matrices of a certain format

1 Answered Questions

[SOLVED] A big "Game of Life"

1 Answered Questions

[SOLVED] Creating a flood from a seed point in an array

  • 2015-06-03 10:35:15
  • Martin S.
  • 257 View
  • 6 Score
  • 1 Answer
  • Tags:   python numpy

2 Answered Questions

Sponsored Content