Realtime plotting

I was wondering how InspectDR would handle this myself… so I made a small test:

using InspectDR
using Colors

function getmeasdata(t, φ)
	sigA = sin.(t .+ φ)
	sigB = cos.(t .+ φ)
	return (sigA, sigB)
end

#Build general structure of animation plot
function buildanimplot()
	NPOINTS = 1000
	NCYCLES = 2 #To display
	RED = RGB24(1, 0, 0)
	GREEN = RGB24(0, 1, 0)
	BLUE = RGB24(0, 0, 1)

	#collect: InspectDR does not take AbstractArray:
	t = collect(range(0, stop=NCYCLES*2pi, length=NPOINTS))
	(sigA, sigB) = getmeasdata(t, 0)

	#Using Plot2D simplified "template" constructor:
	p = InspectDR.Plot2D(:lin, [:lin, :lin],
		title = "Measured Data", xlabel = "time (s)",
		ylabels = ["Amplitude (V)", "Amplitude (V)"]
	)
	p.layout[:enable_legend] = true

	wfrmA = add(p, t, sigA, id="Signal A", strip=1)
		wfrmA.line = line(color=RED, width=2)
	wfrmB = add(p, t, sigB, id="Signal B", strip=2)
		wfrmB.line = line(color=BLUE, width=2)

	gplot = display(InspectDR.GtkDisplay(), p)
	return (gplot, t, wfrmA, wfrmB)
end

#Update animated plot in "real time":
function testanimplot()
	DURATION = 5 #sec
	NSTEPS = 1000
	RADPERSEC = 2pi
	tstep = DURATION/NSTEPS
	ϕstep = RADPERSEC * tstep

	(gplot, t, wfrmA, wfrmB) = buildanimplot()

@time begin #Curious to see how long it actually takes
	for ϕ in range(0, step=ϕstep, length=NSTEPS)
		sleep(DURATION/NSTEPS)
		(sigA, sigB) = getmeasdata(t, ϕ)
		wfrmA.ds.y = sigA
		wfrmB.ds.y = sigB
		InspectDR.refresh(gplot)
	end
end

	return gplot
end

gplot = testanimplot()

This might make it easier to see if InspectDR is adequate for your task…

2 Likes