Created
September 27, 2016 19:03
-
-
Save jeanminet/2913ca7a87e96296b27e802575ad6153 to your computer and use it in GitHub Desktop.
Simple simulation of pulse density modulator
This file contains hidden or 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
import numpy as np | |
import matplotlib.pyplot as plt | |
def pdm(x): | |
n = len(x) | |
y = np.zeros(n) | |
error = np.zeros(n+1) | |
for i in range(n): | |
y[i] = 1 if x[i] >= error[i] else 0 | |
error[i+1] = y[i] - x[i] + error[i] | |
return y, error[0:n] | |
n = 100 | |
fclk = 250e6 # clock frequency (Hz) | |
t = np.arange(n) / fclk | |
f_sin = 5e6 # sine frequency (Hz) | |
x = 0.5 + 0.4 * np.sin(2*np.pi*f_sin*t) | |
y, error = pdm(x) | |
plt.plot(1e9*t, x, label='input signal') | |
plt.step(1e9*t, y, label='pdm signal', linewidth=2.0) | |
plt.step(1e9*t, error, label='error') | |
plt.xlabel('Time (ns)') | |
plt.ylim(-0.05,1.05) | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment