BH plot
This commit is contained in:
parent
35b9398c52
commit
a67b700699
134
main.py
134
main.py
@ -1,16 +1,20 @@
|
||||
from PIL.ImageChops import offset
|
||||
from siglent_sdg.siglent import SiglentGen
|
||||
from asyncio import sleep
|
||||
from rigol_dho_lib.rigol import RigolOsc, CHANNEL_COUNT
|
||||
import time
|
||||
from datetime import datetime
|
||||
from rigol_dho_lib import rigol
|
||||
from siglent_sdg import siglent
|
||||
import numpy as np
|
||||
import csv
|
||||
from scipy.integrate import cumulative_trapezoid
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
SINE_FREQ = 100e3
|
||||
|
||||
CYCLE_COUNT = 1000
|
||||
CYCLE_COUNT = 100
|
||||
|
||||
DIV_COUNT = 10
|
||||
|
||||
@ -19,10 +23,17 @@ OSC_CHANNEL_A = 1 # GEN signal
|
||||
OSC_CHANNEL_B = 2 # OUT
|
||||
GEN_CHANNEL = siglent.ChannelID.CH1
|
||||
|
||||
UPPER_BOUND_DIV = 7
|
||||
LOWER_BOUND_DIV = 6
|
||||
UPPER_BOUND_DIV = 8
|
||||
LOWER_BOUND_DIV = 3
|
||||
TARGET_DIV = (UPPER_BOUND_DIV + LOWER_BOUND_DIV) / 2
|
||||
|
||||
PATH = "/home/zychlix/Desktop/pomiary/out"
|
||||
|
||||
R0 = 99.4
|
||||
|
||||
AMPLITUDE = 3
|
||||
COUNT = 200
|
||||
|
||||
|
||||
class ImpedanceAnalyzer:
|
||||
def __init__(self, gen_addr: str, osc_addr: str):
|
||||
@ -32,9 +43,12 @@ class ImpedanceAnalyzer:
|
||||
self.mem_depth = self.osc.getMemoryDepth()
|
||||
self.osc.setPoints(self.mem_depth)
|
||||
|
||||
self.amplitude = 10
|
||||
self.amplitude = AMPLITUDE
|
||||
self.debug_voltages = []
|
||||
|
||||
self.span_a = 0
|
||||
self.span_b = 0
|
||||
|
||||
self.dc = 0
|
||||
|
||||
self.scales = [10] * (CHANNEL_COUNT + 1)
|
||||
@ -51,7 +65,7 @@ class ImpedanceAnalyzer:
|
||||
|
||||
self.osc.single()
|
||||
|
||||
time.sleep(0.1)
|
||||
time.sleep(0.3)
|
||||
|
||||
channel_A_data = self.getScaledWaveform(OSC_CHANNEL_A)
|
||||
channel_B_data = self.getScaledWaveform(OSC_CHANNEL_B)
|
||||
@ -63,8 +77,8 @@ class ImpedanceAnalyzer:
|
||||
|
||||
print(f"{freq} V_a {v_a} V_o {v_o}")
|
||||
|
||||
R0 = 10
|
||||
z = R0 * (v_a - v_o) / v_o
|
||||
# z = v_a / (v_o / R0)
|
||||
|
||||
self.debug_voltages.append([np.abs(v_a), np.abs(v_o)])
|
||||
|
||||
@ -74,8 +88,8 @@ class ImpedanceAnalyzer:
|
||||
data = self.osc.getChannel(ch).getWaveform()
|
||||
vpp = self.calculateVRMS(data) * 2 * np.sqrt(2)
|
||||
if not (LOWER_BOUND_DIV < vpp / self.scales[ch] < UPPER_BOUND_DIV):
|
||||
self.autoscale(ch)
|
||||
time.sleep(1)
|
||||
# self.autoscale(ch)
|
||||
time.sleep(2) # change back to 1
|
||||
self.osc.single()
|
||||
data = self.osc.getChannel(ch).getWaveform()
|
||||
return data
|
||||
@ -84,6 +98,7 @@ class ImpedanceAnalyzer:
|
||||
f = np.logspace(start, stop, samples, endpoint=True, base=10.0)
|
||||
z_array = []
|
||||
for i in f:
|
||||
# print(f"Frequency: {f}")
|
||||
z_array.append(self.getImpedance(i))
|
||||
print(z_array)
|
||||
return z_array, f
|
||||
@ -95,17 +110,21 @@ class ImpedanceAnalyzer:
|
||||
|
||||
vpp = 0
|
||||
|
||||
self.osc.run()
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
while True:
|
||||
rms = self.osc.getChannel(channel).getVrms()[0]
|
||||
# rms = self.osc.getChannel(channel).getVrms()[0]
|
||||
self.osc.single()
|
||||
|
||||
data = self.osc.getChannel(channel).getWaveform()
|
||||
time.sleep(1)
|
||||
rms = self.calculateVRMS(data)
|
||||
vpp = rms * 2 * np.sqrt(2)
|
||||
|
||||
if LOWER_BOUND_DIV < vpp / self.scales[channel] < UPPER_BOUND_DIV:
|
||||
break
|
||||
elif vpp / self.scales[channel] > UPPER_BOUND_DIV:
|
||||
elif (
|
||||
vpp / self.scales[channel] > UPPER_BOUND_DIV
|
||||
or self.osc.getChannel(channel).getVrms()[0] > 1000
|
||||
):
|
||||
self.scales[channel] = self.osc.getChannel(channel).clampVscale(
|
||||
self.scales[channel] * 2
|
||||
)
|
||||
@ -117,53 +136,100 @@ class ImpedanceAnalyzer:
|
||||
print(
|
||||
f"Autoscaled channel: {channel}, RMS: {rms}, Vpp:{vpp}, Scale:{self.scales[channel]}"
|
||||
)
|
||||
time.sleep(1)
|
||||
|
||||
return
|
||||
|
||||
def PlotBH(self, freq, amplitude):
|
||||
setWindowSize(self.osc, 1, freq)
|
||||
|
||||
self.gen.channels[GEN_CHANNEL].apply_sine(freq, amplitude, self.dc)
|
||||
self.gen.channels[GEN_CHANNEL].set_output(True)
|
||||
|
||||
self.osc.run()
|
||||
time.sleep(5)
|
||||
|
||||
self.osc.single()
|
||||
|
||||
time.sleep(0.3)
|
||||
|
||||
voltage_data = self.getScaledWaveform(OSC_CHANNEL_A)
|
||||
current_data = self.getScaledWaveform(OSC_CHANNEL_B)
|
||||
|
||||
time_array = self.osc.getChannel(OSC_CHANNEL_A).genTimeArray(voltage_data)
|
||||
|
||||
v_l = voltage_data # Voltage induced in the inductor
|
||||
|
||||
Offset = np.mean(v_l)
|
||||
|
||||
v_l = v_l - Offset
|
||||
|
||||
B = (
|
||||
cumulative_trapezoid(v_l, time_array, initial=0) / 1
|
||||
) # Correct to proper values
|
||||
H = current_data / R0 # As well as in here
|
||||
|
||||
plt.plot(H, B)
|
||||
|
||||
plt.show()
|
||||
|
||||
def exportZtoCSV(self, z, filename: str):
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
np.savetxt(filename + timestamp + ".csv", z, delimiter=",", fmt="%s")
|
||||
|
||||
|
||||
def setWindowSize(osc, cycles, frequency):
|
||||
period = 1 / frequency * cycles / DIV_COUNT
|
||||
osc.setTimescale(period)
|
||||
|
||||
|
||||
def dft(data, time_array, freq):
|
||||
carrier = np.exp(2j * np.pi * freq * time_array)
|
||||
x = carrier * data
|
||||
print(carrier)
|
||||
# def dft(data, time_array, freq):
|
||||
# carrier = np.exp(-2j * np.pi * freq * time_array)
|
||||
# x = carrier * data
|
||||
# print(carrier)
|
||||
|
||||
return sum(x) / len(carrier) * 2
|
||||
# return sum(x) / len(carrier) * 2
|
||||
|
||||
|
||||
def dft(data, time_array, freq):
|
||||
sin_ref = np.sin(2 * np.pi * freq * time_array)
|
||||
cos_ref = np.cos(2 * np.pi * freq * time_array)
|
||||
|
||||
i = 2 * np.mean(data * cos_ref)
|
||||
q = 2 * np.mean(data * sin_ref)
|
||||
|
||||
return i - 1j * q
|
||||
|
||||
|
||||
# uv
|
||||
def main():
|
||||
|
||||
# time.sleep(0.2)
|
||||
# imp = ImpedanceAnalyzer("TCPIP::10.112.1.2::INSTR", "TCPIP::10.112.1.3::INSTR")
|
||||
|
||||
# plt.plot(time_array, channel_A_data)
|
||||
# plt.plot(time_array, channel_B_data)
|
||||
|
||||
# plt.show()
|
||||
|
||||
# plt.plot(time_array, np.real(x))
|
||||
# plt.plot(time_array, np.imag(x))
|
||||
|
||||
imp = ImpedanceAnalyzer("TCPIP::10.112.1.15::INSTR", "TCPIP::10.112.1.9::INSTR")
|
||||
imp = ImpedanceAnalyzer("TCPIP::192.168.1.4::INSTR", "TCPIP::192.168.1.5::INSTR")
|
||||
|
||||
imp.gen.channels[GEN_CHANNEL.CH1].set_output(True)
|
||||
|
||||
z, f = imp.getSweep(3, 7, 20)
|
||||
# imp.autoscale(1)
|
||||
|
||||
# imp.autoscale(2)
|
||||
|
||||
imp.PlotBH(3e3, AMPLITUDE)
|
||||
|
||||
return 0
|
||||
z, f = imp.getSweep(2, 7, COUNT)
|
||||
|
||||
z_real = np.real(z)
|
||||
z_imag = np.imag(z)
|
||||
fig, ax = plt.subplots(3)
|
||||
ax[0].plot(f, z_real, label="Real")
|
||||
ax[0].plot(f, z_imag, label="Imag")
|
||||
ax[0].set_yscale("log")
|
||||
ax[0].set_yscale("linear")
|
||||
ax[1].plot(f, np.abs(z), label="Magnitude")
|
||||
ax[1].set_yscale("log")
|
||||
ax_phase = ax[1].twinx()
|
||||
ax_phase.plot(f, np.angle(z), label="Phase", color="orange", linestyle="--")
|
||||
ax_phase.plot(
|
||||
f, np.angle(z) / np.pi * 180, label="Phase", color="orange", linestyle="--"
|
||||
)
|
||||
ax_phase.legend()
|
||||
ax[0].set_xscale("log")
|
||||
ax[1].set_xscale("log")
|
||||
@ -174,6 +240,8 @@ def main():
|
||||
ax[0].legend()
|
||||
ax[1].legend()
|
||||
|
||||
imp.exportZtoCSV(np.array([f, z]).T, PATH)
|
||||
|
||||
plt.show()
|
||||
|
||||
return
|
||||
|
||||
Loading…
Reference in New Issue
Block a user