Skip to content

Commit

Permalink
Incorrect definition of transmission coefficient
Browse files Browse the repository at this point in the history
  • Loading branch information
mfikes committed Jan 15, 2024
1 parent 0e9215a commit e4c705b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@ struct ReflectionCoefficientPowerView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
UnitInputView(value: $viewModel.reflectionCoefficientPower, unit: ReflectionCoefficientPowerUnit.ρ², label: "ρ²", description: "refl. coeff. power")
UnitInputView(value: $viewModel.reflectionCoefficientPower, unit: ReflectionCoefficientPowerUnit.ρ², label: "RP", description: "refl. power")
}
}

struct TransmissionCoefficientView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
UnitInputView(value: $viewModel.transmissionCoefficient, unit: TransmissionCoefficientUnit.τ, label: "τ", description: "trans. coeff.")
UnitInputView(value: $viewModel.transmissionCoefficientTau, unit: TransmissionCoefficientUnit.τ, label: "τ", description: "trans. coeff.")
}
}

struct TransmissionCoefficientPowerView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
UnitInputView(value: $viewModel.transmissionCoefficientPower, unit: TransmissionCoefficientPowerUnit.τ², label: "τ²", description: "trans. coeff. power")
UnitInputView(value: $viewModel.transmissionCoefficientPower, unit: TransmissionCoefficientPowerUnit.P, label: "TP", description: "trans. power")
}
}

Expand All @@ -56,11 +56,11 @@ struct ReturnLossView: View {
}
}

struct TransmissionLossView: View {
struct ReflectionLossView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
UnitInputView(value: $viewModel.transmissionLoss, unit: TransmissionLossUnit.dB, label: "TL", description: "transmission loss")
UnitInputView(value: $viewModel.reflectionLoss, unit: ReflectionLossUnit.dB, label: "RL", description: "reflection loss")
}
}

Expand Down Expand Up @@ -93,7 +93,7 @@ struct CharacterizationView: View {
DisplayView {
HStack {
ReturnLossView(viewModel: viewModel)
TransmissionLossView(viewModel: viewModel)
ReflectionLossView(viewModel: viewModel)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Impedance Converter/Impedance Converter/Units.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ enum TransmissionCoefficientUnit: String, UnitWithPowerOfTen {
}

enum TransmissionCoefficientPowerUnit: String, UnitWithPowerOfTen {
case τ²
case P
var id: Self { self }
var basePower: Int { 0 }
var shouldRender: Bool {
return self != .τ²
return self != .P
}
}

enum TransmissionLossUnit: String, UnitWithPowerOfTen {
enum ReflectionLossUnit: String, UnitWithPowerOfTen {
case dB
var id: Self { self }
var basePower: Int { 0 }
Expand Down
39 changes: 18 additions & 21 deletions Impedance Converter/Impedance Converter/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,44 +483,41 @@ class ViewModel: ObservableObject, Codable {
}
}

var transmissionCoefficient: Double {
var transmissionCoefficient: Complex<Double> {
get {
if (unityReflectionCoefficient()) {
return 0
} else {
return 1 - reflectionCoefficient.length
}
1 + reflectionCoefficient
}
set {
guard newValue >= 0 && newValue <= 1 else { return }
let reflectionCoefficientLength = 1 - newValue
reflectionCoefficient = Complex.init(length: reflectionCoefficientLength, phase: reflectionCoefficient.phase)
reflectionCoefficient = newValue - 1
}
}

var transmissionCoefficientTau: Double {
get {
return transmissionCoefficient.length
}
set {
guard !transmissionCoefficient.phase.isNaN else { return }
transmissionCoefficient = Complex.init(length: newValue, phase: transmissionCoefficient.phase)
}
}

var transmissionCoefficientPower: Double {
get {
if (unityReflectionCoefficient()) {
return 0
} else {
return transmissionCoefficient * transmissionCoefficient
}
1 - reflectionCoefficientPower
}
set {
guard newValue >= 0 && newValue <= 1 else { return }
transmissionCoefficient = sqrt(newValue)
reflectionCoefficientPower = 1 - newValue
}
}

var transmissionLoss: Double {
var reflectionLoss: Double {
get {
let transmissionCoefficientValue = transmissionCoefficientPower
return -10 * log10(transmissionCoefficientValue)
return -10 * log10(transmissionCoefficientPower)
}
set {
guard newValue >= 0 else { return }
let transmissionCoefficientValue = pow(10, -newValue / 10)
transmissionCoefficientPower = transmissionCoefficientValue
transmissionCoefficientPower = pow(10, -newValue / 10)
}
}

Expand Down
60 changes: 16 additions & 44 deletions Impedance Converter/Impedance ConverterTests/ViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -585,28 +585,23 @@ class TransmissionParametersTests: ViewModelTestBase {
let reflectionCoefficient = Complex(0.5, 0)
viewModel.reflectionCoefficient = reflectionCoefficient

let expectedTransmisionCoefficientPower = pow(1 - reflectionCoefficient.length, 2)
let expectedTransmisionCoefficientPower = 1 - reflectionCoefficient.lengthSquared
XCTAssertEqual(viewModel.transmissionCoefficientPower, expectedTransmisionCoefficientPower)

let newTransmissionCoefficientPower = 0.3
viewModel.transmissionCoefficientPower = newTransmissionCoefficientPower
let expectedReflectionCoefficientMagnitude = 1 - sqrt(newTransmissionCoefficientPower)
XCTAssertEqual(viewModel.reflectionCoefficient.magnitude, expectedReflectionCoefficientMagnitude, accuracy: 1e-6)
}

func testReflectionCoefficientTransmissionCoefficient() {
viewModel.reactance = 40
property("Reflection coefficient plus transmission coefficient must sum to 1") <- forAll( Gen<Double>.choose((0, 1)) ) { rho in
self.viewModel.reflectionCoefficientRho = rho
return abs(rho + self.viewModel.transmissionCoefficient - 1) < 1e-6
property("Reflection coefficient power plus transmission coefficient power must sum to 1") <- forAll( Gen<Double>.choose((0, 1)) ) { rho in
self.viewModel.reflectionCoefficientPower = rho
return abs(rho + self.viewModel.transmissionCoefficientPower - 1) < 1e-6
}
}

func testTransmissionCoefficientReflectionCoefficient() {
viewModel.reactance = 40
property("Reflection coefficient plus transmission coefficient must sum to 1") <- forAll( Gen<Double>.choose((0, 1)) ) { tau in
self.viewModel.transmissionCoefficient = tau
return abs(self.viewModel.reflectionCoefficientRho + tau - 1) < 1e-6
property("Reflection coefficient power plus transmission coefficient power must sum to 1") <- forAll( Gen<Double>.choose((0, 1)) ) { tau in
self.viewModel.transmissionCoefficientPower = tau
return abs(self.viewModel.reflectionCoefficientPower + tau - 1) < 1e-6
}
}

Expand All @@ -632,44 +627,21 @@ class TransmissionParametersTests: ViewModelTestBase {
return self.viewModel.returnLoss.isZero
}
}

// Testing Transmission Coefficient
func testTransmissionCoefficient() {
let reflectionCoefficient = Complex(0.5, 0)
viewModel.reflectionCoefficient = reflectionCoefficient

// Check transmission coefficient calculation
let expectedTransmissionCoefficient = 1 - reflectionCoefficient.length
XCTAssertEqual(viewModel.transmissionCoefficient, expectedTransmissionCoefficient)

// Set transmission coefficient and check reflection coefficient changes
let newTransmissionCoefficient = 0.75
viewModel.transmissionCoefficient = newTransmissionCoefficient
let expectedReflectionCoefficientMagnitude = 1 - newTransmissionCoefficient
XCTAssertEqual(viewModel.reflectionCoefficient.magnitude, expectedReflectionCoefficientMagnitude, accuracy: 1e-6)
}

func testTransmissionCoefficientZero() {
property("Transmission coefficient should be zero for reflection coefficient length of 1 at any phase") <- forAll { (phase: Double) in
self.viewModel.reflectionCoefficient = Complex(length: 1.0, phase: phase)
return self.viewModel.transmissionCoefficient.isZero
}
}

// Testing Transmission Loss
func testTransmissionLoss() {
viewModel.reactance = 50
viewModel.transmissionCoefficient = 0.75
viewModel.transmissionCoefficientPower = 0.75

// Check transmission loss calculation
let expectedTransmissionLoss = -20 * log10(viewModel.transmissionCoefficient)
XCTAssertEqual(viewModel.transmissionLoss, expectedTransmissionLoss)
// Check reflection loss calculation
let expectedReflectionLoss = -10 * log10(viewModel.transmissionCoefficientPower)
XCTAssertEqual(viewModel.reflectionLoss, expectedReflectionLoss)

// Set transmission loss and check transmission coefficient changes
let newTransmissionLoss = 3.0
viewModel.transmissionLoss = newTransmissionLoss
let expectedTransmissionCoefficientValue = pow(10, -newTransmissionLoss / 20)
XCTAssertEqual(viewModel.transmissionCoefficient, expectedTransmissionCoefficientValue, accuracy: 1e-6)
// Set reflection loss and check transmission coefficient changes
let newReflectionLoss = 3.0
viewModel.reflectionLoss = newReflectionLoss
let expectedTransmissionCoefficientPower = pow(10, -newReflectionLoss / 10)
XCTAssertEqual(viewModel.transmissionCoefficientPower, expectedTransmissionCoefficientPower, accuracy: 1e-6)
}
}

Expand Down

0 comments on commit e4c705b

Please # to comment.