-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreateTestFile.py
More file actions
130 lines (105 loc) · 4 KB
/
Copy pathcreateTestFile.py
File metadata and controls
130 lines (105 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# createTestFile.py
#
# Reference implementation for SOFA file creation with pysofaconventions
#
# (C) Andrés Pérez-López - Eurecat / UPF
# 24/08/2018
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from netCDF4 import Dataset
import time
import numpy as np
import os
#----------Create it----------#
filePath = "/Volumes/Dinge/SOFA/testpysofaconventions.sofa"
# Need to delete it first if file already exists
if os.path.exists(filePath):
os.remove(filePath)
rootgrp = Dataset(filePath, 'w', format='NETCDF4')
#----------Required Attributes----------#
rootgrp.Conventions = 'SOFA'
rootgrp.Version = '1.0'
rootgrp.SOFAConventions = 'SimpleFreeFieldHRIR'
rootgrp.SOFAConventionsVersion = '0.1'
rootgrp.APIName = 'pysofaconventions'
rootgrp.APIVersion = '0.1'
rootgrp.APIVersion = '0.1'
rootgrp.AuthorContact = 'andres.perez@eurecat.org'
rootgrp.Organization = 'Eurecat - UPF'
rootgrp.License = 'WTFPL - Do What the Fuck You Want to Public License'
rootgrp.DataType = 'FIR'
rootgrp.RoomType = 'reverberant'
rootgrp.DateCreated = time.ctime(time.time())
rootgrp.DateModified = time.ctime(time.time())
rootgrp.Title = 'testpysofaconventions'
rootgrp.RoomType = 'free field'
rootgrp.DatabaseName = 'CoolDatabase'
rootgrp.ListenerShortName = '001'
#----------Required Dimensions----------#
m = 3
n = 48000
r = 2
e = 1
i = 1
c = 3
rootgrp.createDimension('M', m)
rootgrp.createDimension('N', n)
rootgrp.createDimension('E', e)
rootgrp.createDimension('R', r)
rootgrp.createDimension('I', i)
rootgrp.createDimension('C', c)
#----------Required Variables----------#
listenerPositionVar = rootgrp.createVariable('ListenerPosition', 'f8', ('I','C'))
listenerPositionVar.Units = 'metre'
listenerPositionVar.Type = 'cartesian'
listenerPositionVar[:] = np.zeros(c)
listenerUpVar = rootgrp.createVariable('ListenerUp', 'f8', ('I','C'))
listenerUpVar.Units = 'metre'
listenerUpVar.Type = 'cartesian'
listenerUpVar[:] = np.asarray([0,0,1])
# Listener looking to the left (+Y axis)
listenerViewVar = rootgrp.createVariable('ListenerView', 'f8', ('I','C'))
listenerViewVar.Units = 'metre'
listenerViewVar.Type = 'cartesian'
listenerViewVar[:] = np.asarray([0,1,0])
emitterPositionVar = rootgrp.createVariable('EmitterPosition', 'f8', ('E','C','I'))
emitterPositionVar.Units = 'metre'
emitterPositionVar.Type = 'spherical'
# Equidistributed speakers in circle
emitterPositionVar[:] = np.zeros((e,c,i))
# for idx in range(e):
# azi = idx*360/float(e)
# ele = 0.
# dis = 1.
# emitterPositionVar[idx,:,:] = np.asarray([azi,ele,dis])
sourcePositionVar = rootgrp.createVariable('SourcePosition', 'f8', ('I','C'))
sourcePositionVar.Units = 'metre'
sourcePositionVar.Type = 'cartesian'
sourcePositionVar[:] = np.zeros(c)
sourceUpVar = rootgrp.createVariable('SourceUp', 'f8', ('I','C'))
sourceUpVar.Units = 'metre'
sourceUpVar.Type = 'cartesian'
sourceUpVar[:] = np.asarray([0,0,1])
sourceViewVar = rootgrp.createVariable('SourceView', 'f8', ('I','C'))
sourceViewVar.Units = 'metre'
sourceViewVar.Type = 'cartesian'
sourceViewVar[:] = np.asarray([1,0,0])
receiverPositionVar = rootgrp.createVariable('ReceiverPosition', 'f8', ('R','C','I'))
receiverPositionVar.Units = 'metre'
receiverPositionVar.Type = 'cartesian'
receiverPositionVar[:] = np.zeros((r,c,i))
samplingRateVar = rootgrp.createVariable('Data.SamplingRate', 'f8', ('I'))
samplingRateVar.Units = 'hertz'
samplingRateVar[:] = 48000
delayVar = rootgrp.createVariable('Data.Delay', 'f8', ('I','R'))
delay = np.zeros((i,r))
delayVar[:,:] = delay
dataIRVar = rootgrp.createVariable('Data.IR', 'f8', ('M','R','N'))
dataIRVar.ChannelOrdering = 'acn'
dataIRVar.Normalization = 'sn3d'
dataIRVar[:] = np.random.rand(m,r,n)
#----------Close it----------#
rootgrp.close()