-
Notifications
You must be signed in to change notification settings - Fork 7
/
sacpolezero.ts
279 lines (232 loc) · 6.97 KB
/
sacpolezero.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Philip Crotwell
* University of South Carolina, 2019
* http://www.seis.sc.edu
*/
import {Complex} from "./oregondsputil";
import {isNumArg, stringify} from "./util";
/**
* SAC style response file. This contains poles and zeros to represent the
* analog stage, plus a constant for the overall gain. See
* seisplotjs.transfer.convertToSacPoleZero() for conversion from a
* StationXML response to SacPoleZero.
*/
export class SacPoleZero {
/**
* Complex poles
*/
poles: Array<InstanceType<typeof Complex>>;
/**
* Complex zeros
*/
zeros: Array<InstanceType<typeof Complex>>;
/**
* Scalar overall gain
*/
constant: number;
/** number of zeros added to convert to displacement, for debugging */
gamma: number|undefined;
/** hertz/radian factor, for debugging */
mulFactor: number;
/** sensitivity accounting for gamma, for debugging */
sd: number;
/** normalization factor for poles and zeros accounting for gamma, for debugging */
A0: number;
constructor(poles: Array<InstanceType<typeof Complex>>, zeros: Array<InstanceType<typeof Complex>>, constant: number) {
this.poles = poles;
this.zeros = zeros;
this.constant = constant;
this.mulFactor = 1;
this.sd = 1;
this.A0 = 1;
}
toString(): string {
const s = ["sacPoleZero:"];
s.push("ZEROS " + this.zeros.length);
for (let i = 0; i < this.zeros.length; i++) {
s.push(" " + this.zeros[i].real() + " " + this.zeros[i].imag());
}
s.push("POLES " + this.poles.length);
for (let i = 0; i < this.poles.length; i++) {
s.push(" " + this.poles[i].real() + " " + this.poles[i].imag());
}
s.push("CONSTANT " + this.constant);
if (
isNumArg(this.gamma) &&
isNumArg(this.mulFactor) &&
isNumArg(this.sd) &&
isNumArg(this.A0)
) {
s.push("* gamma: " + this.gamma);
s.push("* mulFactor: " + this.mulFactor);
s.push("* sd: " + this.sd);
s.push("* A0: " + this.A0);
}
return s.join("\n");
}
evalPoleZeroInverse(freq: number): InstanceType<typeof Complex> {
const s = new Complex(0, 2 * Math.PI * freq);
let zeroOut = new Complex(1, 0);
let poleOut = new Complex(1, 0);
for (let i = 0; i < this.poles.length; i++) {
poleOut = poleOut.timesComplex(s.minusComplex(this.poles[i]));
}
for (let i = 0; i < this.zeros.length; i++) {
if (
s.real() === this.zeros[i].real() &&
s.imag() === this.zeros[i].imag()
) {
return new Complex(0, 0);
}
zeroOut = zeroOut.timesComplex(s.minusComplex(this.zeros[i]));
}
const out = poleOut.overComplex(zeroOut);
return out.overReal(this.constant);
}
trimZeros(gamma: number) {
const sacPoleZero = this;
for (let i = 0; i < gamma; i++) {
const z = sacPoleZero.zeros[sacPoleZero.zeros.length - 1 - i];
if (z.real() !== 0 || z.imag() !== 0) {
throw new Error(
`Attempt to trim ${gamma} zeros from SacPoleZero, but zero isn't 0+i0: ${stringify(
z,
)}`,
);
}
}
// subtract gama zeros, ex 1 to get
let trimmedZeros = sacPoleZero.zeros.slice().reverse();
for (let i = 0; i < gamma; i++) {
const idx = trimmedZeros.findIndex(d => d.real() === 0 && d.imag() === 0);
trimmedZeros.splice(idx, 1);
}
trimmedZeros = trimmedZeros.reverse();
sacPoleZero.zeros = trimmedZeros;
}
toText(): string {
const s = [];
s.push("ZEROS " + this.zeros.length);
for (let i = 0; i < this.zeros.length; i++) {
s.push(" " + this.zeros[i].real() + " " + this.zeros[i].imag());
}
s.push("POLES " + this.poles.length);
for (let i = 0; i < this.poles.length; i++) {
s.push(" " + this.poles[i].real() + " " + this.poles[i].imag());
}
s.push("CONSTANT " + this.constant);
return s.join("\n");
}
/**
* Caclulates the frequency response from the given poles and zeros.
*
* @param freqs frequencies to compute
* @returns frequency response
*/
calcForDisplay(freqs: Array<number>): Array<InstanceType<typeof Complex>> {
const out = freqs.map(freq => {
let respAtS = this.evalPoleZeroInverse(freq);
respAtS = new Complex(1, 0).overComplex(respAtS);
return respAtS;
});
return out;
}
/**
* Parses a string in sac polezero format into a SacPoleZero.
*
* @param data string to parse
* @returns SacPoleZero instance
*/
static parse(data: string): SacPoleZero {
const pz = {
zeros: Array<InstanceType<typeof Complex>>(0),
poles: Array<InstanceType<typeof Complex>>(0),
constant: 1,
};
const lines = data.split("\n");
let numZeros = 0;
let numPoles = 0;
let i = 0;
while (i < lines.length) {
let l = lines[i];
let items = l.trim().split(/ +/);
if (items[0] === "ZEROS") {
numZeros = parseInt(items[1]);
i++;
l = lines[i];
items = l.trim().split(/ +/);
while (i < lines.length && pz.zeros.length < numZeros) {
if (items[0] === "POLES") {
// no more zeros, fill array with 0
for (let z = pz.zeros.length; z < numZeros; z++) {
pz.zeros.push(new Complex(0, 0));
}
break;
} else {
const real = parseFloat(items[0]);
const imag = parseFloat(items[1]);
pz.zeros.push(new Complex(real, imag));
}
i++;
l = lines[i];
items = l.trim().split(/ +/);
}
}
if (items[0] === "POLES") {
numPoles = parseInt(items[1]);
i++;
l = lines[i];
items = l.trim().split(/ +/);
while (i < lines.length && pz.poles.length < numPoles) {
if (items[0] === "CONSTANT") {
// no more poles, fill array with 0
for (let z = pz.poles.length; z < numPoles; z++) {
pz.poles.push(new Complex(0, 0));
}
break;
} else {
const real = parseFloat(items[0]);
const imag = parseFloat(items[1]);
pz.poles.push(new Complex(real, imag));
}
i++;
l = lines[i];
items = l.trim().split(/ +/);
}
}
if (items[0] === "CONSTANT") {
pz.constant = parseFloat(items[1]);
}
i++;
}
return new SacPoleZero(pz.poles, pz.zeros, pz.constant);
}
}
export function geomspace(
start: number,
stop: number,
num: number,
): Array<number> {
const log_start = Math.log(start);
const log_stop = Math.log(stop);
return logspace(log_start, log_stop, num);
}
export function logspace(
start: number,
stop: number,
num: number,
): Array<number> {
return linspace(start, stop, num).map(n => Math.pow(10, n));
}
export function linspace(
start: number,
stop: number,
num: number,
): Array<number> {
const delta = (stop - start) / (num - 1);
const out = [];
for (let i = 0; i < num; i++) {
out.push(start + i * delta);
}
return out;
}