Skip to content

Instantly share code, notes, and snippets.

@jonathanschilling
Created November 16, 2022 21:47
Show Gist options
  • Save jonathanschilling/c87108ae03df58f77f64e5bc50854e37 to your computer and use it in GitHub Desktop.
Save jonathanschilling/c87108ae03df58f77f64e5bc50854e37 to your computer and use it in GitHub Desktop.
Macintosh SE screen capture from VSync, HSync and Video measurements (LeCroy 7200A)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
import h5py
folder = "/data/jonathan/Projekte/Elektrotechnik/Computerbasteln/Macintosh/MacSE_screencap/screencap/"
#folder = "C:/Users/Jonathan/data/Projekte/Elektrotechnik/Computerbasteln/Macintosh/Macintosh SE/screencap/"
# read HDF5 file
# timebase are equal, since LeCroy 7200A plugins were synced during acquisition
f = h5py.File(folder+"MacSE.h5", "r")
VSync = f["VSync_sig"][()]
#VSync_time = f["VSync_time"][()]
t = f["VSync_time"][()]
HSync = f["HSync_sig"][()]
#HSync_time = f["HSync_time"][()]
Video = f["Video_sig"][()]
#Video_time = f["Video_time"][()]
f.close()
# for now, trigger only on falling edge
def trigger(time, signal, threshold = 1.0, holdoffIdx = 0):
n = len(time)
for i in range(holdoffIdx, n-1):
if signal[i] > threshold and signal[i+1] <= threshold:
#tTrig = time[i]
# compute accurate intersection of connecting line with trigger level line
dt = time[i+1] - time[i]
dy = signal[i+1] - signal[i]
tTrig = time[i] + (threshold - signal[i]) * dt / dy
return tTrig, i
return np.nan, -1
# look for first falling edge in t vector
# and set this as zero of timebase
tTrig, iTrig = trigger(t, VSync)
print("triggered on VSync falling edge at index %d, time %g"%(iTrig, tTrig))
# find next frame
tNextFrame, iNextFrame = trigger(t, VSync, holdoffIdx = iTrig+1)
print("triggered on VSync falling edge at index %d, time %g"%(iNextFrame, tNextFrame))
# find all HSync falling edges in this frame and count how many lines are in this frame
nLines = 0
lineStartIndices = []
lineStartTimes = []
holdoffIdx = iTrig+1
while holdoffIdx < iNextFrame:
tNextLine, iNextLine = trigger(t, HSync, holdoffIdx = holdoffIdx)
if iNextLine < 0 or iNextLine > iNextFrame:
break
print("found line %d at t=%g"%(nLines, tNextLine,))
lineStartTimes.append(tNextLine)
lineStartIndices.append(iNextLine)
nLines += 1
holdoffIdx = iNextLine+1
print("found %d lines"%(nLines,))
##f, ax=plt.subplots(3, sharex=True)
##
##ax[0].plot(t, VSync)
##ax[0].axvline(tTrig)
##ax[0].axvline(tNextFrame)
##ax[0].set_ylabel("VSync")
##ax[0].grid(True)
##
##ax[1].plot(t, HSync)
##for l in lineStartTimes:
## ax[1].axvline(l)
##ax[1].set_ylabel("HSync")
##ax[1].grid(True)
##
##ax[2].plot(t, Video)
##ax[2].set_ylabel("Video")
##ax[2].grid(True)
##
##ax[-1].set_xlabel("t / s")
##ax[-1].ticklabel_format(axis='x', scilimits=(-2,2))
##
##ax[0].set_title("Macintosh SE Video Capture")
# 27 to skip in the beginning
# 1 to skip in the end
# --> 370 - 27 - 1 = 342 actual lines
actLines = 342
# extract individual lines and put them on relative timebases
lineTime = []
lineSigs = []
for i in range(27, 27+actLines):
print("work on line %d/%d"%(i-27, actLines))
idxStart = lineStartIndices[i]
idxEnd = lineStartIndices[i+1]
lineTime.append(t[idxStart:idxEnd] - lineStartTimes[i])
lineSigs.append(Video[idxStart:idxEnd])
# define pixel times within line
pixelsPerLine = 512
pixelStart = 0
#tPixelStart = 1.16645e-5 # 1.1377e-5
tPixelStart = 1.13911e-5
#tPixelEnd = 4.3503e-5
tPixelEnd = 4.4e-5
dt = (tPixelEnd - tPixelStart) / (pixelsPerLine-1 - pixelStart)
tPixelStart -= dt * pixelStart
tPixel = np.linspace(tPixelStart, tPixelEnd, pixelsPerLine)
##plt.figure()
##for tP in tPixel:
## plt.axvline(tP, color="k", linewidth=0.5)
##for i in range(actLines):
## plt.plot(lineTime[i], lineSigs[i])
# interpolate video signal onto pixels
image = np.zeros([actLines, pixelsPerLine])
for i,t in enumerate(lineTime):
s = lineSigs[i]
image[i,:] = np.where(np.interp(tPixel, t, s) > 0.5, 1, 0)
img.imsave("screencap.png", image, cmap="Greys")
plt.figure()
plt.imshow(image)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment