Advertisement
prog

Untitled

Jul 27th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Radio
  4. {
  5.     public class AutomaticGainControl : IFilter
  6.     {
  7.         private const double MeanLevel = 0.0001;
  8.         private const double MaxFactor = 1.0;
  9.         private const double MinFactor = 1000.0;
  10.         private const double TimeConst = 0.00005;
  11.  
  12.         private readonly Integrator _integrator = new Integrator(TimeConst);
  13.  
  14.         public double Process(double sample)
  15.         {
  16.             var dc = _integrator.Process(sample);
  17.             var factor = MeanLevel / dc;
  18.             if (factor > MaxFactor)
  19.             {
  20.                 factor = MaxFactor;
  21.             }
  22.             if (factor < MinFactor)
  23.             {
  24.                 factor = MinFactor;
  25.             }
  26.             return sample * factor;
  27.         }
  28.     }
  29.  
  30.     public class Integrator : IFilter
  31.     {
  32.         private readonly double _ratio;
  33.         private double _mean;
  34.  
  35.         public Integrator()
  36.         {
  37.             _ratio = 0.01;
  38.         }
  39.  
  40.         public Integrator(double ratio)
  41.         {
  42.             _ratio = ratio;
  43.         }
  44.  
  45.         public double Process(double sample)
  46.         {
  47.             _mean = _mean * (1 - _ratio) + sample * _ratio;
  48.             return _mean;
  49.         }
  50.     }
  51.  
  52.     public class DcRemover : IFilter
  53.     {
  54.         private readonly Integrator _integrator;
  55.  
  56.         public DcRemover()
  57.         {
  58.             _integrator = new Integrator(0.01);
  59.         }
  60.        
  61.         public DcRemover(double ratio)
  62.         {
  63.             _integrator = new Integrator(ratio);
  64.         }
  65.  
  66.         public double Process(double sample)
  67.         {
  68.             return sample - _integrator.Process(sample);
  69.         }
  70.     }
  71.  
  72.     public class AmDetector : IDetector
  73.     {
  74.         private readonly DcRemover _dcRemover = new DcRemover();
  75.  
  76.         public double Demodulate(Complex d)
  77.         {
  78.             var sample = d.Modulus();
  79.             return _dcRemover.Process(sample);
  80.         }
  81.     }
  82.  
  83.     public class LsbDetector : IDetector
  84.     {
  85.         private int _sampleRate;
  86.         private readonly Oscillator _bfo = new Oscillator();
  87.  
  88.         public LsbDetector()
  89.         {
  90.             _bfo.Frequency = Radio.BfoFrequency;
  91.         }
  92.  
  93.         public double Demodulate(Complex sample)
  94.         {
  95.             _bfo.Tick();
  96.             var product = sample * _bfo.Out.Conjugate();
  97.             return product.Real + product.Imag;
  98.         }
  99.  
  100.         public int SampleRate
  101.         {
  102.             get { return _sampleRate; }
  103.             set
  104.             {
  105.                 _sampleRate = value;
  106.                 _bfo.SampleRate = value;
  107.             }
  108.         }
  109.     }
  110.  
  111.     public class UsbDetector : IDetector
  112.     {
  113.         private int _sampleRate;
  114.         private readonly Oscillator _bfo = new Oscillator();
  115.  
  116.         public UsbDetector()
  117.         {
  118.             _bfo.Frequency = Radio.BfoFrequency;
  119.         }
  120.  
  121.         public double Demodulate(Complex sample)
  122.         {
  123.             _bfo.Tick();
  124.             var product = sample * _bfo;
  125.             return product.Real + product.Imag;
  126.         }
  127.  
  128.         public int SampleRate
  129.         {
  130.             get { return _sampleRate; }
  131.             set
  132.             {
  133.                 _sampleRate = value;
  134.                 _bfo.SampleRate = value;
  135.             }
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement