package ru.r2cloud.jradio.demod;

import java.io.IOException;

import ru.r2cloud.jradio.ByteInput;
import ru.r2cloud.jradio.Context;
import ru.r2cloud.jradio.FloatInput;
import ru.r2cloud.jradio.blocks.ClockRecoveryMMComplex;
import ru.r2cloud.jradio.blocks.Constellation;
import ru.r2cloud.jradio.blocks.ConstellationSoftDecoder;
import ru.r2cloud.jradio.blocks.CostasLoop;
import ru.r2cloud.jradio.blocks.FloatToChar;
import ru.r2cloud.jradio.blocks.Rail;
import ru.r2cloud.jradio.blocks.RmsAgc;
import ru.r2cloud.jradio.blocks.RootRaisedCosineFilter;

public class QpskDemodulator implements ByteInput {

	private final FloatToChar f2char;

	public QpskDemodulator(FloatInput source, int symbolRate, Constellation constel) {
		float clockAlpha = 0.01f;
		RmsAgc agc = new RmsAgc(source, 1e-2f, 0.5f);
		RootRaisedCosineFilter rrcf = new RootRaisedCosineFilter(agc, 1.0f, symbolRate, 0.6f, 361);
		float omega = rrcf.getContext().getSampleRate() / symbolRate;
		ClockRecoveryMMComplex clockmm = new ClockRecoveryMMComplex(rrcf, omega, clockAlpha * clockAlpha / 4, 0.5f, clockAlpha, 0.005f);
		CostasLoop costas = new CostasLoop(clockmm, 0.008f, 4, false);
		ConstellationSoftDecoder constelDecoder = new ConstellationSoftDecoder(costas, constel);
		Rail rail = new Rail(constelDecoder, -1.0f, 1.0f);
		f2char = new FloatToChar(rail, 127.0f);
	}

	@Override
	public byte readByte() throws IOException {
		return f2char.readByte();
	}

	@Override
	public Context getContext() {
		return f2char.getContext();
	}

	@Override
	public void close() throws IOException {
		f2char.close();
	}

}

@@ -167,64 +167,8 @@
 ut;%0A
-import ru.r2cloud.jradio.blocks.ClockRecoveryMMComplex;%0A
 impo
@@ -210,16 +210,16 @@
 lation;%0A
+
 import r
@@ -312,16 +312,56 @@
 asLoop;%0A
+import ru.r2cloud.jradio.blocks.Firdes;%0A
 import r
@@ -397,16 +397,75 @@
 ToChar;%0A
+import ru.r2cloud.jradio.blocks.PolyphaseClockSyncComplex;%0A
 import r
@@ -537,64 +537,8 @@
 Agc;
-%0Aimport ru.r2cloud.jradio.blocks.RootRaisedCosineFilter;
 %0A%0Apu
@@ -712,36 +712,8 @@
 ) %7B%0A
-%09%09float clockAlpha = 0.01f;%0A
 %09%09Rm
@@ -762,181 +762,218 @@
 ;%0A%09%09
-RootRaisedCosineFilter rrcf = new RootRaisedCosineFilter(agc, 1.0f, symbolRate
+float omega = agc.getContext().getSampleRate() / symbolRate;%0A%09%09int nfilts = 16;%0A%09%09float%5B%5D rrcTaps = Firdes.rootRaisedCosine(nfilts, nfilts, 1.0f / omega
 , 0.
-6f, 361);%0A%09%09float omega = rrcf.getContext().getSampleRate() / symbolRate;%0A%09%09ClockRecoveryMM
+5f, (int) (11 * omega * nfilts));%0A%09%09PolyphaseClockSync
 Comp
@@ -994,31 +994,34 @@
 new 
-ClockRecoveryMM
+PolyphaseClockSync
 Complex(
 rrcf
@@ -1020,12 +1020,11 @@
 lex(
-rrcf
+agc
 , om
@@ -1032,61 +1032,50 @@
 ga, 
-clockAlpha * clockAlpha / 4, 0
+0.1f, rrcTaps, nfilts, nfilts / 2, 1
 .5f, 
-clockAlpha, 0.005f
+1
 );%0A%09
@@ -1127,9 +1127,9 @@
 0.00
-8
+6
 f, 4

switch qpskdemodulator from mm to pfb sync