In [5]:
from __future__ import division
from __future__ import print_function

from math import pi,sin
from math import fmod

import matplotlib.pyplot as plt

from myhdl import *

In [22]:
def analog_sine(out):
    """
    this module assumes 1 simulation tick represents a very 
    very high frequency.  In otherwords the digital "clock" 
    periods in simulation ticks is large (1000 or more).  To 
    relate back to absolute frequencies simply use the ratios ...
    
    """
    @instance
    def model():
        phia = 0            # phase accumulator
        phinc = (2*pi)/1000 # say a 1GHz signal (say 1 sim tick == 1ps)
        while True:
            out.next = sin(phia)
            phia = fmod(phia + phinc, 2*pi)
            yield delay(1)
    return model
            
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# use the above model
sine = Signal(0.)          # a float/real
gsine = analog_sine(sine)  # the analog model (or rather )
capture1 = []              # collect samples
capture2 = []

# generate a new signal that is 2x the generated sine
sine2x = Signal(0.)
@always(delay(1))
def model_sine2():
    sine2x.next = 2*sine
    
@instance
def tbstim():
    for ii in range(16*8192):
        yield delay(1)
        capture1.append(float(sine))  # cast float or use sine.val
        capture2.append(float(sine2x))
    raise StopSimulation
    
Simulation((tbstim, gsine, model_sine2,)).run()


fig,axm = plt.subplots(2, figsize=(12,6.75))
axm[0].plot(capture1[:2000])
axm[0].plot(capture2[:2000])

Pxx,f = axm[1].psd(capture1, Fs=1e12, NFFT=8*8192)
axm[1].set_xlim(0, 4e9)
axm[1].set_ylim(-180, -40)
print("end")


end

In [ ]: