def envelope_detector(signal, frecuency, sampling_rate):
    """ Returns the envelope of the signal received in the transducer.
  
  The signal is multiplied by the carrier signal (in phase), and then low pass
  filtered."""
    angular_frecuency = 2 * np.pi * frecuency
    nyquist_frequency = sampling_rate / 2
    frecuency_width = 100
    filter_size = 11

    phase = pll.pll(signal, angular_frecuency, sampling_rate)
    time = np.arange(0, signal.size) / (1.0 * sampling_rate)
    demodulated_signal = np.multiply(signal, np.sin(angular_frecuency * time + phase))

    lowpass = scipysignal.firwin(filter_size, cutoff=frecuency_width, nyq=nyquist_frequency)
    demodulated_signal = scipysignal.lfilter(lowpass, 1, demodulated_signal)
    # The energy of the signal splits into a DC and twice the frecuency
    # components, so it must be multiplied by two.
    return 2 * np.abs(demodulated_signal)
 def estimate_tof(self, signals):
   # Average all the signals and calculate the envelope
   average = uniform_sampled_signal.average(signals)
   envelope = utilities.get_signal_envelope(average)
   # Find the intesection of the signals with a certain level.
   # To improve accuracy take tree slightly diffent levels and average.
   thresholds = []
   for thresholds_level in self.thresholds_levels:
     thresholds.append(self.get_intersection(envelope, thresholds_level))
   coarse_tof = (thresholds[0]+thresholds[1]+thresholds[2])/3
   # Using the information of the main frecuency in the response we now fit
   # the coase stimation to fit the phase of this wave
   f = 27100 # carrier frecuency in the response
   w = 2*np.pi*f # associated angular frecuecy
   T = 1.0/f # associated period
   phase = pll.pll(average, w)
   # get the number of the cycle that contains the coarse estimation
   n_cycle = np.floor((coarse_tof+(phase+np.pi)/w)/T)
   fine_tof = -phase/w+n_cycle*T
   print "%e"%fine_tof
   return fine_tof, phase
def envelope_detector(signal, frecuency, sampling_rate):
  """ Returns the envelope of the signal received in the transducer.
  
  The signal is multiplied by the carrier signal (in phase), and then low pass
  filtered."""
  angular_frecuency = 2*np.pi*frecuency
  nyquist_frequency = sampling_rate/2
  frecuency_width = 100
  filter_size = 11
  
  phase = pll.pll(signal, angular_frecuency, sampling_rate)
  time = np.arange(0, signal.size)/(1.0*sampling_rate)
  demodulated_signal = np.multiply(signal,
                                   np.sin(angular_frecuency*time+phase))
  
  lowpass = scipysignal.firwin(filter_size, cutoff = frecuency_width,
                               nyq = nyquist_frequency)
  demodulated_signal = scipysignal.lfilter(lowpass, 1, demodulated_signal)
  # The energy of the signal splits into a DC and twice the frecuency
  # components, so it must be multiplied by two.
  return 2*np.abs(demodulated_signal)