Materialien/plot Frequenzabhängige Kapazität.py

174 lines
6.7 KiB
Python

import numpy as np
import matplotlib.pyplot as plt
import math
from matplotlib.ticker import ScalarFormatter
def parse_file(filename):
# Listen für die Daten
frequencies = []
capacitances = []
resistances = []
# Datei Zeile für Zeile lesen
with open(filename, 'r') as file:
for line in file:
if line.strip(): # Leere Zeilen überspringen
parts = line.split()
if parts[7] == "INVALID":
continue
# if float(parts[4]) < 0:
# continue
# Frequenz und Konvertierung in Hz
freq_value = float(parts[1])
freq_unit = parts[2]
if freq_unit == "kHz":
freq_value *= 1e3
elif freq_unit == "MHz":
freq_value *= 1e6
elif freq_unit == "Hz":
freq_value *= 1
else:
print("Fehler freq")
frequencies.append(freq_value)
# Kapazität und Konvertierung in Farad
cap_value = float(parts[4])
cap_unit = parts[5]
if cap_unit == "pF":
cap_value *= 1e-12
elif cap_unit == "nF":
cap_value *= 1e-9
elif cap_unit == "uF":
cap_value *= 1e-6
# elif cap_unit == "F":
# cap_unit *= 1
else:
print("Fehler freq")
capacitances.append(cap_value) # Pikofarad zu Farad
# Widerstand und Konvertierung in Ohm
res_value = float(parts[7])
res_unit = parts[8]
if res_unit == "kOHMS":
res_value *= 1e3
elif res_unit == "MOHMS":
res_value *= 1e6
elif res_unit == "mOHMS":
res_value *= 1e-3
elif res_unit == "OHMS":
res_unit *= 1
else:
print("Fehler res", res_unit)
resistances.append(res_value)
# Umwandlung in numpy-Arrays
frequencies = np.array(frequencies)
capacitances = np.array(capacitances)
resistances = np.array(resistances)
return frequencies, capacitances, resistances
def plot_data(frequencies, y_values):
plt.figure(figsize=(10, 6))
plt.plot(frequencies, y_values, label="Y-Werte (Platzhalter)", marker="o")
plt.xscale("log") # Logarithmische Skalierung der x-Achse
plt.xlabel("Frequenz (Hz)")
plt.ylabel("Y-Werte")
plt.title("Logarithmischer Plot der Frequenz")
plt.grid(which="both", linestyle="--", linewidth=0.5)
plt.legend()
plt.show()
def plot_Kapazität(capacitances_1, capacitances_2, capacitances_3, frequencies_1, frequencies_2, frequencies_3,
filename):
# Plot
plt.figure(figsize=(10, 7.5))
# Mehrere Linien plotten
# plt.plot(frequencies_1, capacitances_1, label='Keramikkondensator 1 nF', linestyle='-', color='blue')
plt.plot(frequencies_2, capacitances_2, label='Folienkondensator 100 µF', linestyle='-', color='green')
plt.plot(frequencies_3, capacitances_3, label='Elektrolytkondensator 100 µF)', linestyle='-', color='red')
# Logarithmische Achsen
plt.xscale('log') # Logarithmische Skalierung der Frequenz
# plt.yscale('log') # Optional: Logarithmische Skalierung der Kapazität
# Achsentitel und Beschriftungen
plt.xlabel('f / Hz')
plt.ylabel('C / F')
# plt.title('Frequenzabhängige Kapazitäten')
formatter = ScalarFormatter()
formatter.set_powerlimits((-3, 3)) # Format wird bei Werten außerhalb von 10^-3 und 10^3 angewendet
plt.gca().yaxis.set_major_formatter(formatter) # Wendet den Formatierer auf die y-Achse an
# Gitter und Legende
plt.grid()
# plt.grid(which='both', linestyle='--', linewidth=0.5)
plt.legend()
plt.tight_layout()
# Anzeige des Plots
# plt.show()
plt.savefig(filename, dpi=600)
def plot_Impedanz(impedanz_betrag_1, impedanz_betrag_2, impedanz_betrag_3, frequencies_1, frequencies_2, frequencies_3, filename):
# Plot
plt.figure(figsize=(10, 6))
# Mehrere Linien plotten
# plt.plot(frequencies_1, impedanz_betrag_1, label='Keramikkondensator 1 nF', linestyle='-', color='blue')
plt.plot(frequencies_2, impedanz_betrag_2, label='Folien 100 µF', linestyle='-', color='green')
plt.plot(frequencies_3, impedanz_betrag_3, label='Elektrolyt 100 µF)', linestyle='-', color='red')
# Logarithmische Achsen
plt.xscale('log') # Logarithmische Skalierung der Frequenz
plt.yscale('log') # Optional: Logarithmische Skalierung der Kapazität
# Achsentitel und Beschriftungen
plt.xlabel('Frequenz f [Hz]')
plt.ylabel('Impedanz |Z| [Ohm]')
# plt.title('Frequenzabhängige Kapazitäten')
formatter = ScalarFormatter()
formatter.set_powerlimits((-3, 3)) # Format wird bei Werten außerhalb von 10^-3 und 10^3 angewendet
plt.gca().yaxis.set_major_formatter(formatter) # Wendet den Formatierer auf die y-Achse an
# Gitter und Legende
plt.grid()
# plt.grid(which='both', linestyle='--', linewidth=0.5)
plt.legend()
plt.tight_layout()
# Anzeige des Plots
# plt.show()
plt.savefig(filename, dpi=600)
print("was mache ich mit negativen Kapazitäten?")
# Beispiel: Datei einlesen und plotten
filename_1 = "1nF.txt" # Ersetzen durch den tatsächlichen Dateinamen
filename_2 = "100muF Folien vermutlich.txt" # Ersetzen durch den tatsächlichen Dateinamen
filename_3 = "100muF Elektrolyt vermutlich.txt" # Ersetzen durch den tatsächlichen Dateinamen
frequencies_1, capacitances_1, resistances_1 = parse_file(filename_1)
frequencies_2, capacitances_2, resistances_2 = parse_file(filename_2)
frequencies_3, capacitances_3, resistances_3 = parse_file(filename_3)
# Platzhalter für Y-Werte
impedanz_betrag_1 = resistances_1 / ((2 * math.pi * frequencies_1 ** 2 * resistances_1 ** 2 * capacitances_1 ** 2 + 1) ** 0.5)
impedanz_betrag_2 = resistances_2 / ((2 * math.pi * frequencies_2 ** 2 * resistances_2 ** 2 * capacitances_2 ** 2 + 1) ** 0.5)
impedanz_betrag_3 = resistances_3 / ((2 * math.pi * frequencies_3 ** 2 * resistances_3 ** 2 * capacitances_3 ** 2 + 1) ** 0.5)
# Daten plotten
# plot_Kapazität(capacitances_1, capacitances_2, capacitances_3, frequencies_1, frequencies_2, frequencies_3, "Frequenzabhängige_Kapazität_100muF.png")
# plot_Kapazität(capacitances_1, capacitances_2, capacitances_3, frequencies_1, frequencies_2, frequencies_3, "Frequenzabhängige_Kapazität_1nF.png")
plot_Impedanz(impedanz_betrag_1, impedanz_betrag_2, impedanz_betrag_3, frequencies_1, frequencies_2, frequencies_3, "Frequenzabhängige_Kapazität_Betrag Z.png")