You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.3 KiB
Python

5 years ago
import numpy as np
def generateAMSine(fs, toneFreq, numSamples, amplitude):
step = (float(toneFreq) / float(fs)) * 2.0 * np.pi
phaseArray = np.array(range(0,numSamples)) * step
#e^(j*theta) = cos(theta) + j * sin(theta)
wave = np.exp(1.0j * phaseArray) * amplitude
return wave
class OOKModulator:
def __init__(self, baseband_samplerate, am_frequency):
self.baseband_samplerate = baseband_samplerate
self.am_frequency = am_frequency
5 years ago
self.samples = generateAMSine(self.baseband_samplerate, 0, 1000, 0)
5 years ago
def addPadding(self, uSDuration):
sampleCount = int((uSDuration / 1000000.0) * self.baseband_samplerate)
self.samples = np.append(self.samples, generateAMSine(self.baseband_samplerate, 0, sampleCount, 0))
def addModulation(self, uSDuration):
sampleCount = int((uSDuration / 1000000.0) * self.baseband_samplerate)
self.samples = np.append(self.samples, generateAMSine(self.baseband_samplerate, self.am_frequency, sampleCount, 1))
def getSamples(self, numpyType):
5 years ago
self.addPadding(1000)
5 years ago
return self.samples.astype(numpyType)
def getSamplesAndReset(self, numpyType):
5 years ago
self.addPadding(1000)
5 years ago
returnSamples = self.samples.astype(numpyType)
5 years ago
self.samples = generateAMSine(self.baseband_samplerate, 0, 1000, 0)
5 years ago
return returnSamples