title: Python DSP, Automatic Gain Control (AGC)
body: <p>I'll try and ask specific questions here:-</p>
  
  <p>I'm using python to look at some relatively basic DSP, and I'd like to implement Automatic Gain Control. Unless I'm mistaken this takes the (simplified) form:</p>
  
  <p><img src="https://i.stack.imgur.com/ZFYrG.png" alt="enter image description here"></p>
  
  <p>I'm not too concerned by phase shifts between the i/o signals and the input signal is 1MHz sine wave modulated by a slowly varying wave (as indicated above) and I'm sampling at 40MHz. My questions are:</p>
  
  <p><strong>(i) I'm unable to find any decent description on AGC, suggestions for this are welcome.</strong></p>
  
  <p>[I have a very good book on <a href="http://www.dspguide.com/" rel="nofollow noreferrer">general DSP</a>, but AGC is only mentioned in passing]</p>
  
  <p>I know the basic idea of <em>low in-signal then high gain</em>, and vice versa; and I also appreciate a filter generating the 'fast attack'/'slow decay' response, but I'm unsure of details on AGC algorithms within DSP.  </p>
  
  <p>Secondly:</p>
  
  <p><strong>(ii) How may I achieve this within python?</strong> My starting code is something like this:</p>
  
  <pre><code>import numpy as np
  import pylab as plt
  
  # 1000 samples of a 1MHz signal, sampled at 40MHz
  sample_rate = 40e6
  fc          = 1e6
  fm          = 1e5
  nsamples    = 1000
  
  # for now assume an AM type signal  
  t = np.arange(nsamples) / sample_rate
  signal = np.sin(2*np.pi*fc*t) * ( (1 + np.sin(2*np.pi*fm*t))/2 )
  
  plt.plot(t, signal)
  plt.grid()
  plt.xlabel('time (0 to 25us)')
  plt.ylabel('ip signal amplitude')
  plt.show()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/fLAdn.png" alt="enter image description here"></p>