Last active
August 29, 2015 14:15
-
-
Save Azoay/536dc329c975e9c3994b to your computer and use it in GitHub Desktop.
HTKのフォーマットにcsvデータを変換するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
とりあえず,使い方を忘れないようにメモしておく | |
このスクリプト(htk_convert.py)は特徴ファイルをHTKフォーマットに変換するスクリプトです. | |
コマンドライン引数で与えられたファイル名のcsvファイルを適当なHTKフォーマットに変換する. | |
その際,HTKフォーマットのヘッダーの値は以下のように生成されるように書かれている. | |
現状として,リニアメルフィルターバング(MELSPEC)の24次元用 | |
nSample => number of samples in file (4-byte integer)はCSVから自動的に算出 | |
sampPeriod => sample period in 100ns units (4-byte integer)はとりあえず10000 (10 msec) | |
sampSize => number of bytes per sample (2-byte integer)は4 byte(一つのデータのサイズ) * nCmops(次元数)で算出 | |
parmKind => a code indicating the sample kind (2-byte integer)は8(MELSPEC)に設定 | |
もし,別のデータ(例えば対数とか)で取りたければparmKindを適宜,変えればOKだと思う. | |
参考 | |
http://www.ee.columbia.edu/ln/LabROSA/doc/HTKBook21/node58.html | |
http://d.hatena.ne.jp/masayukipo/20090621 | |
http://d.hatena.ne.jp/masayukipo/20070930 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import numpy | |
import sys | |
import os | |
from struct import * | |
#debug フラグ | |
debug = False | |
def loadCSV(filename): | |
csvdata = numpy.genfromtxt(filename, delimiter=",",dtype='f') | |
if (debug): | |
for data in csvdata: | |
print(data) | |
return csvdata | |
def getNSamples(data): | |
return len(data) | |
def getNCmps(data): | |
return len(data[0]) | |
def writeFile(filename, nSamples, sampPeriod, sampSize, parmKind, value): | |
f = open(filename, 'wb') | |
b_nSamples = pack('>L', nSamples) # big endian + unsigned long | |
b_sampPeriod = pack('>L', sampPeriod) # big endian + unsigned long | |
b_sampSize = pack('>H', sampSize) # big endian + unsigned short | |
b_parmKind = pack('>H', parmKind) # big endian + unsigned short | |
odata = b_nSamples + b_sampPeriod + b_sampSize + b_parmKind | |
if (debug): | |
print(odata) | |
hoge = unpack('LLHH', odata) | |
print(hoge) | |
for data in value: | |
for d in data: | |
odata += pack(">f", d) | |
if (debug): | |
print(pack(">f", d)) | |
f.write(odata) | |
f.close() | |
if __name__ == '__main__': | |
args = sys.argv | |
argc = len(args) | |
if (argc != 2): | |
print("usage: # python %s filename", args[0]) | |
quit() | |
file = args[1] | |
name, ext = os.path.splitext(file) | |
out = name + ".mfb" | |
print(out, " converting....") | |
data = loadCSV(file) | |
# サンプル数 | |
nSamples = getNSamples(data) | |
# フレームシフト 100nsごと | |
sampPeriod = 10000 | |
# サンプルサイズ 1フレームあたりのバイト数 要は次元数*byteでOK(もしかしたらparmKindとの兼ね合いで24次元にしないといけないかも) | |
nComps = getNCmps(data) | |
sampSize = 4 * nComps | |
# ファイル形式 | |
parmKind = 8 | |
writeFile(out, nSamples, sampPeriod, sampSize, parmKind, data) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import numpy | |
from struct import * | |
debug = False | |
def loadCSV(filename): | |
csvdata = numpy.genfromtxt(filename, delimiter=",",dtype='f') | |
if (debug): | |
for data in csvdata: | |
print(data) | |
return csvdata | |
def getNSamples(data): | |
return len(data) | |
def getNCmps(data): | |
return len(data[0]) | |
def writeFile(filename, nSamples, sampPeriod, sampSize, parmKind, value): | |
f = open(filename, 'wb') | |
b_nSamples = pack('>L', nSamples) # big endian + unsigned long | |
b_sampPeriod = pack('>L', sampPeriod) # big endian + unsigned long | |
b_sampSize = pack('>H', sampSize) # big endian + unsigned short | |
b_parmKind = pack('>H', parmKind) # big endian + unsigned short | |
odata = b_nSamples + b_sampPeriod + b_sampSize + b_parmKind | |
if (debug): | |
print(odata) | |
hoge = unpack('LLHH', odata) | |
print(hoge) | |
for data in value: | |
for d in data: | |
odata += pack(">f", d) | |
if (debug): | |
print(pack(">f", d)) | |
f.write(odata) | |
f.close() | |
if __name__ == '__main__': | |
file ="breath-Mel.csv" | |
out = "breath-Mel.mfb" | |
data = loadCSV(file) | |
# サンプル数 | |
nSamples = getNSamples(data) | |
# フレームシフト 100nsごと | |
sampPeriod = 10000 | |
# サンプルサイズ 1フレームあたりのバイト数 要は次元数*byteでOK(もしかしたらparmKindとの兼ね合いで24次元にしないといけないかも) | |
nComps = getNCmps(data) | |
sampSize = 4 * nComps | |
# ファイル形式 | |
parmKind = 8 | |
writeFile(out, nSamples, sampPeriod, sampSize, parmKind, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment