From 9832b8702e0f3df0d5d80f269ed389ed508e72b1 Mon Sep 17 00:00:00 2001 From: Mike Fikes Date: Sun, 14 Jan 2024 17:04:17 -0500 Subject: [PATCH 1/2] Incorrect definition of transmission coefficient --- .../CharacterizationView.swift | 12 ++-- .../Impedance Converter/Units.swift | 6 +- .../Impedance Converter/ViewModel.swift | 45 +++++++------- .../ViewModelTests.swift | 60 +++++-------------- 4 files changed, 48 insertions(+), 75 deletions(-) diff --git a/Impedance Converter/Impedance Converter/CharacterizationView.swift b/Impedance Converter/Impedance Converter/CharacterizationView.swift index 09bcbcf..07d3e52 100644 --- a/Impedance Converter/Impedance Converter/CharacterizationView.swift +++ b/Impedance Converter/Impedance Converter/CharacterizationView.swift @@ -28,7 +28,7 @@ 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") } } @@ -36,7 +36,7 @@ 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.") } } @@ -44,7 +44,7 @@ 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") } } @@ -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") } } @@ -93,7 +93,7 @@ struct CharacterizationView: View { DisplayView { HStack { ReturnLossView(viewModel: viewModel) - TransmissionLossView(viewModel: viewModel) + ReflectionLossView(viewModel: viewModel) } } } diff --git a/Impedance Converter/Impedance Converter/Units.swift b/Impedance Converter/Impedance Converter/Units.swift index e8b62bd..16b3a7b 100644 --- a/Impedance Converter/Impedance Converter/Units.swift +++ b/Impedance Converter/Impedance Converter/Units.swift @@ -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 } diff --git a/Impedance Converter/Impedance Converter/ViewModel.swift b/Impedance Converter/Impedance Converter/ViewModel.swift index fd77bd4..958da1e 100644 --- a/Impedance Converter/Impedance Converter/ViewModel.swift +++ b/Impedance Converter/Impedance Converter/ViewModel.swift @@ -460,7 +460,11 @@ class ViewModel: ObservableObject, Codable { var reflectionCoefficientPower: Double { get { - return reflectionCoefficient.lengthSquared + if (unityReflectionCoefficient()) { + return 1 + } else { + return reflectionCoefficient.lengthSquared + } } set { guard !reflectionCoefficient.phase.isNaN else { return } @@ -483,44 +487,41 @@ class ViewModel: ObservableObject, Codable { } } - var transmissionCoefficient: Double { + var transmissionCoefficient: Complex { 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) } } diff --git a/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift b/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift index 9104a56..73c3183 100644 --- a/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift +++ b/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift @@ -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.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.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.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.choose((0, 1)) ) { tau in + self.viewModel.transmissionCoefficientPower = tau + return abs(self.viewModel.reflectionCoefficientPower + tau - 1) < 1e-6 } } @@ -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) } } From 0ab6f35cc5382be9ff44292cf161a5b5d1ce3aca Mon Sep 17 00:00:00 2001 From: Mike Fikes Date: Sun, 14 Jan 2024 21:46:14 -0500 Subject: [PATCH 2/2] Improve tests --- .../Impedance ConverterTests/ViewModelTests.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift b/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift index 73c3183..0852542 100644 --- a/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift +++ b/Impedance Converter/Impedance ConverterTests/ViewModelTests.swift @@ -590,16 +590,18 @@ class TransmissionParametersTests: ViewModelTestBase { } func testReflectionCoefficientTransmissionCoefficient() { - viewModel.reactance = 40 property("Reflection coefficient power plus transmission coefficient power must sum to 1") <- forAll( Gen.choose((0, 1)) ) { rho in + self.viewModel.reactance = 3 + self.viewModel.reactance = 4 self.viewModel.reflectionCoefficientPower = rho return abs(rho + self.viewModel.transmissionCoefficientPower - 1) < 1e-6 } } func testTransmissionCoefficientReflectionCoefficient() { - viewModel.reactance = 40 property("Reflection coefficient power plus transmission coefficient power must sum to 1") <- forAll( Gen.choose((0, 1)) ) { tau in + self.viewModel.reactance = 3 + self.viewModel.reactance = 4 self.viewModel.transmissionCoefficientPower = tau return abs(self.viewModel.reflectionCoefficientPower + tau - 1) < 1e-6 }