Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Incorrect definition of transmission coefficient #158

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 23 additions & 22 deletions Impedance Converter/Impedance Converter/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -483,44 +487,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
66 changes: 20 additions & 46 deletions Impedance Converter/Impedance ConverterTests/ViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -585,28 +585,25 @@ 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.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 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.reactance = 3
self.viewModel.reactance = 4
self.viewModel.transmissionCoefficientPower = tau
return abs(self.viewModel.reflectionCoefficientPower + tau - 1) < 1e-6
}
}

Expand All @@ -632,44 +629,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