Advertisement
prog

Untitled

Jul 27th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.36 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.  
  139.     public class Oscillator
  140.     {
  141.         private int _sampleRate;
  142.         private int _frequency;
  143.         private int _tick;
  144.         private double _anglePerSample;
  145.         private double _outI;
  146.         private double _outQ;
  147.  
  148.         public int SampleRate
  149.         {
  150.             get { return _sampleRate; }
  151.             set
  152.             {
  153.                 _sampleRate = value;
  154.                 Configure();
  155.             }
  156.         }
  157.  
  158.         public int Frequency
  159.         {
  160.             get { return _frequency; }
  161.             set
  162.             {
  163.                 _frequency = value;
  164.                 Configure();
  165.             }
  166.         }
  167.  
  168.         private void Configure()
  169.         {
  170.                 _anglePerSample = (2.0 * Math.PI * _frequency) / _sampleRate;
  171.         }
  172.  
  173.         public double OutI
  174.         {
  175.             get { return _outI; }
  176.         }
  177.  
  178.         public double OutQ
  179.         {
  180.             get { return _outQ; }
  181.         }
  182.  
  183.         public Complex Out
  184.         {
  185.             get { return new Complex(_outI, _outQ); }
  186.         }
  187.  
  188.         public void Tick()
  189.         {
  190.             _outI = Math.Cos(_anglePerSample * _tick);
  191.             _outQ = Math.Sin(_anglePerSample * _tick);
  192.             _tick++;
  193.             if (_tick == int.MaxValue)
  194.             {
  195.                 _tick = 0;
  196.             }
  197.         }
  198.  
  199.         public static implicit operator Complex(Oscillator osc)
  200.         {
  201.             return osc.Out;
  202.         }
  203.     }
  204.  
  205.     public class ButterworthFilter2000 : IFilter
  206.     {
  207.         double GAIN = 3.867896374e+04;
  208.  
  209.         double[] xv = new double[6];
  210.         double[] yv = new double[6];
  211.  
  212.         public double Process(double s)
  213.         {
  214.             Array.Copy(xv, 1, xv, 0, 5);
  215.             xv[5] = s / GAIN;
  216.             Array.Copy(yv, 1, yv, 0, 5);
  217.             yv[5] = (xv[0] + xv[5]) + 5 * (xv[1] + xv[4]) + 10 * (xv[2] + xv[3])
  218.                      + (0.4273259957 * yv[0]) + (-2.4983654150 * yv[1])
  219.                      + (5.8779978532 * yv[2]) + (-6.9612764722 * yv[3])
  220.                      + (4.1534907153 * yv[4]);
  221.  
  222.             return yv[5];
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement