-
Notifications
You must be signed in to change notification settings - Fork 7
/
transfer.ts
501 lines (458 loc) · 13.7 KB
/
transfer.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* Philip Crotwell
* University of South Carolina, 2019
* http://www.seis.sc.edu
*/
import {calcDFT, inverseDFT, FFTResult} from "./fft";
import {SeismogramSegment} from "./seismogramsegment";
import {Seismogram} from "./seismogram";
import {SacPoleZero} from "./sacpolezero";
import {Response, PolesZeros} from "./stationxml";
import {Complex} from "./oregondsputil";
// `allMeasures` includes all the measures packaged with this library
import configureMeasurements, { allMeasures, AllMeasuresUnits } from 'convert-units';
const convert = configureMeasurements(allMeasures);
/**
* Applies response, poles and zeros along with overall gain to the seismogram.
* Should produce results similar to the sac command:
* transfer from polezero to none
*
* @param seis seismogram to instrument correct
* @param response response to apply
* @param lowCut low cut
* @param lowPass low pass
* @param highPass high pass
* @param highCut high cut
* @returns instrument corrected seismogram
*/
export function transfer(
seis: Seismogram,
response: Response,
lowCut: number,
lowPass: number,
highPass: number,
highCut: number,
): Seismogram {
if (!response) {
throw new Error("Response not exist???");
}
const sacPoleZero = convertToSacPoleZero(response);
return transferSacPZ(seis, sacPoleZero, lowCut, lowPass, highPass, highCut);
}
export function transferSacPZ(
seis: Seismogram,
sacPoleZero: SacPoleZero,
lowCut: number,
lowPass: number,
highPass: number,
highCut: number,
): Seismogram {
const outSeis = [];
for (let i = 0; i < seis.segments.length; i++) {
const result = transferSacPZSegment(
seis.segments[i],
sacPoleZero,
lowCut,
lowPass,
highPass,
highCut,
);
outSeis.push(result);
}
return new Seismogram(outSeis);
}
export function transferSacPZSegment(
seis: SeismogramSegment,
sacPoleZero: SacPoleZero,
lowCut: number,
lowPass: number,
highPass: number,
highCut: number,
): SeismogramSegment {
const sampFreq = seis.sampleRate;
const values = seis.y;
let outData = Float32Array.from(values);
/* sac premultiplies the data by the sample period before doing the fft. Later it
* seems to be cancled out by premultiplying the pole zeros by a similar factor.
* I don't understand why they do this, but am reproducing it in order to be
* compatible.
*/
outData.forEach((d, i) => (outData[i] = d / sampFreq));
let freqValues = calcDFT(outData);
freqValues = combine(
freqValues,
sampFreq,
sacPoleZero,
lowCut,
lowPass,
highPass,
highCut,
);
outData = inverseDFT(freqValues, values.length);
// a extra factor of nfft gets in somehow???
outData.forEach((d, i) => (outData[i] = d * freqValues.length));
const out = seis.cloneWithNewData(outData);
out.yUnit = "m";
return out;
}
export function calcResponse(
response: Response,
numPoints: number,
sampleRate: number,
unit: string,
): FFTResult {
const sacPoleZero = convertToSacPoleZero(response);
const unitQty = convert(1).getUnit(unit as AllMeasuresUnits);
let gamma = 0;
if (unitQty === null) {
throw new Error("unknown response unit: " + unit);
} else if (unitQty.measure == 'length') {
gamma = 0;
} else if (unitQty.measure == 'speed') {
gamma = 1;
} else if (unitQty.measure == 'acceleration') {
gamma = 2;
} else {
throw new Error(
"response unit is not displacement (m), velocity (m/s) or acceleration (m/s^2): " +
unit,
);
}
sacPoleZero.trimZeros(gamma);
const out = calcResponseFromSacPoleZero(sacPoleZero, numPoints, sampleRate);
return out;
}
/**
* Caclulates the frequency response from the given poles and zeros.
*
* @param sacPoleZero poles and zeros
* @param numPoints number of points in the output fft
* @param sampleRate sample rate to compute at
* @returns frequency response
*/
export function calcResponseFromSacPoleZero(
sacPoleZero: SacPoleZero,
numPoints: number,
sampleRate: number,
): FFTResult {
const deltaF = sampleRate / numPoints;
// inst response as packed frequency array
const freqValues = new Float32Array(numPoints);
let respAtS;
// zero freq
respAtS = evalPoleZeroInverse(sacPoleZero, 0);
respAtS = new Complex(1, 0).overComplex(respAtS);
freqValues[0] = respAtS.real();
// nyquist
let freq = sampleRate / 2;
respAtS = evalPoleZeroInverse(sacPoleZero, freq);
respAtS = new Complex(1, 0).overComplex(respAtS);
freqValues[freqValues.length / 2] = respAtS.real();
for (let i = 1; i < freqValues.length / 2; i++) {
freq = i * deltaF;
respAtS = evalPoleZeroInverse(sacPoleZero, freq);
//respAtS = respAtS.timesReal(deltaF*i);
respAtS = new Complex(1, 0).overComplex(respAtS);
if (respAtS.real() !== 0 || respAtS.imag() !== 0) {
freqValues[i] = respAtS.real();
freqValues[freqValues.length - i] = respAtS.imag();
} else {
freqValues[i] = 1e-10;
freqValues[freqValues.length - i] = 0;
}
}
const out = FFTResult.createFromPackedFreq(freqValues, numPoints, sampleRate);
return out;
}
/**
* Applies poles and zeros to the fft of a time series. Modifies the freqValues
* in place.
*
* @param freqValues fft of a timeseries
* @param sampFreq sampling frequency
* @param sacPoleZero poles and zeros
* @param lowCut low cut
* @param lowPass low pass
* @param highPass high pass
* @param highCut high cut
* @returns input freq values, with poles and zeros applied
*/
export function combine(
freqValues: Float32Array,
sampFreq: number,
sacPoleZero: SacPoleZero,
lowCut: number,
lowPass: number,
highPass: number,
highCut: number,
): Float32Array {
const deltaF = sampFreq / freqValues.length;
// handle zero freq, no imag, set real to 0
freqValues[0] = 0;
// handle nyquist
let freq = sampFreq / 2;
let respAtS = evalPoleZeroInverse(sacPoleZero, freq);
respAtS = respAtS.timesReal(
deltaF * calcFreqTaper(freq, lowCut, lowPass, highPass, highCut),
);
freqValues[freqValues.length / 2] = respAtS
.timesReal(freqValues[freqValues.length / 2])
.real();
for (let i = 1; i < freqValues.length / 2; i++) {
freq = i * deltaF;
respAtS = evalPoleZeroInverse(sacPoleZero, freq);
respAtS = respAtS.timesReal(
deltaF * calcFreqTaper(freq, lowCut, lowPass, highPass, highCut),
);
const freqComplex = new Complex(
freqValues[i],
freqValues[freqValues.length - i],
).timesComplex(respAtS);
freqValues[i] = freqComplex.real();
freqValues[freqValues.length - i] = freqComplex.imag();
}
return freqValues;
}
/**
* Evaluates the poles and zeros at the given value. The return value is
* 1/(pz(s) to avoid divide by zero issues. If there is a divide by zero
* situation, then the response is set to be 0+0i.
*
* @param sacPoleZero SAC PoleZero response
* @param freq frequency to evaluate
* @returns complex frequency domain value for this frequency
*/
export function evalPoleZeroInverse(
sacPoleZero: SacPoleZero,
freq: number,
): InstanceType<typeof Complex> {
return sacPoleZero.evalPoleZeroInverse(freq);
}
/**
* Calculates the frequency taper for the given parameters.
*
* @param freq frequency
* @param lowCut low cut
* @param lowPass low pass
* @param highPass high pass
* @param highCut high cut
* @returns taper value at the frequency
*/
export function calcFreqTaper(
freq: number,
lowCut: number,
lowPass: number,
highPass: number,
highCut: number,
): number {
if (lowCut > lowPass || lowPass > highPass || highPass > highCut) {
throw new Error(
"must be lowCut > lowPass > highPass > highCut: " +
lowCut +
" " +
lowPass +
" " +
highPass +
" " +
highCut,
);
}
if (freq <= lowCut || freq >= highCut) {
return 0;
}
if (freq >= lowPass && freq <= highPass) {
return 1;
}
if (freq > lowCut && freq < lowPass) {
return (
0.5 * (1.0 + Math.cos((Math.PI * (freq - lowPass)) / (lowCut - lowPass)))
);
}
// freq > highPass && freq < highCut
return (
0.5 * (1.0 - Math.cos((Math.PI * (freq - highCut)) / (highPass - highCut)))
);
}
/**
* Applies the frequency taper to the fft of the time series.
*
* @param fftResult fft of time series
* @param sampleRate sample rate
* @param lowCut low cut
* @param lowPass low pass
* @param highPass high pass
* @param highCut high cut
* @returns fft with taper applied
*/
export function applyFreqTaper(
fftResult: FFTResult,
sampleRate: number,
lowCut: number,
lowPass: number,
highPass: number,
highCut: number,
): FFTResult {
const deltaF = fftResult.fundamentalFrequency;
return FFTResult.createFromAmpPhase(
fftResult.amplitudes().map((v, i) =>
i === 0
? 0
: v * calcFreqTaper(i * deltaF, lowCut, lowPass, highPass, highCut),
),
fftResult.phases(),
fftResult.origLength,
fftResult.sampleRate,
);
}
/**
* commonly used units
*/
export const METER = convert().getUnit("m");
export const METER_PER_SECOND = convert().getUnit("m/s");
export const METER_PER_SECOND_PER_SECOND = convert().getUnit("m/s2");
export function calcGamma(unit: string): number {
let gamma;
const unitQty = convert(1).getUnit(unit as AllMeasuresUnits);
if (unitQty === null) {
throw new Error("unknown response unit: " + unit);
} else if (unitQty.measure == 'length') {
gamma = 0;
} else if (unitQty.measure == 'speed') {
gamma = 1;
} else if (unitQty.measure == 'acceleration') {
gamma = 2;
} else {
throw new Error(
"response unit is not displacement (m), velocity (m/s) or acceleration (m/s^2): " +
unit,
);
}
return gamma;
}
export function calcScaleUnit(unit: string): number {
let scale;
const unitQty = convert(1).getUnit(unit as AllMeasuresUnits);
if (unitQty === null) {
throw new Error("unknown response unit: " + unit);
} else if (unitQty.measure == 'length') {
scale = convert(1).from(unit as AllMeasuresUnits).to('m');
} else if (unitQty.measure == 'speed') {
scale = convert(1).from(unit as AllMeasuresUnits).to('m/s');
} else if (unitQty.measure == 'acceleration') {
scale = convert(1).from(unit as AllMeasuresUnits).to('m/s2');
} else {
throw new Error(
"response unit is not displacement (m), velocity (m/s) or acceleration (m/s^2): " +
unit,
);
}
return scale;
}
/**
* Converts a StationXML response to SAC PoleZero style. This
* converts the analog to digital stage (usually 0) along
* with the overall gain, but does not include later FIR stages.
* To maintain compatibility with SAC, this includes extra zeros
* if needed to convert to displacement. The number of extra zeros
* added is kept as gamma in the result.
*
* @param response stationxml Response to convert
* @returns SAC PoleZero style version of the response
*/
export function convertToSacPoleZero(response: Response): SacPoleZero {
let polesZeros: PolesZeros;
if (response.stages[0].filter instanceof PolesZeros) {
polesZeros = response.stages[0].filter;
} else {
throw new Error("can't find PolesZeros");
}
if (response.instrumentSensitivity === null) {
throw new Error("response.instrumentSensitivity missing");
}
let unit = response.instrumentSensitivity.inputUnits;
if (unit === "M/S" || unit === "M/SEC") {
unit = "m/s";
}
let gamma = calcGamma(unit);
let scaleUnit = calcScaleUnit(unit);
const scale_sensitivity =
scaleUnit * response.instrumentSensitivity.sensitivity;
return convertPoleZeroToSacStyle(
polesZeros,
scale_sensitivity,
response.instrumentSensitivity.frequency,
gamma,
);
}
export function convertPoleZeroToSacStyle(
polesZeros: PolesZeros,
sensitivity: number,
sensitivity_freq: number,
gamma: number,
): SacPoleZero {
let mulFactor = 1;
if (polesZeros.pzTransferFunctionType === "LAPLACE (HERTZ)") {
mulFactor = 2 * Math.PI;
}
const zeros = [];
// extra gamma zeros are (0,0)
for (let i = 0; i < polesZeros.zeros.length; i++) {
zeros[i] = new Complex(
polesZeros.zeros[i].real() * mulFactor,
polesZeros.zeros[i].imag() * mulFactor,
);
}
for (let i = 0; i < gamma; i++) {
zeros.push(new Complex(0, 0));
}
const poles = [];
for (let i = 0; i < polesZeros.poles.length; i++) {
poles[i] = new Complex(
polesZeros.poles[i].real() * mulFactor,
polesZeros.poles[i].imag() * mulFactor,
);
}
let constant = polesZeros.normalizationFactor;
let sd = sensitivity;
const fs = sensitivity_freq;
sd *= Math.pow(2 * Math.PI * fs, gamma);
let A0 = polesZeros.normalizationFactor;
const fn = polesZeros.normalizationFrequency;
A0 = A0 / Math.pow(2 * Math.PI * fn, gamma);
if (polesZeros.pzTransferFunctionType === "LAPLACE (HERTZ)") {
A0 *= Math.pow(
2 * Math.PI,
polesZeros.poles.length - polesZeros.zeros.length,
);
}
if (poles.length === 0 && zeros.length === 0) {
constant = sd * A0;
} else {
constant = sd * calc_A0(poles, zeros, fs);
}
const sacPZ = new SacPoleZero(poles, zeros, constant);
sacPZ.gamma = gamma;
sacPZ.mulFactor = mulFactor;
sacPZ.sd = sd;
sacPZ.A0 = A0;
return sacPZ;
}
export function calc_A0(
poles: Array<InstanceType<typeof Complex>>,
zeros: Array<InstanceType<typeof Complex>>,
ref_freq: number,
): number {
let numer = new Complex(1, 0);
let denom = new Complex(1, 0);
let f0;
let a0;
f0 = new Complex(0, 2 * Math.PI * ref_freq);
for (let i = 0; i < zeros.length; i++) {
denom = denom.timesComplex(f0.minusComplex(zeros[i]));
}
for (let i = 0; i < poles.length; i++) {
numer = numer.timesComplex(f0.minusComplex(poles[i]));
}
a0 = numer.overComplex(denom).abs();
return a0;
}