Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@junklight
Created April 27, 2020 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junklight/617d63c30faaa24e727bd0a4cfe30c07 to your computer and use it in GitHub Desktop.
Save junklight/617d63c30faaa24e727bd0a4cfe30c07 to your computer and use it in GitHub Desktop.
simple tracking of oscillator output and phase
import mpld3
mpld3.enable_notebook()
import math
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
plt.rcParams['figure.figsize'] = [18,10]
x = 10000
x
samples = np.arange(x,dtype=np.float64)
settwo = np.arange(x,dtype=np.float64)
phase = 0
freq = 0.0004
class osc (object):
def __init__(self):
self.phase = 0.0
self.pulseStage = 0.0
def render(self,freq, pwm):
self.phase = self.phase + freq
while True:
if self.pulseStage == 0:
if self.phase < pwm:
break
self.pulseStage = 1.0
if self.pulseStage == 1:
if self.phase < 1.0:
break
self.pulseStage = 0.0
self.phase = self.phase - 1.0
return self.pulseStage
def getPhase(self):
return self.phase
o = osc();
for idx in range(0,x):
samples[idx] = o.render( freq , 0.5 )
settwo[idx] = o.getPhase()
phase += freq
x = np.linspace(0, x, x)
plt.plot(x, samples , label ='osc')
plt.plot(x, settwo , label ='phase' )
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("phase & osc out")
plt.legend()
plt.show()
@junklight
Copy link
Author

Screenshot 2020-04-27 at 12 01 38

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