Created
December 1, 2023 02:08
-
-
Save GaryLee/6276ecb0f88325d3751810ac46cbb3e2 to your computer and use it in GitHub Desktop.
A simple python function to generate digital clock.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
# coding: utf-8 | |
import numpy as np | |
def generate_clock(freq, time=10.0, phase=0.0, amplitude=1.0, duty=.5, resolution=10): | |
"""Generate clock. | |
Parameters | |
---------- | |
freq : float | |
The frequency of clock to be generated. Unit: Hz. | |
time : float | |
The total time of the sequence of clock signals. Unit: second. default: 10.0. | |
phase : float | |
The degree of phase for the generated clock. Unit: degree. Range: +/-180. default: 0. | |
amplitude: float | |
The amplitude of generated clock. default: 1.0. | |
duty: float | |
The duty of generated clock. Range: 0.0 ~ 1.0. default: 0.5. | |
resolution: int | |
The resolution of generated clock. The final resolutio is time * resolution * freq. default: 10. | |
Returns | |
------- | |
clk, t: numpy.array, numpy.array | |
The array of generated clock and time series. | |
""" | |
assert resolution > 1.0, "resolution must be larger than 1.0." | |
assert time > 0.0, "time must be larger than 0.0." | |
ticks = int(time * resolution * freq) | |
t = np.linspace(0.0, time, ticks) | |
start_deg = 180.0 * duty | |
clk_ref = np.sin(2 * np.pi * freq * t + np.radians(start_deg + phase)) | |
digitize_thrd = np.cos(np.radians(start_deg)) | |
clk = (clk_ref >= digitize_thrd) * amplitude | |
return clk, t | |
clk, t = generate_clock(freq=1.0) | |
# Plot. | |
import matplotlib.pyplot as plt | |
plt.title('y-position vs. time for falling cupcake paper') | |
plt.xlabel('t (s)') | |
plt.ylabel('y (m)') | |
plt.plot(t , clk,'m.-') | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment