From 44e1520dbe02da418d02128ef5347d91682146b5 Mon Sep 17 00:00:00 2001 From: bemasher Date: Sat, 1 Sep 2018 17:41:15 -0600 Subject: [PATCH] Remove generator test. --- gen/gen.go | 118 ------------------------------ gen/gen_test.go | 185 ------------------------------------------------ 2 files changed, 303 deletions(-) delete mode 100644 gen/gen.go delete mode 100644 gen/gen_test.go diff --git a/gen/gen.go b/gen/gen.go deleted file mode 100644 index 2c6a523c7..000000000 --- a/gen/gen.go +++ /dev/null @@ -1,118 +0,0 @@ -package gen - -import ( - "crypto/rand" - "fmt" - "math" - - "github.com/bemasher/rtlamr/crc" -) - -func NewRandSCM() (pkt []byte, err error) { - bch := crc.NewCRC("BCH", 0, 0x6F63, 0) - - pkt = make([]byte, 12) - _, err = rand.Read(pkt) - if err != nil { - return nil, err - } - - pkt[0] = 0xF9 - pkt[1] = 0x53 - pkt[2] &= 0x07 - - checksum := bch.Checksum(pkt[2:10]) - pkt[10] = uint8(checksum >> 8) - pkt[11] = uint8(checksum & 0xFF) - - return -} - -type ManchesterLUT [16]byte - -func NewManchesterLUT() ManchesterLUT { - return ManchesterLUT{ - 85, 86, 89, 90, 101, 102, 105, 106, 149, 150, 153, 154, 165, 166, 169, 170, - } -} - -func (lut ManchesterLUT) Encode(data []byte) (manchester []byte) { - manchester = make([]byte, len(data)<<1) - - for idx := range data { - manchester[idx<<1] = lut[data[idx]>>4] - manchester[idx<<1+1] = lut[data[idx]&0x0F] - } - - return -} - -func UnpackBits(data []byte) []byte { - bits := make([]byte, len(data)<<3) - - for idx, b := range data { - offset := idx << 3 - for bit := 7; bit >= 0; bit-- { - bits[offset+(7-bit)] = (b >> uint8(bit)) & 0x01 - } - } - - return bits -} - -func Upsample(bits []byte, factor int) []byte { - signal := make([]byte, len(bits)*factor) - - for idx, b := range bits { - offset := idx * factor - for i := 0; i < factor; i++ { - signal[offset+i] = b - } - } - - return signal -} - -func CmplxOscillatorS8(samples int, freq float64, samplerate float64) []int8 { - signal := make([]int8, samples<<1) - - for idx := 0; idx < samples<<1; idx += 2 { - s, c := math.Sincos(2 * math.Pi * float64(idx) * freq / samplerate) - signal[idx] = int8(s * 127.5) - signal[idx+1] = int8(c * 127.5) - } - - return signal -} - -func CmplxOscillatorU8(samples int, freq float64, samplerate float64) []uint8 { - signal := make([]uint8, samples<<1) - - for idx := 0; idx < samples<<1; idx += 2 { - s, c := math.Sincos(2 * math.Pi * float64(idx) * freq / samplerate) - signal[idx] = uint8(s*127.5 + 127.5) - signal[idx+1] = uint8(c*127.5 + 127.5) - } - - return signal -} - -func CmplxOscillatorF64(samples int, freq float64, samplerate float64) []float64 { - signal := make([]float64, samples<<1) - - for idx := 0; idx < len(signal); idx += 2 { - signal[idx], signal[idx+1] = math.Sincos(2 * math.Pi * float64(idx) * freq / samplerate) - } - - return signal -} - -func F64toU8(f64 []float64, u8 []byte) { - if len(f64) != len(u8) { - panic(fmt.Errorf("arrays must have same dimensions: %d != %d", len(f64), len(u8))) - } - - for idx, val := range f64 { - u8[idx] = uint8(val*127.5 + 127.5) - } -} diff --git a/gen/gen_test.go b/gen/gen_test.go deleted file mode 100644 index 774252a60..000000000 --- a/gen/gen_test.go +++ /dev/null @@ -1,185 +0,0 @@ -package gen - -import ( - "bytes" - "encoding/binary" - "io" - "log" - "math" - "math/rand" - "os" - "strconv" - "strings" - "testing" - - "github.com/bemasher/rtlamr/crc" - "github.com/bemasher/rtlamr/parse" - - _ "github.com/bemasher/rtlamr/scm" -) - -func init() { - log.SetFlags(log.Lshortfile | log.Lmicroseconds) -} - -func TestNewRandSCM(t *testing.T) { - bch := crc.NewCRC("BCH", 0, 0x6F63, 0) - - for i := 0; i < 512; i++ { - scm, err := NewRandSCM() - if err != nil { - t.Fatal(err) - } - - checksum := bch.Checksum(scm[2:]) - if checksum != 0 { - t.Fatalf("Failed checksum: %04X\n", checksum) - } - } -} - -func TestManchesterLUT(t *testing.T) { - lut := NewManchesterLUT() - - recv := lut.Encode([]byte{0x00}) - expt := []byte{0x55, 0x55} - if !bytes.Equal(recv, expt) { - t.Fatalf("Expected %02X got %02X\n", expt, recv) - } - - recv = lut.Encode([]byte{0xF9, 0x53}) - expt = []byte{0xAA, 0x96, 0x66, 0x5A} - if !bytes.Equal(recv, expt) { - t.Fatalf("Expected %02X got %02X\n", expt, recv) - } -} - -func TestUnpackBits(t *testing.T) { - t.Logf("%d\n", UnpackBits([]byte{0xF9, 0x53})) -} - -func TestUpsample(t *testing.T) { - t.Logf("%d\n", Upsample(UnpackBits([]byte{0xF9, 0x53}), 8)) -} - -func TestCmplxOscillatorS8(t *testing.T) { - t.SkipNow() - signalFile, err := os.Create("cmplxs8.bin") - if err != nil { - t.Fatal(err) - } - defer signalFile.Close() - - err = binary.Write(signalFile, binary.BigEndian, CmplxOscillatorS8(1<<10, 5e3, 2.4e6)) - if err != nil { - t.Fatal(err) - } -} - -func TestCmplxOscillatorU8(t *testing.T) { - t.SkipNow() - - signalFile, err := os.Create("cmplxu8.bin") - if err != nil { - t.Fatal(err) - } - defer signalFile.Close() - - err = binary.Write(signalFile, binary.BigEndian, CmplxOscillatorU8(1<<10, 5e3, 2.4e6)) - if err != nil { - t.Fatal(err) - } -} - -type TestCase struct { - *io.PipeReader - Data []byte - SignalLevelIdx int -} - -func TestGenerateSCM(t *testing.T) { - genParser, err := parse.NewParser("scm", 72) - 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} - - go func() { - var block []byte - noise := make([]byte, cfg.BlockSize2<<1) - - for signalLevelIdx, signalLevel := range signalLevels { - for idx := 0; idx < 24; idx++ { - r, w := io.Pipe() - - scm, _ := NewRandSCM() - testCases <- TestCase{r, scm, signalLevelIdx} - - 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(signalLevels)) - - for testCase := range testCases { - p, err := parse.NewParser("scm", 72) - 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.SignalLevelIdx]++ - } - - if err == io.EOF { - testCase.Close() - break - } - } - } - - var row []string - for _, count := range results { - row = append(row, strconv.Itoa(count)) - } - t.Log(strings.Join(row, ",")) -}