Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahmetserdar/870575d7af00361eba913c4185d6fc16 to your computer and use it in GitHub Desktop.
Save ahmetserdar/870575d7af00361eba913c4185d6fc16 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment