UNPKG

dsp-collection

Version:

A collection of JavaScript modules for digital signal processing (written in TypeScript)

171 lines 4.68 kB
import Complex from "./Complex.js"; import MutableComplex from "./MutableComplex.js"; import { assert } from "../utils/MiscUtils.js"; const emptyFloat64Array = new Float64Array(0); export default class ComplexArray { constructor(x = 0) { if (typeof x == "number") { this.constructByLength(x); } else if (Array.isArray(x) && x[0] instanceof Complex) { this.constructByArrayOfComplex(x); } else if (x instanceof Object && x.length !== undefined) { this.constructByArrayOfNumber(x); } else { throw new Error("Invalid constructor argument."); } } constructByLength(length) { this.length = length; if (length) { this.re = new Float64Array(length); this.im = new Float64Array(length); } else { this.re = emptyFloat64Array; this.im = emptyFloat64Array; } } constructByArrayOfComplex(a) { this.length = a.length; this.re = new Float64Array(a.length); this.im = new Float64Array(a.length); for (let i = 0; i < a.length; i++) { this.re[i] = a[i].re; this.im[i] = a[i].im; } } constructByArrayOfNumber(a) { this.length = a.length; this.re = new Float64Array(a); this.im = new Float64Array(a.length); } static fromPolar(absArray, argArray) { const n = absArray.length; assert(n == argArray.length); const a = new ComplexArray(n); for (let i = 0; i < n; i++) { a.setPolar(i, absArray[i], argArray[i]); } return a; } slice(begin, end) { const a2 = new ComplexArray(); a2.re = this.re.slice(begin, end); a2.im = this.im.slice(begin, end); a2.length = a2.re.length; return a2; } subarray(begin, end) { const a2 = new ComplexArray(); a2.re = this.re.subarray(begin, end); a2.im = this.im.subarray(begin, end); a2.length = end - begin; return a2; } set(i, c) { this.re[i] = c.re; this.im[i] = c.im; } setReIm(i, re, im) { this.re[i] = re; this.im[i] = im; } setPolar(i, abs, arg) { this.re[i] = abs * Math.cos(arg); this.im[i] = abs * Math.sin(arg); } static copy1(a1, i1, a2, i2) { a2.re[i2] = a1.re[i1]; a2.im[i2] = a1.im[i1]; } get(i) { return new MutableComplex(this.re[i], this.im[i]); } getAbs(i) { return Math.hypot(this.re[i], this.im[i]); } getArg(i) { return Math.atan2(this.im[i], this.re[i]); } toString() { let s = "["; for (let i = 0; i < this.length; i++) { if (i > 0) { s += ", "; } s += "(" + this.re[i] + ", " + this.im[i] + ")"; } s += "]"; return s; } getAbsArray() { const n = this.length; const a = new Float64Array(n); for (let i = 0; i < n; i++) { a[i] = this.getAbs(i); } return a; } getArgArray() { const n = this.length; const a = new Float64Array(n); for (let i = 0; i < n; i++) { a[i] = this.getArg(i); } return a; } addRealTo(i, x) { this.re[i] += x; } addTo(i, x) { this.re[i] += x.re; this.im[i] += x.im; } subRealFrom(i, x) { this.re[i] -= x; } subFrom(i, x) { this.re[i] -= x.re; this.im[i] -= x.im; } mulByReal(i, x) { this.re[i] *= x; this.im[i] *= x; } mulBy(i, x) { this.setMul(i, this.re[i], this.im[i], x.re, x.im); } divByReal(i, x) { this.re[i] /= x; this.im[i] /= x; } divBy(i, x) { this.setDiv(i, this.re[i], this.im[i], x.re, x.im); } mulByArray(a2) { const n = this.length; assert(a2.length == n); for (let i = 0; i < n; i++) { this.setMul(i, this.re[i], this.im[i], a2.re[i], a2.im[i]); } } mulAllByReal(x) { const n = this.length; for (let i = 0; i < n; i++) { this.mulByReal(i, x); } } setMul(i, re1, im1, re2, im2) { this.re[i] = re1 * re2 - im1 * im2; this.im[i] = re1 * im2 + im1 * re2; } setDiv(i, re1, im1, re2, im2) { const m = re1 * re1 + im1 * im1; this.re[i] = (re1 * re2 + im1 * im2) / m; this.im[i] = (im1 * re2 - re1 * im2) / m; } } //# sourceMappingURL=ComplexArray.js.map