Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private const int SAMPLERATE = 100000;
- private static Timer TimerPhase, TimerADC;
- private static SignalInput Signal;
- private static MovingAverage avg = new MovingAverage(3000);
- static void Main(string[] args) {
- Signal = new SignalInput(50.0, Math.PI / 2);
- TimerPhase = new Timer(1000, 10, Int32.MaxValue, TimerPhaseCallback);
- TimerADC = new Timer(SAMPLERATE, 0, 2, TimerADCCallback);
- foreach (var tick in Enumerable.Range(0, Clock.TicksFromSeconds(1))) {
- Clock.Tick();
- }
- }
- private static int RectSyncState = 1;
- private static int RectSignalState = 1;
- private static int TimerPhaseLevelDelta = 0;
- private static List<double> signalList = new List<double>();
- private static List<double> phaseList = new List<double>();
- static void TimerPhaseCallback() {
- if (RectSyncState == -1) RectSyncState = 1;
- else if (RectSyncState == 1) RectSyncState = -1;
- }
- static void TimerADCCallback() {
- var sample = Signal.Sample();
- signalList.Add(sample);
- phaseList.Add(RectSyncState);
- if (sample >= 0) {
- RectSignalState = 1;
- } else {
- RectSignalState = -1;
- }
- var phaseGuess = RectSyncState * RectSignalState;
- avg.Consume(phaseGuess);
- var filteredPhaseGuess = avg.Average;
- if (TimerADC.TickCount % 100 == 0)
- System.Diagnostics.Debug.Print(filteredPhaseGuess.ToString());
- if (Math.Abs(filteredPhaseGuess) >= 0.02) {
- TimerPhase.AlertLevel += 15;
- TimerPhaseLevelDelta++;
- } else {
- TimerPhase.AlertLevel -= TimerPhaseLevelDelta * 15;
- TimerPhaseLevelDelta = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement