Files
JaySynth/matlab/midi/eval_midi_sync.m
2025-08-21 07:27:52 +02:00

75 lines
1.2 KiB
Matlab
Executable File

function eval_midi_sync(pe, fe)
N = 5000;
t = (0:N-1)/N;
dx1 = 40/N
dx2 = (40+fe)/N
qi1 = 0;
qi2 = 0;
xi1 = 0;
xi2 = 0;
xx1 = 0;
xx2 = pe;
lead = 0;
lag = 0;
ddx = 0;
for i=1:N,
[qi1, qi2, xi1, xi2] = pd(xx1 >=0.5, xx2 >=0.5, qi1, qi2, xi1, xi2);
err = (qi1-qi2);
lead = 0.002*err;
lag = lag + 0.000005*err;
ddx(i) = (lead+lag);
xx1 = mod(xx1 + dx1, 1);
xx2 = mod(xx2 + dx2 + ddx(i), 1);
q1(i) = qi1;
q2(i) = qi2;
x1(i) = xx1 >=0.5;
x2(i) = xx2 >=0.5;
end;
subplot(4, 1, 1)
plot(t, x1, t, x2); grid; axis([0 1 -0.2 1.2])
subplot(4, 1, 2)
plot(t, q1); grid; axis([0 1 -0.2 1.2])
subplot(4, 1, 3)
plot(t, q2); grid; axis([0 1 -0.2 1.2])
subplot(4, 1, 4)
plot(t, ddx); grid;
function [Q1, Q2, xo1, xo2] = pd(x1, x2, qi1, qi2, xi1, xi2)
xo1 = xi1;
xo2 = xi2;
qo1 = qi1;
qo2 = qi2;
for i=1:length(x1)
% Q1
% Detect edge
if (x1(i) - xo1) > 0.5
qo1 = 1;
end;
xo1 = x1(i);
% Q2
% Detect edge
if (x2(i) - xo2) > 0.5
qo2 = 1;
end;
xo2 = x2(i);
% Asynchronous reset
if (qo1 * qo2) > 0.5
qo1 = 0;
qo2 = 0;
end;
Q1(i) = qo1;
Q2(i) = qo2;
end;