Skip to content

Instantly share code, notes, and snippets.

@jeanminet
Created September 27, 2016 19:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeanminet/2913ca7a87e96296b27e802575ad6153 to your computer and use it in GitHub Desktop.
Save jeanminet/2913ca7a87e96296b27e802575ad6153 to your computer and use it in GitHub Desktop.
Simple simulation of pulse density modulator
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()
@jeanminet
Copy link
Author

pdm_python

@jeanminet
Copy link
Author

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment