Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Radio
- {
- public class AutomaticGainControl : IFilter
- {
- private const double MeanLevel = 0.0001;
- private const double MaxFactor = 1.0;
- private const double MinFactor = 1000.0;
- private const double TimeConst = 0.00005;
- private readonly Integrator _integrator = new Integrator(TimeConst);
- public double Process(double sample)
- {
- var dc = _integrator.Process(sample);
- var factor = MeanLevel / dc;
- if (factor > MaxFactor)
- {
- factor = MaxFactor;
- }
- if (factor < MinFactor)
- {
- factor = MinFactor;
- }
- return sample * factor;
- }
- }
- public class Integrator : IFilter
- {
- private readonly double _ratio;
- private double _mean;
- public Integrator()
- {
- _ratio = 0.01;
- }
- public Integrator(double ratio)
- {
- _ratio = ratio;
- }
- public double Process(double sample)
- {
- _mean = _mean * (1 - _ratio) + sample * _ratio;
- return _mean;
- }
- }
- public class DcRemover : IFilter
- {
- private readonly Integrator _integrator;
- public DcRemover()
- {
- _integrator = new Integrator(0.01);
- }
- public DcRemover(double ratio)
- {
- _integrator = new Integrator(ratio);
- }
- public double Process(double sample)
- {
- return sample - _integrator.Process(sample);
- }
- }
- public class AmDetector : IDetector
- {
- private readonly DcRemover _dcRemover = new DcRemover();
- public double Demodulate(Complex d)
- {
- var sample = d.Modulus();
- return _dcRemover.Process(sample);
- }
- }
- public class LsbDetector : IDetector
- {
- private int _sampleRate;
- private readonly Oscillator _bfo = new Oscillator();
- public LsbDetector()
- {
- _bfo.Frequency = Radio.BfoFrequency;
- }
- public double Demodulate(Complex sample)
- {
- _bfo.Tick();
- var product = sample * _bfo.Out.Conjugate();
- return product.Real + product.Imag;
- }
- public int SampleRate
- {
- get { return _sampleRate; }
- set
- {
- _sampleRate = value;
- _bfo.SampleRate = value;
- }
- }
- }
- public class UsbDetector : IDetector
- {
- private int _sampleRate;
- private readonly Oscillator _bfo = new Oscillator();
- public UsbDetector()
- {
- _bfo.Frequency = Radio.BfoFrequency;
- }
- public double Demodulate(Complex sample)
- {
- _bfo.Tick();
- var product = sample * _bfo;
- return product.Real + product.Imag;
- }
- public int SampleRate
- {
- get { return _sampleRate; }
- set
- {
- _sampleRate = value;
- _bfo.SampleRate = value;
- }
- }
- }
- public class Oscillator
- {
- private int _sampleRate;
- private int _frequency;
- private int _tick;
- private double _anglePerSample;
- private double _outI;
- private double _outQ;
- public int SampleRate
- {
- get { return _sampleRate; }
- set
- {
- _sampleRate = value;
- Configure();
- }
- }
- public int Frequency
- {
- get { return _frequency; }
- set
- {
- _frequency = value;
- Configure();
- }
- }
- private void Configure()
- {
- _anglePerSample = (2.0 * Math.PI * _frequency) / _sampleRate;
- }
- public double OutI
- {
- get { return _outI; }
- }
- public double OutQ
- {
- get { return _outQ; }
- }
- public Complex Out
- {
- get { return new Complex(_outI, _outQ); }
- }
- public void Tick()
- {
- _outI = Math.Cos(_anglePerSample * _tick);
- _outQ = Math.Sin(_anglePerSample * _tick);
- _tick++;
- if (_tick == int.MaxValue)
- {
- _tick = 0;
- }
- }
- public static implicit operator Complex(Oscillator osc)
- {
- return osc.Out;
- }
- }
- public class ButterworthFilter2000 : IFilter
- {
- double GAIN = 3.867896374e+04;
- double[] xv = new double[6];
- double[] yv = new double[6];
- public double Process(double s)
- {
- Array.Copy(xv, 1, xv, 0, 5);
- xv[5] = s / GAIN;
- Array.Copy(yv, 1, yv, 0, 5);
- yv[5] = (xv[0] + xv[5]) + 5 * (xv[1] + xv[4]) + 10 * (xv[2] + xv[3])
- + (0.4273259957 * yv[0]) + (-2.4983654150 * yv[1])
- + (5.8779978532 * yv[2]) + (-6.9612764722 * yv[3])
- + (4.1534907153 * yv[4]);
- return yv[5];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement