Пример #1
0
func (rcvr *Receiver) NewReceiver() {
	var err error
	if rcvr.p, err = parse.NewParser(strings.ToLower(*msgType), *symbolLength, *decimation); err != nil {
		log.Fatal(err)
	}

	// Connect to rtl_tcp server.
	if err := rcvr.Connect(nil); err != nil {
		log.Fatal(err)
	}

	rcvr.HandleFlags()

	cfg := rcvr.p.Cfg()

	gainFlagSet := false
	flag.Visit(func(f *flag.Flag) {
		switch f.Name {
		case "centerfreq":
			cfg.CenterFreq = uint32(rcvr.Flags.CenterFreq)
		case "samplerate":
			cfg.SampleRate = int(rcvr.Flags.SampleRate)
		case "gainbyindex", "tunergainmode", "tunergain", "agcmode":
			gainFlagSet = true
		case "unique":
			rcvr.fc.Add(NewUniqueFilter())
		case "filterid":
			rcvr.fc.Add(meterID)
		case "filtertype":
			rcvr.fc.Add(meterType)
		}
	})

	rcvr.SetCenterFreq(cfg.CenterFreq)
	rcvr.SetSampleRate(uint32(cfg.SampleRate))

	if !gainFlagSet {
		rcvr.SetGainMode(true)
	}

	if !*quiet {
		rcvr.p.Log()
	}

	// Tell the user how many gain settings were reported by rtl_tcp.
	if !*quiet {
		log.Println("GainCount:", rcvr.SDR.Info.GainCount)
	}

	return
}
Пример #2
0
func TestGenerateSCM(t *testing.T) {
	genParser, err := parse.NewParser("scm", 72, 1)
	if err != nil {
		t.Fatal(err)
	}

	cfg := genParser.Cfg()
	lut := NewManchesterLUT()

	noisedB := -30.0
	noiseAmp := math.Pow(10, noisedB/20)

	testCases := make(chan TestCase)

	signalLevels := []float64{-40, -35, -30, -25, -20, -15, -10, -5, 0}
	decimationFactors := []int{1, 2, 3, 4, 6, 8, 9, 12, 18}

	go func() {
		var block []byte
		noise := make([]byte, cfg.BlockSize2<<1)

		for signalLevelIdx, signalLevel := range signalLevels {
			for decimationIdx, _ := range decimationFactors {
				for idx := 0; idx < 24; idx++ {
					r, w := io.Pipe()

					scm, _ := NewRandSCM()
					testCases <- TestCase{r, scm, signalLevelIdx, decimationIdx}

					manchester := lut.Encode(scm)
					bits := Upsample(UnpackBits(manchester), 72<<1)

					freq := (rand.Float64() - 0.5) * float64(cfg.SampleRate)
					carrier := CmplxOscillatorF64(len(bits)>>1, freq, float64(cfg.SampleRate))

					signalAmplitude := math.Pow(10, signalLevel/20)
					for idx := range carrier {
						carrier[idx] *= float64(bits[idx]) * signalAmplitude
						carrier[idx] += (rand.Float64() - 0.5) * 2.0 * noiseAmp
					}

					if len(block) != len(carrier) {
						block = make([]byte, len(carrier))
					}
					F64toU8(carrier, block)

					w.Write(block)
					for idx := range noise {
						noise[idx] = byte((rand.Float64()-0.5)*2.0*noiseAmp*127.5 + 127.5)
					}
					w.Write(noise)
					w.Close()
				}
			}
		}
		close(testCases)
	}()

	results := make([][]int, len(decimationFactors))
	for idx := range results {
		results[idx] = make([]int, len(signalLevels))
	}

	for testCase := range testCases {
		p, err := parse.NewParser("scm", 72, decimationFactors[testCase.DecimationIdx])
		if err != nil {
			t.Fatal(err)
		}

		cfg := p.Cfg()
		block := make([]byte, cfg.BlockSize2)

		for {
			_, err := testCase.Read(block)
			indices := p.Dec().Decode(block)
			for _ = range p.Parse(indices) {
				results[testCase.DecimationIdx][testCase.SignalLevelIdx]++
			}

			if err == io.EOF {
				testCase.Close()
				break
			}
		}
	}

	for idx := range results {
		var row []string
		for _, count := range results[idx] {
			row = append(row, strconv.Itoa(count))
		}
		t.Log(strings.Join(row, ","))
	}
}