Пример #1
0
func DecodeCW(path string,
	sample_rate float64,
	sample_type pb.IQParams_Type,
	cw_params *pb.CWParams) (
	[]pb.Contact_Blob, error) {

	const decimation = 1024 //512

	// 1. CW Filter
	filtered_path := fmt.Sprintf("%s_filtered", path)
	c := exec.Command("python", cwFilterPath,
		path,
		sample_type.String(),
		fmt.Sprintf("%f", sample_rate),
		fmt.Sprintf("%d", decimation),
		filtered_path)
	err := c.Run()
	if err != nil {
		return nil, err
	}

	file, err := os.Open(filtered_path)
	if err != nil {
		log.Printf("Error opening %s: %s", filtered_path, err.Error())
		return nil, err
	}
	r := bufio.NewReader(file)
	filtered := make([]float64, 0)
	for {
		f, err := binary.ReadFloat64LE(r)
		if err != nil {
			break
		}
		filtered = append(filtered, f)
	}
	file.Close()

	// Delete temporary file.
	err = os.Remove(filtered_path)
	if err != nil {
		fmt.Printf("Error deleting file: %s", err.Error())
		return nil, err
	}

	// 2. Optimize

	frame_duration_s := float64(decimation) / sample_rate

	log.Printf("%s\n", cw_params)

	dot_len := int(*cw_params.DotDurationS/frame_duration_s + 0.5)

	log.Printf("duration: %f\ndot_len: %d\n", frame_duration_s, dot_len)

	words := decode_cw(filtered, dot_len)
	text := strings.Join(words, " ")

	if text == "" {
		return nil, nil
	}

	blobs := make([]pb.Contact_Blob, 1)
	blobs[0].Format = pb.Contact_Blob_MORSE.Enum()
	blobs[0].InlineData = ([]byte)(text)
	return blobs, nil
}
Пример #2
0
// FIXME: add format param
func DecodePackets(path string,
	sample_rate_hz float64,
	sample_type pb.IQParams_Type,
	c pb.Channel) (
	blobs []pb.Contact_Blob, err error) {

	if c.DopplerStrategy == nil {
		return
	}
	doppler_strategy := c.DopplerStrategy.String()

	if c.Modulation == nil || c.Baud == nil {
		return
	}
	var demod_script string
	var multimon_type string
	if *c.Modulation == pb.Channel_LSB_BFSK &&
		*c.Baud == 1200 {
		demod_script = afsk1200LSBPath
		multimon_type = "AFSK1200"
	} else if *c.Modulation == pb.Channel_FM_GMSK &&
		*c.Baud == 9600 {
		demod_script = nbfm9600Path
		multimon_type = "FSK9600"
	} else {
		return nil, nil
	}

	log.Printf("Running doppler analysis")
	doppler_path := fmt.Sprintf("%s_doppler", path)
	c_analysis := exec.Command("python", dopplerAnalysisPath,
		path, sample_type.String(), doppler_path, doppler_strategy)
	err = c_analysis.Run()
	if err != nil {
		log.Printf("Error running %s: %s",
			dopplerAnalysisPath, err.Error())
		return nil, err
	}

	log.Printf("Running doppler correction")
	corrected_path := fmt.Sprintf("%s_corrected", path)
	err = doppler.ApplyDopplerCorrections(
		path, sample_type, doppler_path, corrected_path)
	if err != nil {
		log.Printf(
			"Error applying doppler corrections: %s", err.Error())
		return nil, err
	}

	log.Printf("Running demodulation")
	wav_path := fmt.Sprintf("%s.wav", path)
	c_demod := exec.Command("python", demod_script,
		corrected_path, fmt.Sprintf("%f", sample_rate_hz), wav_path)
	err = c_demod.Run()
	if err != nil {
		log.Printf("Error running %s: %s", demod_script, err.Error())
		return nil, err
	}

	log.Printf("Running multimon")
	c_decode := exec.Command(multimonPath,
		"-a", multimon_type,
		"-t", "wav",
		wav_path)
	out_pipe, err := c_decode.StdoutPipe()
	if err != nil {
		return nil, err
	}
	if err := c_decode.Start(); err != nil {
		return nil, err
	}

	r := bufio.NewReader(out_pipe)
	for {
		line, err := r.ReadString('\n')
		if err != nil {
			break
		}

		const packetPrefix = "HEXPACKET: "
		if !strings.HasPrefix(line, packetPrefix) {
			continue
		}
		log.Printf("%s", line)

		line = line[len(packetPrefix) : len(line)-1]
		frame, err := hex.DecodeString(line)
		if err != nil {
			log.Printf("Error decoding hex packet: %s", err.Error())
		}
		if len(frame) > 0 {
			var blob pb.Contact_Blob
			blob.Format = pb.Contact_Blob_FRAME.Enum()
			blob.InlineData = frame
			blobs = append(blobs, blob)
		}
	}
	out_pipe.Close()
	c_decode.Wait()

	log.Printf("Decoded %d packets.", len(blobs))

	// Delete temporary files.
	err = os.Remove(corrected_path)
	if err != nil {
		log.Printf("Error deleting file: %s", err.Error())
		return blobs, err
	}

	return blobs, nil
}