kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
				
				
				
			Linting and refactoring
							rodzic
							
								
									cb3122d632
								
							
						
					
					
						commit
						1c8477f1a9
					
				|  | @ -185,6 +185,43 @@ class Calibration: | ||||||
|     def isValid2Port(self) -> bool: |     def isValid2Port(self) -> bool: | ||||||
|         return self.dataset.complete2port() |         return self.dataset.complete2port() | ||||||
| 
 | 
 | ||||||
|  |     def _calc_port_1(self, freq: int, cal: CalData): | ||||||
|  |         g1 = self.gamma_short(freq) | ||||||
|  |         g2 = self.gamma_open(freq) | ||||||
|  |         g3 = self.gamma_load(freq) | ||||||
|  | 
 | ||||||
|  |         gm1 = cal["short"].z | ||||||
|  |         gm2 = cal["open"].z | ||||||
|  |         gm3 = cal["load"].z | ||||||
|  | 
 | ||||||
|  |         denominator = (g1 * (g2 - g3) * gm1 + | ||||||
|  |                        g2 * g3 * gm2 - g2 * g3 * gm3 - | ||||||
|  |                        (g2 * gm2 - g3 * gm3) * g1) | ||||||
|  |         cal["e00"] = - ((g2 * gm3 - g3 * gm3) * g1 * gm2 - | ||||||
|  |                           (g2 * g3 * gm2 - g2 * g3 * gm3 - | ||||||
|  |                            (g3 * gm2 - g2 * gm3) * g1) * gm1 | ||||||
|  |                           ) / denominator | ||||||
|  |         cal["e11"] = ((g2 - g3) * gm1 - g1 * (gm2 - gm3) + | ||||||
|  |                         g3 * gm2 - g2 * gm3) / denominator | ||||||
|  |         cal["delta_e"] = - ((g1 * (gm2 - gm3) - g2 * gm2 + g3 * | ||||||
|  |                                gm3) * gm1 + (g2 * gm3 - g3 * gm3) * | ||||||
|  |                               gm2) / denominator | ||||||
|  | 
 | ||||||
|  |     def _calc_port_2(self, freq: int, cal: CalData): | ||||||
|  |         gt = self.gamma_through(freq) | ||||||
|  | 
 | ||||||
|  |         gm4 = cal["through"].z | ||||||
|  |         gm5 = cal["thrurefl"].z | ||||||
|  |         gm6 = cal["isolation"].z | ||||||
|  |         gm7 = gm5 - cal["e00"] | ||||||
|  | 
 | ||||||
|  |         cal["e30"] = cal["isolation"].z | ||||||
|  |         cal["e10e01"] = cal["e00"] * cal["e11"] - cal["delta_e"] | ||||||
|  |         cal["e22"] = gm7 / ( | ||||||
|  |             gm7 * cal["e11"] * gt**2 + cal["e10e01"] * gt**2) | ||||||
|  |         cal["e10e32"] = (gm4 - gm6) * ( | ||||||
|  |             1 - cal["e11"] * cal["e22"] *gt**2) / gt | ||||||
|  | 
 | ||||||
|     def calc_corrections(self): |     def calc_corrections(self): | ||||||
|         if not self.isValid1Port(): |         if not self.isValid1Port(): | ||||||
|             logger.warning( |             logger.warning( | ||||||
|  | @ -195,27 +232,10 @@ class Calibration: | ||||||
|         logger.debug("Calculating calibration for %d points.", self.size()) |         logger.debug("Calculating calibration for %d points.", self.size()) | ||||||
| 
 | 
 | ||||||
|         for freq, caldata in self.dataset.items(): |         for freq, caldata in self.dataset.items(): | ||||||
|             g1 = self.gamma_short(freq) |  | ||||||
|             g2 = self.gamma_open(freq) |  | ||||||
|             g3 = self.gamma_load(freq) |  | ||||||
| 
 |  | ||||||
|             gm1 = caldata["short"].z |  | ||||||
|             gm2 = caldata["open"].z |  | ||||||
|             gm3 = caldata["load"].z |  | ||||||
| 
 |  | ||||||
|             try: |             try: | ||||||
|                 denominator = (g1 * (g2 - g3) * gm1 + |                 self._calc_port_1(freq, caldata) | ||||||
|                                g2 * g3 * gm2 - g2 * g3 * gm3 - |                 if self.isValid2Port(): | ||||||
|                                (g2 * gm2 - g3 * gm3) * g1) |                     self._calc_port_2(freq, caldata) | ||||||
|                 caldata["e00"] = - ((g2 * gm3 - g3 * gm3) * g1 * gm2 - |  | ||||||
|                                     (g2 * g3 * gm2 - g2 * g3 * gm3 - |  | ||||||
|                                      (g3 * gm2 - g2 * gm3) * g1) * gm1 |  | ||||||
|                                     ) / denominator |  | ||||||
|                 caldata["e11"] = ((g2 - g3) * gm1 - g1 * (gm2 - gm3) + |  | ||||||
|                                   g3 * gm2 - g2 * gm3) / denominator |  | ||||||
|                 caldata["delta_e"] = - ((g1 * (gm2 - gm3) - g2 * gm2 + g3 * |  | ||||||
|                                          gm3) * gm1 + (g2 * gm3 - g3 * gm3) * |  | ||||||
|                                         gm2) / denominator |  | ||||||
|             except ZeroDivisionError as exc: |             except ZeroDivisionError as exc: | ||||||
|                 self.isCalculated = False |                 self.isCalculated = False | ||||||
|                 logger.error( |                 logger.error( | ||||||
|  | @ -225,21 +245,6 @@ class Calibration: | ||||||
|                     f"Two of short, open and load returned the same" |                     f"Two of short, open and load returned the same" | ||||||
|                     f" values at frequency {freq}Hz.") from exc |                     f" values at frequency {freq}Hz.") from exc | ||||||
| 
 | 
 | ||||||
|             if self.isValid2Port(): |  | ||||||
|                 caldata["e30"] = caldata["isolation"].z |  | ||||||
|                 gt = self.gamma_through(freq) |  | ||||||
|                 gm4 = caldata["through"].z |  | ||||||
|                 gm5 = caldata["thrurefl"].z |  | ||||||
|                 gm6 = caldata["isolation"].z |  | ||||||
| 
 |  | ||||||
|                 caldata["e10e01"] = caldata["e00"] * caldata["e11"] - caldata["delta_e"] |  | ||||||
|                 gm7 = gm5 - caldata["e00"] |  | ||||||
|                 caldata["e22"] = gm7 /(gm7*caldata["e11"]*gt**2+caldata["e10e01"]*gt**2) |  | ||||||
|                 caldata["e10e32"] = (gm4-gm6)*(1-caldata["e11"]*caldata["e22"]*gt**2)/gt |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|         self.gen_interpolation() |         self.gen_interpolation() | ||||||
|         self.isCalculated = True |         self.isCalculated = True | ||||||
|         logger.debug("Calibration correctly calculated.") |         logger.debug("Calibration correctly calculated.") | ||||||
|  | @ -344,28 +349,12 @@ class Calibration: | ||||||
|         s21 = s21 * (i["e10e01"](dp.freq)/(i["e11"](dp.freq)*dp11.z-i["delta_e"](dp.freq))) |         s21 = s21 * (i["e10e01"](dp.freq)/(i["e11"](dp.freq)*dp11.z-i["delta_e"](dp.freq))) | ||||||
|         return Datapoint(dp.freq, s21.real, s21.imag) |         return Datapoint(dp.freq, s21.real, s21.imag) | ||||||
| 
 | 
 | ||||||
|     def dump_correct(self, dp: Datapoint, dp11: Datapoint): |  | ||||||
|         i = self.interp |  | ||||||
|         if dp.freq == 2520000000 or dp.freq == 2600000000: |  | ||||||
|             print("freq: ", dp.freq) |  | ||||||
|             print("z11: ", dp11.z) |  | ||||||
|             print("z21: ", dp.z) |  | ||||||
|             print("e00: ", i["e00"](dp.freq)) |  | ||||||
|             print("e11: ", i["e11"](dp.freq)) |  | ||||||
|             print("delta_e: ", i["delta_e"](dp.freq)) |  | ||||||
|             print("e10e01: ", i["e10e01"](dp.freq)) |  | ||||||
|             print("e30: ", i["e30"](dp.freq)) |  | ||||||
|             print("e22: ", i["e22"](dp.freq)) |  | ||||||
|             print("e10e32: ", i["e10e32"](dp.freq)) |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     # TODO: implement tests |     # TODO: implement tests | ||||||
|     def save(self, filename: str): |     def save(self, filename: str): | ||||||
|         # Save the calibration data to file |         # Save the calibration data to file | ||||||
|         if not self.isValid1Port(): |         if not self.isValid1Port(): | ||||||
|             raise ValueError("Not a valid 1-Port calibration") |             raise ValueError("Not a valid 1-Port calibration") | ||||||
|         with open(f"{filename}", "w") as calfile: |         with open(filename, mode="w", encoding='utf-8') as calfile: | ||||||
|             calfile.write("# Calibration data for NanoVNA-Saver\n") |             calfile.write("# Calibration data for NanoVNA-Saver\n") | ||||||
|             for note in self.notes: |             for note in self.notes: | ||||||
|                 calfile.write(f"! {note}\n") |                 calfile.write(f"! {note}\n") | ||||||
|  | @ -383,7 +372,7 @@ class Calibration: | ||||||
|         self.notes = [] |         self.notes = [] | ||||||
| 
 | 
 | ||||||
|         parsed_header = False |         parsed_header = False | ||||||
|         with open(filename) as calfile: |         with open(filename, encoding='utf-8') as calfile: | ||||||
|             for i, line in enumerate(calfile): |             for i, line in enumerate(calfile): | ||||||
|                 line = line.strip() |                 line = line.strip() | ||||||
|                 if line.startswith("!"): |                 if line.startswith("!"): | ||||||
|  |  | ||||||
		Ładowanie…
	
		Reference in New Issue
	
	 Holger Müller
						Holger Müller