Skip to content

Instantly share code, notes, and snippets.

@kumanna
Created August 26, 2025 12:07
Show Gist options
  • Select an option

  • Save kumanna/316fe9b7cce8ef01f46baff30373b606 to your computer and use it in GitHub Desktop.

Select an option

Save kumanna/316fe9b7cce8ef01f46baff30373b606 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import sys
Ts = 0.000001
n_cycles = 240000
if len(sys.argv) > 1:
n_cycles = int(sys.argv[1])
t = np.arange(0, n_cycles * Ts, Ts)
A2 = 0.06
f1, theta1, fd1, f2, theta2, fd2 = 300, 0, 0, 400, np.pi / 3, 4
s1 = np.cos(2 * np.pi * (f1 + fd1) * t + theta1)
s2 = A2 * np.cos(2 * np.pi * (f2 + fd2) * t + theta2)
sign_plot = np.int8(np.sign(s1 + s2))
successive_difference = sign_plot[1:] - sign_plot[:-1]
# plt.stem(t, sign_plot, label=r'$\text{sign}(s_1(t) + s_2(t)) - \text{sign}(s_1(t))$')
# plt.legend()
# plt.figure()
# plt.stem(successive_difference)
# plt.show()
up_jumps = np.where(successive_difference == 2)[0] + 1
down_jumps = np.where(successive_difference == -2)[0] + 1
def phi(t, A, f1, f2, fd2, theta2):
return np.atan2(A * np.sin(2 * np.pi * (f2 - f1 + fd2) * t + theta2),
1 + A * np.cos(2 * np.pi * (f2 - f1 + fd2) * t + theta2))
# In up jumps, the phase should be 3pi / 2
idx = up_jumps[7]
t_up_jump = t[idx]
phi_idx = phi(t_up_jump, A2, f1, f2, fd2, theta2)
total_phase = phi_idx + 2 * np.pi * f1 * t_up_jump
total_phase = np.mod(total_phase, 2 * np.pi)
if total_phase > np.pi: total_phase -= 2 * np.pi
print(total_phase)
# In down jumps, the phase should be pi / 2
idx = down_jumps[6]
t_down_jump = t[idx]
phi_idx = phi(t_down_jump, A2, f1, f2, fd2, theta2)
total_phase = phi_idx + 2 * np.pi * f1 * t_down_jump
total_phase = np.mod(total_phase, 2 * np.pi)
if total_phase > np.pi: total_phase -= 2 * np.pi
print(total_phase)
def wrap_angle(total_phase):
total_phase = np.mod(total_phase, 2 * np.pi)
if total_phase > np.pi: total_phase -= 2 * np.pi
return total_phase
# Let's get rid of 2 * pi * f1 * t
phi_only_list = []
for t_up_jump in t[up_jumps]:
print(f"LOOP_DEBUG: {wrap_angle(2 * np.pi * f1 * t_up_jump + phi(t_up_jump, A2, f1, f2, fd2, theta2))}")
phi_only = wrap_angle(np.pi * 3 / 2 - 2 * np.pi * f1 * t_up_jump)
print(f"LOOP_DEBUG: {phi(t_up_jump, A2, f1, f2, fd2, theta2)}, {phi_only}")
phi_only_list.append(phi_only)
for t_down_jump in t[down_jumps]:
phi_only = np.pi / 2 - 2 * np.pi * f1 * t_down_jump
phi_only_list.append(wrap_angle(phi_only))
tan_vals = np.tan(phi_only_list)
print(np.max(np.abs(tan_vals)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment