1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // Copyright (C) 2017 Keelan Lightfoot
- // Copyright (C) 2007-2008 Board of Regents of the University of Wisconsin
- // System (Univ. of Wisconsin-Madison, Trace R&D Center)
- // Copyright (C) 2007-2008 Omnitor AB
- // Copyright (C) 2007-2008 Voiceriver Inc
- //
- // This library is free software; you can redistribute it and/or modify it
- // under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation; either version 2.1 of the License, or (at
- // your option) any later version.
-
- package gortty
-
- import (
- "context"
- "math"
- )
-
- // RawDemodulator is fun
- type RawDemodulator struct {
- input chan int16
- output chan DCSignal
- dsp *dsp
- settings *ModemSettings
- sigOut bool
- samplerate int
- div int
- }
-
- // NewRawDemodulator is fun
- func NewRawDemodulator(ctx context.Context, s *ModemSettings, input chan int16, output chan DCSignal) *RawDemodulator {
-
- d := new(RawDemodulator)
- d.input = input
- d.output = output
-
- d.settings = s
-
- // the bandpass filter is picky about the sample rate, and the output is perfectly
- // balanced when the sample rate is 3x the center frequency, so lets divide the sample
- // rate down to as close to that as we can get.
-
- centerFrequency := math.Abs(float64(d.settings.OneFreq)+float64(d.settings.ZeroFreq)) / 2 * 3
-
- d.div = int(math.Floor(float64(d.settings.SampleRate) / float64(centerFrequency)))
- if d.div == 0 {
- d.div = 1
- }
-
- d.samplerate = d.settings.SampleRate / d.div
-
- d.dsp = newDsp(d.settings.OneFreq, d.settings.ZeroFreq, d.samplerate)
-
- go d.run(ctx)
- return d
- }
-
- func (d *RawDemodulator) run(ctx context.Context) {
-
- count := 0
- for {
- select {
- case <-ctx.Done():
- return
- case sam := <-d.input:
- // sample rate reduction
- count++
- if count%d.div > 0 {
- continue
- }
-
- d.dsp.demod(float64(sam))
-
- var signal int8
- var quality int8
-
- if math.Abs(d.dsp.demLpf) > d.dsp.demTotal*d.dsp.minThresh && d.dsp.demTotal*d.dsp.minThresh != 0 {
- quality = 1
- } else {
- quality = 0
- }
-
- if d.dsp.demLpf-d.dsp.minThresh*d.dsp.demTotal > 0 {
- signal = 1 * quality
- } else {
- signal = -1 * quality
- }
-
- d.output <- DCSignal{
- Signal: signal,
- Quality: quality,
- }
- }
- }
- }
|