Ejemplo n.º 1
0
class MovingAverage:
    def __init__(self, window_len):
        self.window_len = window_len
        self.buffer = Ringbuffer(window_len)

    def new_sample(self, x):
        self.buffer.new_sample(x)
        return self.buffer.get_sum() / float(self.window_len)
Ejemplo n.º 2
0
class MovingAverage:
    def __init__(self, window_len):
        self.window_len = window_len
        self.buffer = Ringbuffer(window_len)
        
        
    def new_sample(self, x):
        self.buffer.new_sample(x)
        return self.buffer.get_sum()/float(self.window_len)
Ejemplo n.º 3
0
class PLL:
    #phase locked loop
    def __init__(self, bp_filter, lp_filter, centre_freq, sr, loop_gain=1.0, in_gain=1.0):
        self.sr = sr
        self.base_rate = (centre_freq*2*math.pi) / sr
        self.int = 0
        self.bp_filter = bp_filter
        self.lp_filter = lp_filter
        self.phase_buffer = Ringbuffer(int(sr/centre_freq))
        self.loop_gain = loop_gain
        self.in_gain = in_gain
        
                
        
    def set_gain(self, gain):
        self.gain = gain
        
        
    def set_base_freq(self, freq):
        self.base_rate = (freq*2*math.pi) / self.sr
        
        
    def get_sync(self):
        return abs(self.phase_buffer.get_sum())
        
        
        
    def new_sample(self, x):
        (osc,int,y) = self.run(x)
        return int
        
    def run(self, x):                
    
        #band pass the input
        y = self.bp_filter.new_sample(x*self.in_gain)
        
        
            
        # drive the oscillator at natural frequency
        self.int = self.int + self.base_rate
        osc = math.sin(self.int)
        
        #compute phase difference
        phase_com = osc * y
        
        #store phase comparison
        self.phase_buffer.new_sample(phase_com)
        
        # filter the phase comparator
        ph_adj = self.lp_filter.new_sample(phase_com)
        
        # shift the integrator
        self.int = self.int + ph_adj * self.loop_gain
        
        #return value of oscillator, the phase value (unwrapped), and the bp filtered input signal
        return (osc, self.int, y)
Ejemplo n.º 4
0
class PLL:
    #phase locked loop
    def __init__(self,
                 bp_filter,
                 lp_filter,
                 centre_freq,
                 sr,
                 loop_gain=1.0,
                 in_gain=1.0):
        self.sr = sr
        self.base_rate = (centre_freq * 2 * math.pi) / sr
        self.int = 0
        self.bp_filter = bp_filter
        self.lp_filter = lp_filter
        self.phase_buffer = Ringbuffer(int(sr / centre_freq))
        self.loop_gain = loop_gain
        self.in_gain = in_gain

    def set_gain(self, gain):
        self.gain = gain

    def set_base_freq(self, freq):
        self.base_rate = (freq * 2 * math.pi) / self.sr

    def get_sync(self):
        return abs(self.phase_buffer.get_sum())

    def new_sample(self, x):
        (osc, int, y) = self.run(x)
        return int

    def run(self, x):

        #band pass the input
        y = self.bp_filter.new_sample(x * self.in_gain)

        # drive the oscillator at natural frequency
        self.int = self.int + self.base_rate
        osc = math.sin(self.int)

        #compute phase difference
        phase_com = osc * y

        #store phase comparison
        self.phase_buffer.new_sample(phase_com)

        # filter the phase comparator
        ph_adj = self.lp_filter.new_sample(phase_com)

        # shift the integrator
        self.int = self.int + ph_adj * self.loop_gain

        #return value of oscillator, the phase value (unwrapped), and the bp filtered input signal
        return (osc, self.int, y)