Skip to content

Commit

Permalink
Remove IQ buffer from decoder, implement buffer for sample dumping in…
Browse files Browse the repository at this point in the history
… main program.
  • Loading branch information
bemasher committed Apr 22, 2016
1 parent cedc2f8 commit 17b0016
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 1 addition & 5 deletions decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ type Decoder struct {
Decimation int
DecCfg PacketConfig

IQ []byte
Signal []float64
Filtered []float64
Quantized []byte
Expand Down Expand Up @@ -132,7 +131,6 @@ func NewDecoder(cfg PacketConfig, decimation int) (d Decoder) {
d.DecCfg = d.Cfg.Decimate(d.Decimation)

// Allocate necessary buffers.
d.IQ = make([]byte, d.Cfg.BufferLength<<1)
d.Signal = make([]float64, d.DecCfg.BlockSize+d.DecCfg.SymbolLength2)
d.Filtered = make([]float64, d.DecCfg.BlockSize)
d.Quantized = make([]byte, d.DecCfg.BufferLength)
Expand Down Expand Up @@ -171,13 +169,11 @@ func NewDecoder(cfg PacketConfig, decimation int) (d Decoder) {
// Decode accepts a sample block and performs various DSP techniques to extract a packet.
func (d Decoder) Decode(input []byte) []int {
// Shift buffers to append new block.
copy(d.IQ, d.IQ[d.Cfg.BlockSize<<1:])
copy(d.Signal, d.Signal[d.DecCfg.BlockSize:])
copy(d.Quantized, d.Quantized[d.DecCfg.BlockSize:])
copy(d.IQ[d.Cfg.PacketLength<<1:], input[:])

// Compute the magnitude of the new block.
d.demod.Execute(d.IQ[d.Cfg.PacketLength<<1:], d.Signal[d.DecCfg.SymbolLength2:])
d.demod.Execute(input, d.Signal[d.DecCfg.SymbolLength2:])

// Perform matched filter on new block.
d.Filter(d.Signal, d.Filtered)
Expand Down
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package main

import (
"bytes"
"encoding/xml"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -121,6 +123,7 @@ func (rcvr *Receiver) Run() {
}()

block := make([]byte, rcvr.p.Cfg().BlockSize2)
sampleBuf := new(bytes.Buffer)

start := time.Now()
for {
Expand All @@ -138,6 +141,15 @@ func (rcvr *Receiver) Run() {
log.Fatal("Error reading samples: ", err)
}

// If dumping samples, discard the oldest block from the buffer if
// it's full and write the new block to it.
if *sampleFilename != os.DevNull {
if sampleBuf.Len() > rcvr.p.Cfg().BufferLength<<1 {
io.CopyN(ioutil.Discard, sampleBuf, int64(len(block)))
}
sampleBuf.Write(block)
}

pktFound := false
indices := rcvr.p.Dec().Decode(block)

Expand All @@ -149,7 +161,7 @@ func (rcvr *Receiver) Run() {
var msg parse.LogMessage
msg.Time = time.Now()
msg.Offset, _ = sampleFile.Seek(0, os.SEEK_CUR)
msg.Length = rcvr.p.Cfg().BufferLength << 1
msg.Length = sampleBuf.Len()
msg.Message = pkt

err = encoder.Encode(msg)
Expand All @@ -175,7 +187,7 @@ func (rcvr *Receiver) Run() {

if pktFound {
if *sampleFilename != os.DevNull {
_, err = sampleFile.Write(rcvr.p.Dec().IQ)
_, err = sampleFile.Write(sampleBuf.Bytes())
if err != nil {
log.Fatal("Error writing raw samples to file:", err)
}
Expand Down

0 comments on commit 17b0016

Please sign in to comment.