-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResonatorNetwork.sc
225 lines (204 loc) · 7.55 KB
/
ResonatorNetwork.sc
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
ResonatorNetwork {
var <resonators,<connPointMatrix,<massMatrix,<excPointMatrix,<readoutPointMatrix,<modalData;
classvar pythonScriptPath,<>pythonPath;
*initClass {
Class.initClassTree(String);
pythonScriptPath = this.class.filenameSymbol.asString.dirname;
pythonPath = "/System/Library/Frameworks/Python.framework/Versions/Current/bin/python"
}
*new { arg resonators,connPointMatrix,massMatrix,excPointMatrix,readoutPointMatrix;
^super.newCopyArgs(resonators,connPointMatrix,massMatrix,excPointMatrix,readoutPointMatrix)
.init(resonators,connPointMatrix,massMatrix,excPointMatrix,readoutPointMatrix)
}
init { arg resonators,connPointMatrix,massMatrix,excPointMatrix,readoutPointMatrix;
this.resonators = resonators ? [Resonator1D.new,Resonator1D.new(101)];
this.connPointMatrix = connPointMatrix ? Array2D.fromArray(2,1,[0.5,0.5]);
this.massMatrix = massMatrix ? Array2D.fromArray(2,1,[1,1]);
this.excPointMatrix = excPointMatrix ? Array2D.fromArray(2,1,[0.25,0]);
this.readoutPointMatrix = readoutPointMatrix ? Array2D.fromArray(2,1,[0.5,0])
}
resonators_ { arg newResonators;
(newResonators.size == 0).if { newResonators = [newResonators].asList };
newResonators.respondsTo('values').if { newResonators = newResonators.values };
newResonators.isKindOf(Array).if { newResonators = newResonators.asList };
resonators = newResonators
}
connPointMatrix_ { arg newConnPointMatrix;
newConnPointMatrix.respondsTo('rows').if {
connPointMatrix = newConnPointMatrix
} {
(newConnPointMatrix.shape.size != 2).if {
Error("arg connPointMatrix=% must be a 2D indexable collection".format(newConnPointMatrix)).throw
} {
(newConnPointMatrix any: { |arr| arr.size != newConnPointMatrix[0].size }).if {
Error("arg connPointMatrix=% must contain equal sized rows".format(newConnPointMatrix)).throw
} {
connPointMatrix = newConnPointMatrix
}
}
}
}
massMatrix_ { arg newMassMatrix;
newMassMatrix.respondsTo('rows').if {
massMatrix = newMassMatrix
} {
(newMassMatrix.shape.size != 2).if {
Error("arg massMatrix=% must be a 2D indexable collection".format(newMassMatrix)).throw
} {
(newMassMatrix any: { |arr| arr.size != newMassMatrix[0].size }).if {
Error("arg massMatrix=% must contain equal sized rows".format(newMassMatrix)).throw
} {
massMatrix = newMassMatrix
}
}
}
}
excPointMatrix_ { arg newExcPointMatrix;
newExcPointMatrix.respondsTo('rows').if {
excPointMatrix = newExcPointMatrix
} {
(newExcPointMatrix.shape.size != 2).if {
Error("arg excPointMatrix=% must be a 2D indexable collection".format(newExcPointMatrix)).throw
} {
(newExcPointMatrix any: { |arr| arr.size != newExcPointMatrix[0].size }).if {
Error("arg excPointMatrix=% must contain equal sized rows".format(newExcPointMatrix)).throw
} {
excPointMatrix = newExcPointMatrix
}
}
}
}
readoutPointMatrix_ { arg newReadoutPointMatrix;
newReadoutPointMatrix.respondsTo('rows').if {
readoutPointMatrix = newReadoutPointMatrix
} {
(newReadoutPointMatrix.shape.size != 2).if {
Error("arg readoutPointMatrix=% must be a 2D indexable collection".format(newReadoutPointMatrix)).throw
} {
(newReadoutPointMatrix any: { |arr| arr.size != newReadoutPointMatrix[0].size }).if {
Error("arg readoutPointMatrix=% must contain equal sized rows".format(newReadoutPointMatrix)).throw
} {
readoutPointMatrix = newReadoutPointMatrix
}
}
}
}
calcModalData { arg minFreq=25,maxFreq=22050,minT60=0.01,gain=1,pathname=pythonScriptPath ++ "/modalData.json",incl="ynnn",async=false;
var cmdSeq;
this.prCheckMatrixDimensions;
this.prParseArgsAsJSON(minFreq,maxFreq,minT60,gain,pathname,incl);
cmdSeq = "cd % && % -m pmlib -s % % %".format(
pythonScriptPath, pythonPath, maxFreq * 2, "networkArgs.json", pathname);
async.if { cmdSeq.unixCmd } { cmdSeq.systemCmd };
this.prParseModalData(pathname)
}
loadModalData { arg pathname;
this.prParseModalData(pathname)
}
/*
*******************
* PRIVATE METHODS *
*******************
*/
prCheckMatrixDimensions {
var err=Error("matrix must have the same nr. of rows as there are items in resonators");
[connPointMatrix,massMatrix,excPointMatrix,readoutPointMatrix] do: { |mtr|
var op = mtr.respondsTo('rows').if { 'rows' } { 'size' };
(mtr.perform(op) != resonators.size).if { err.throw }
};
err=Error("args connPointMatrix and massMatrix must have the same dimensions");
(connPointMatrix.respondsTo('rows') and: { massMatrix.respondsTo('rows') }).if {
(connPointMatrix.rows == massMatrix.rows and: { connPointMatrix.cols == massMatrix.cols }).not.if { err.throw }
} {
(connPointMatrix.respondsTo('rows') and: { massMatrix.respondsTo('rows').not }).if {
(connPointMatrix.rows == massMatrix.size and: { connPointMatrix.cols == massMatrix[0].size }).not.if { err.throw }
} {
(connPointMatrix.respondsTo('rows').not and: { massMatrix.respondsTo('rows') }).if {
(connPointMatrix.size == massMatrix.rows and: { connPointMatrix[0].size == massMatrix.cols }).not.if { err.throw }
} {
(connPointMatrix.shape != massMatrix.shape).if { err.throw }
}
}
}
}
prParseArgsAsJSON { arg minFreq,maxFreq,minT60,gain,pathname,incl;
var matrixToString = { |mtr|
var str = "[";
mtr.isKindOf(Array2D).if {
mtr rowsDo: { |row,j| str = str ++ row ++ (j == (mtr.rows-1)).if { "]\n" } { "," } }
} {
mtr do: { |row,j| str = str ++ row ++ (j == (mtr.size-1)).if { "]\n" } { "," } }
};
str
};
File.use(pythonScriptPath ++ "/networkArgs.json","w",{ |f|
var json = "{ \"minFreq\":" ++ minFreq ++ ",\"maxFreq\":" ++ maxFreq ++ ",\"minT60\":" ++ minT60 ++ ",\"gain\":" ++ gain ++ ",\"incl\":" ++ "\"" ++ incl ++ "\"" ++ ",\"path\":" ++ "\"" ++ pathname ++ "\"" ++ ",\"resonators\":" ++ "[";
resonators do: { |obj,i|
json = json ++ obj.jsonString ++ (i == resonators.lastIndex).if { "]," } { "," }
};
json = json ++ "\"connPointMatrix\":" ++ matrixToString.(connPointMatrix) ++ ",";
json = json ++ "\"massMatrix\":" ++ matrixToString.(massMatrix) ++ ",";
json = json ++ "\"excPointMatrix\":" ++ matrixToString.(excPointMatrix) ++ ",";
json = json ++ "\"readoutPointMatrix\":" ++ matrixToString.(readoutPointMatrix) ++ " }";
f.write(json)
})
}
prParseModalData { arg pathname;
File.use(pathname,"r",{ |f|
File.exists(pathname).not.if {
Error("The file % does not exist. Hence, it is likely that no modal data has been calculated.".format(pathname)).throw
};
modalData = pathname.parseYAMLFile.dataAsStringToFloat;
})
}
}
+ SequenceableCollection {
indicesOfNotEqual { |item|
var indices=Array.new;
this.do { arg val, i;
if (item != val) { indices = indices.add(i) }
};
^indices
}
indicesOfLessThan { |item|
var indices=Array.new;
this.do { arg val, i;
if (item >= val) { indices = indices.add(i) }
};
^indices
}
indicesOfLessThanAbs { |item|
var indices=Array.new;
this.do { arg val, i;
if (item.abs >= val.abs) { indices = indices.add(i) }
};
^indices
}
indicesOfGreaterThan { |item|
var indices=Array.new;
this.do { arg val, i;
if (item <= val) { indices = indices.add(i) }
};
^indices
}
indicesOfGreaterThanAbs { |item|
var indices=Array.new;
this.do { arg val, i;
if (item.abs <= val.abs) { indices = indices.add(i) }
};
^indices
}
}
// converts all dictionary data in string format to float format
+ Dictionary {
dataAsStringToFloat {
var stringToFloat = { |key,val|
val.isKindOf(Dictionary).if {
val.keysValuesChange(stringToFloat)
} {
val.asFloat
}
};
this.keysValuesChange(stringToFloat)
}
}