Advertisement
Guest User

pll

a guest
Mar 28th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1.         private const int SAMPLERATE = 100000;
  2.  
  3.         private static Timer TimerPhase, TimerADC;
  4.         private static SignalInput Signal;
  5.  
  6.         private static MovingAverage avg = new MovingAverage(3000);
  7.  
  8.         static void Main(string[] args) {
  9.             Signal = new SignalInput(50.0, Math.PI / 2);
  10.  
  11.             TimerPhase = new Timer(1000, 10, Int32.MaxValue, TimerPhaseCallback);
  12.             TimerADC = new Timer(SAMPLERATE, 0, 2, TimerADCCallback);
  13.  
  14.             foreach (var tick in Enumerable.Range(0, Clock.TicksFromSeconds(1))) {
  15.                 Clock.Tick();
  16.             }
  17.         }
  18.  
  19.         private static int RectSyncState = 1;
  20.         private static int RectSignalState = 1;
  21.         private static int TimerPhaseLevelDelta = 0;
  22.  
  23.         private static List<double> signalList = new List<double>();
  24.         private static List<double> phaseList = new List<double>();
  25.  
  26.         static void TimerPhaseCallback() {
  27.             if (RectSyncState == -1) RectSyncState = 1;
  28.             else if (RectSyncState == 1) RectSyncState = -1;
  29.         }
  30.  
  31.         static void TimerADCCallback() {
  32.             var sample = Signal.Sample();
  33.  
  34.             signalList.Add(sample);
  35.             phaseList.Add(RectSyncState);
  36.  
  37.             if (sample >= 0) {
  38.                 RectSignalState = 1;
  39.             } else {
  40.                 RectSignalState = -1;
  41.             }
  42.  
  43.             var phaseGuess = RectSyncState * RectSignalState;
  44.             avg.Consume(phaseGuess);
  45.             var filteredPhaseGuess = avg.Average;
  46.  
  47.             if (TimerADC.TickCount % 100 == 0)
  48.                 System.Diagnostics.Debug.Print(filteredPhaseGuess.ToString());
  49.  
  50.             if (Math.Abs(filteredPhaseGuess) >= 0.02) {
  51.                 TimerPhase.AlertLevel += 15;
  52.                 TimerPhaseLevelDelta++;
  53.             } else {
  54.                 TimerPhase.AlertLevel -= TimerPhaseLevelDelta * 15;
  55.                 TimerPhaseLevelDelta = 0;
  56.             }
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement