Skip to content

Commit

Permalink
use complex class
Browse files Browse the repository at this point in the history
  • Loading branch information
td-krzysiek committed Sep 1, 2019
1 parent 0daa1ca commit 5ca5c18
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 117 deletions.
49 changes: 23 additions & 26 deletions src/Smith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ import { SmithConstantCircle } from './SmithConstantCircle';
import { SmithArcsDefs } from './SmithArcsDefs';

import { RadiallyScaledParams } from './radially_scaled_params/RadiallyScaledParams';

interface SmithCirclesDrawOptions {
stroke: string; minorWidth: string; majorWidth: string;
}
import { Complex } from './complex/Complex';

export interface SmithCursorEvent {
reflectionCoefficient: Point;
impedance: Point|undefined;
admittance: Point|undefined;
reflectionCoefficient: Complex;
impedance: Complex|undefined;
admittance: Complex|undefined;
swr: number;
returnLoss: number;
mismatchLoss: number; // reflection loss
Expand All @@ -47,9 +44,9 @@ export interface SmithCursorEvent {
export interface SmithMarkerEvent {
datasetNo: number;
markerNo: number;
reflectionCoefficient: Point;
impedance: Point|undefined;
admittance: Point|undefined;
reflectionCoefficient: Complex;
impedance: Complex|undefined;
admittance: Complex|undefined;
swr: number;
returnLoss: number;
mismatchLoss: number;
Expand Down Expand Up @@ -189,7 +186,9 @@ export class Smith {


private cursorMove(p: Point): void {
this.cursor.Position = this.scalers.default.pointInvert(p);
this.cursor.Position = Complex.fromArray(
this.scalers.default.pointInvert(p)
);
}

private initCursor(): SmithCursor {
Expand All @@ -205,7 +204,7 @@ export class Smith {
}

public get CursorData(): SmithCursorEvent {
const rc: Point = this.cursor.Position;
const rc = this.cursor.Position;
return {
reflectionCoefficient: rc,
impedance: this.calcImpedance(rc),
Expand Down Expand Up @@ -266,13 +265,13 @@ export class Smith {
return new SmithCircle(c, opts);
}

public getReactanceComponentValue(p: Point, f: number): string {
public getReactanceComponentValue(p: Complex, f: number): string {
const z = this.calcs.rflCoeffToImpedance(p);
if (!z) {
return 'Undefined';
}

const x = z[1] * this.Z0;
const x = z.imag * this.Z0;

if (x < 0) {
const cap = 1 / (2 * Math.PI * f * -x);
Expand All @@ -283,14 +282,14 @@ export class Smith {
return this.formatNumber(ind) + 'H';
}

public formatComplex(c: Point, unit: string = '', dp: number = 3): string {
public formatComplex(c: Complex, unit: string = '', dp: number = 3): string {
if (unit !== '') { unit = `[${unit}]`; }
return `${c[0].toFixed(dp)} ${c[1] < 0 ? '-' : '+'} j ${Math.abs(c[1]).toFixed(dp)} ${unit}`;
return `${c.real.toFixed(dp)} ${c.imag < 0 ? '-' : '+'} j ${Math.abs(c.imag).toFixed(dp)} ${unit}`;
}

public formatComplexPolar(c: Point, unit: string = '', dp: number = 3): string {
const m = Math.sqrt(c[0] * c[0] + c[1] * c[1]);
const a = Math.atan2(c[1], c[0]) * 180.0 / Math.PI;
public formatComplexPolar(c: Complex, unit: string = '', dp: number = 3): string {
const m = c.abs();
const a = c.arg() * 180.0 / Math.PI;
return `${m.toFixed(dp)} ${unit}${a.toFixed(dp)}°`;
}

Expand Down Expand Up @@ -342,7 +341,7 @@ export class Smith {
const m = this.data[datasetNo].getMarker(markerNo);
if (!m) { return; }

const rc = m.selectedPoint.point;
const rc = Complex.from(...m.selectedPoint.point);
const freq = m.selectedPoint.freq;

return {
Expand Down Expand Up @@ -389,19 +388,17 @@ export class Smith {
this.userActionHandler = handler;
}

public calcImpedance(rc: Point): Point|undefined {
public calcImpedance(rc: Complex): Complex|undefined {
const impedance = this.calcs.rflCoeffToImpedance(rc);
if (impedance) {
impedance[0] *= this.Z0;
impedance[1] *= this.Z0;
return impedance.mul(this.Z0);
}
return impedance;
}
public calcAdmittance(rc: Point): Point|undefined {
public calcAdmittance(rc: Complex): Complex|undefined {
const admittance = this.calcs.rflCoeffToAdmittance(rc);
if (admittance) {
admittance[0] *= 1 / this.Z0 * 1000.0; // mS
admittance[1] *= 1 / this.Z0 * 1000.0; // mS
return admittance.mul(1 / this.Z0 * 1000.0); // mS
}
return admittance;
}
Expand Down
81 changes: 40 additions & 41 deletions src/SmithConstantCircle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Circle } from './shapes/Circle';
import { Point } from './shapes/Point';
import { Complex } from './complex/Complex';

type Complex = [ number, number ];
// type Complex = [ number, number ];

export class SmithConstantCircle {
private epsilon = 1e-10;
Expand All @@ -10,51 +11,51 @@ export class SmithConstantCircle {
}

public rflCoeffToImpedance(c: Complex): Complex|undefined {
const gr = c[0];
const gi = c[1];
const gr = c.real;
const gi = c.imag;
const d = (1 - gr) * (1 - gr) + gi * gi;
if (Math.abs(d) < this.epsilon) {
return undefined;
}
const zr = (1 - gr * gr - gi * gi) / d;
const zi = (2 * gi) / d;
return [ zr, zi ];
return Complex.from(zr, zi);
}

public impedanceToRflCoeff(c: Complex): Complex|undefined {
const zr = c[0];
const zi = c[1];
const zr = c.real;
const zi = c.imag;
const d = (zr + 1) * (zr + 1) + zi * zi;
if (Math.abs(d) < this.epsilon) {
return undefined;
}
const gr = (zr * zr + zi * zi - 1) / d;
const gi = (2 * zi) / d;
return [ gr, gi ];
return Complex.from(gr, gi);
}

public rflCoeffToAdmittance(c: Complex): Complex|undefined {
const gr = c[0];
const gi = c[1];
const gr = c.real;
const gi = c.imag;
const d = (gr + 1) * (gr + 1) + gi * gi;
if (Math.abs(d) < this.epsilon) {
return undefined;
}
const yr = (1 - gr * gr - gi * gi) / d;
const yi = (-2 * gi) / d;
return [ yr, yi ];
return Complex.from(yr, yi);
}

public admittanceToRflCoeff(c: Complex): Complex|undefined {
const yr = c[0];
const yi = c[1];
const yr = c.real;
const yi = c.imag;
const d = (yr + 1) * (yr + 1) + yi * yi;
if (Math.abs(d) < this.epsilon) {
return undefined;
}
const gr = (1 - yr * yr - yi * yi) / d;
const gi = (-2 * yi) / d;
return [ gr, gi ];
return Complex.from(gr, gi);
}

public resistanceCircle(n: number): Circle {
Expand All @@ -79,9 +80,7 @@ export class SmithConstantCircle {
}

public rflCoeffToSwr(rc: Complex): number {
const x = rc[0];
const y = rc[1];
const gamma = Math.sqrt(x * x + y * y);
const gamma = rc.abs();
return (1 + gamma) / (1 - gamma);
}

Expand Down Expand Up @@ -129,7 +128,7 @@ export class SmithConstantCircle {
}

public rflCoeffP(rc: Complex): number {
return rc[0] * rc[0] + rc[1] * rc[1];
return rc.abs() ** 2;
}

public rflCoeffPToEOrI(p: number): number {
Expand All @@ -139,7 +138,7 @@ export class SmithConstantCircle {
public rflCoeffToQ(rc: Complex): number|undefined {
const impedance = this.rflCoeffToImpedance(rc);
if (!impedance) { return; }
return Math.abs(impedance[1] / impedance[0]);
return Math.abs(impedance.imag / impedance.real);
}

public rflCoeffToTransmCoeffP(rc: Complex): number {
Expand All @@ -152,17 +151,17 @@ export class SmithConstantCircle {

public rflCoeffToTransmCoeff(rc: Complex): Complex {
// T = R + 1
return [ rc[0] + 1, rc[1] ];
return rc.add(1);
}

public transmCoeffToRflCoeff(tc: Complex): Complex {
return [ tc[0] - 1, tc[1] ];
return tc.sub(1);
}

public rflCoeffToTransmCoeffEOrI(rc: Complex): number {
// T = R + 1
const tc = this.rflCoeffToTransmCoeff(rc);
return Math.sqrt(tc[0] ** 2 + tc[1] ** 2);
return tc.abs();
}

// s. w. peak (const. p)
Expand All @@ -186,6 +185,10 @@ export class SmithConstantCircle {
return Math.sqrt((swl - 1) / (swl + 1));
}

// public transmCoeffToInsertionLoss(tc: Complex): number {
// return -20 * Math.log10()
// }

public circleCircleIntersection(c1: Circle, c2: Circle): Point[] {
const dl = Math.sqrt(Math.pow(c2.p[0] - c1.p[0], 2) + Math.pow(c2.p[1] - c1.p[1], 2));

Expand All @@ -208,12 +211,12 @@ export class SmithConstantCircle {
return (Math.pow(p[0] - c.p[0], 2) + Math.pow(p[1] - c.p[1], 2)) <= (Math.pow(c.r, 2));
}

public normalize(p: Point): Point {
return [ p[0] / this.Z0, p[1] / this.Z0 ];
public normalize(c: Complex): Complex {
return c.div(this.Z0);
}

public denormalize(p: Point): Point {
return [ p[0] * this.Z0, p[1] * this.Z0 ];
public denormalize(c: Complex): Complex {
return c.mul(this.Z0);
}

public waveLengthFromFrequency(frequency: number): number {
Expand All @@ -224,10 +227,8 @@ export class SmithConstantCircle {
return 299792458 * waveLength;
}

public magnitude(p: Point): number {
const real = p[0];
const imag = p[1];
return Math.sqrt(real * real + imag * imag);
public magnitude(c: Complex): number {
return c.abs();
}

public dB(value: number): number {
Expand All @@ -239,36 +240,34 @@ export class SmithConstantCircle {
return -1 / (2 * Math.PI * f * x);
}

public capacitanceToReactance(C: number, f: number): number {
return -1 / (2 * Math.PI * f * C);
public capacitanceToReactance(C: number, f: number): Complex {
return Complex.from(0, -1 / (2 * Math.PI * f * C));
}

public reactanceToInductance(x: number, f: number): number|null {
if (x <= 0) { return null; }
return x / (2 * Math.PI * f);
}

public inductanceToReactance(L: number, f: number): number {
return 2 * Math.PI * f * L;
public inductanceToReactance(L: number, f: number): Complex {
return Complex.from(0, 2 * Math.PI * f * L);
}

public addImpedance(rc: Point, [R, X]: [number, number]): Point|undefined {
const Z = this.rflCoeffToImpedance(rc);
public addImpedance(rc: Complex, imp: Complex): Complex|undefined {
let Z = this.rflCoeffToImpedance(rc);
if (Z === undefined) {
return undefined;
}
Z[0] += R / this.Z0;
Z[1] += X / this.Z0;
Z = Z.add(imp.div(this.Z0));
return this.impedanceToRflCoeff(Z);
}

public addAdmittance(rc: Point, [G, B]: [number, number]): Point|undefined {
const Y = this.rflCoeffToAdmittance(rc);
public addAdmittance(rc: Complex, adm: Complex): Complex|undefined {
let Y = this.rflCoeffToAdmittance(rc);
if (Y === undefined) {
return undefined;
}
Y[0] += G * this.Z0;
Y[1] += B * this.Z0;
Y = Y.add(adm.mul(this.Z0));
return this.admittanceToRflCoeff(Y);
}

Expand Down
Loading

0 comments on commit 5ca5c18

Please sign in to comment.