def testbits(self, M, prbsseed): s = signals.SignalQAMGrayCoded(M, 1000, nmodes=1, seed=[prbsseed]) npt.assert_array_almost_equal(s.demodulate(s), s.bits)
def test_comp_freq_offset(self): s = signals.SignalQAMGrayCoded(16, 2**16, fb=20e9, nmodes=1) ph = np.ones(1) * 1e6 s2 = cphaserecovery.comp_freq_offset(s.flatten(), ph) assert 2**16 == s2.shape[0]
def test_bps(self, dtype): s = signals.SignalQAMGrayCoded(32, 2**12, dtype=dtype) s *= np.exp(1.j*np.pi/3) s2, ph = phaserec.bps(s, 32, 10, method="pyt") assert s2.dtype is np.dtype(dtype) assert ph.dtype.itemsize is np.dtype(dtype).itemsize//2
def test_comp_freq_offset(self, ndim): s = signals.SignalQAMGrayCoded(16, 2**16, fb=20e9, nmodes=ndim) ph = np.ones(ndim) * 1e6 s2 = cphaserecovery.comp_freq_offset(s, ph) assert ndim == s2.shape[0]
def test_bps_twostage(self): s = signals.SignalQAMGrayCoded(32, 2**16, fb=20e9, nmodes=1) s2, ph = cphaserecovery.bps_twostage(s.flatten(), 32, s.coded_symbols, 10) assert 2**16 == s2.shape[0]
def test_viterbi(self, ndim): s = signals.SignalQAMGrayCoded(4, 2**16, fb=20e9, nmodes=ndim) s2, ph = cphaserecovery.viterbiviterbi(s, 10, 4) assert ndim == s2.shape[0]
def test_phase_partition_16qam(self, ndim): s = signals.SignalQAMGrayCoded(16, 2**16, fb=20e9, nmodes=ndim) s2, ph = cphaserecovery.phase_partition_16qam(s, 10) assert ndim == s2.shape[0]
def test_dtype(self, dtype): s = signals.SignalQAMGrayCoded(32, 2**12, dtype=dtype) s = impairments.change_snr(s, 22).astype(dtype) o = cython_equalisation.make_decision(s[0], s.coded_symbols) xx = abs(s.symbols[0] - o) npt.assert_array_almost_equal(xx, 0)
def test_dtype(self, dtype): s = signals.SignalQAMGrayCoded(32, 2**12, dtype=dtype) s = impairments.change_snr(s, 23).astype(dtype) o = cython_equalisation.make_decision(s[0], s.coded_symbols) assert o.dtype is np.dtype(dtype)
'mu': [mu], 'ntaps': [ntaps], 'BER': [ber[i_mode]], 'samp_converged': [samp_converged[i_mode]], 'final_error': [final_error[i_mode]] } df_temp = df_temp.append(pd.DataFrame(data=d)) return df_temp for pmd in list_pmd: for snr in list_snr: sigdir = create_dir_for_signal(testDir, pmd, snr, apply_impulse, resample) for r in range(nrepetitions): sig = signals.SignalQAMGrayCoded(4, N, fb=25e9, nmodes=2) if resample: sig = sig.resample(2 * sig.fb, beta=0.1, renormalise=True) if apply_impulse: sig = apply_impulse_response_impairment(sig) if apply_matrix: sig[:, :] = apply_2chnl_delayed_matrix_impairment( sig, 1e-9, 25e9) sig = impairments.apply_PMD(sig, np.pi / 5.6, pmd) sig = impairments.change_snr(sig, snr) sig = impairments.apply_phase_noise(sig, phase_noise) plot_constellation(sig[:, t_conv:t_stop], "Recieved", True, sigdir)
j = 0 fb = 10e9 os = 2 fs = os * fb ntaps = 13 beta = 0.1 for M in Mqams: print("%d-QAM" % M) ser = np.zeros(snr.shape) ber = np.zeros(snr.shape) evm1 = np.zeros(snr.shape) evm_known = np.zeros(snr.shape) i = 0 for sr in snr: print("SNR = %2f.0 dB" % sr) signal = signals.SignalQAMGrayCoded(M, N, nmodes=1, fb=fb) signal = signal.resample(fnew=fs, beta=beta, renormalise=True) signal_s = impairments.change_snr(signal, sr) #signalx = np.atleast_2d(filtering.rrcos_pulseshaping(signal_s, beta)) wx, er = equalisation.equalise_signal(signal_s, 3e-4, Ntaps=ntaps, method="mcma", adaptive_step=True) signalafter = equalisation.apply_filter(signal_s, wx) signalafter = helpers.normalise_and_center(signalafter) evm1[i] = signal.cal_evm()[0] evm_known[i] = signalafter.cal_evm() # check to see that we can recovery timing delay #signalafter = np.roll(signalafter * 1.j**np.random.randint(0,4), np.random.randint(4, 3000)) ser[i] = signalafter.cal_ser()
import numpy as np from qampy import impairments, phaserec from qampy import signals, helpers fb = 40.e9 os = 1 fs = os * fb N = 3 * 10**5 M = 64 snr = 30 lw_LO = np.linspace(10e1, 1000e1, 4) #lw_LO = [100e3] sers = [] for lw in lw_LO: shiftN = np.random.randint(-N / 2, N / 2, 1) s = signals.SignalQAMGrayCoded(M, N, fb=fb) s = s.resample(fs, beta=0.1, renormaise=True) s = impairments.change_snr(s, snr) s = np.roll(s, shiftN, axis=1) pp = impairments.apply_phase_noise(s, lw) recoverd, ph1 = phaserec.bps_twostage(pp, 28, 14, method='pyx') recoverd_2, ph2 = phaserec.bps(pp, 64, 14, method='pyx') recoverd = helpers.dump_edges(recoverd, 20) recoverd_2 = helpers.dump_edges(recoverd_2, 20) ser = recoverd.cal_ser() ser2 = recoverd_2.cal_ser() print("1 stage pyx ser=%g" % ser) print("2 stage pyx ser=%g" % ser2)
def test_from_symbol_array(self, dt): s = signals.SignalQAMGrayCoded(32, 2**10, dtype=dt) ss = signals.SignalQAMGrayCoded.from_symbol_array(s) assert ss.dtype is s.dtype assert np.dtype(dt) is s.symbols.dtype assert np.dtype(dt) is s.coded_symbols.dtype
def test_SignalQAMGray_dtype(self, dt): s = signals.SignalQAMGrayCoded(32, 2**10, dtype=dt) assert s.dtype is np.dtype(dt) assert np.dtype(dt) is s.symbols.dtype assert np.dtype(dt) is s.coded_symbols.dtype
def test_phase_partition_16qam(self, ndim): s = signals.SignalQAMGrayCoded(16, 2**16, fb=20e9, nmodes=ndim) s2, ph = phaserec.phase_partition_16qam(s, 10) assert type(s2) is type(s)
def test_file_exits(self, lvl): tmpdir = tempfile.mkdtemp() fn = os.path.join(tmpdir, "tmp") sig = signals.SignalQAMGrayCoded(4, 2**12, nmodes=1) io.save_signal(fn, sig, lvl) assert os.path.isfile(fn)
def test_comp_freq_offset(self, ndim): s = signals.SignalQAMGrayCoded(16, 2**16, fb=20e9, nmodes=ndim) s2 = phaserec.comp_freq_offset(s, np.ones(ndim) * 1e6) assert type(s2) is type(s)
class TestSyncAndAdjust(object): s = signals.SignalQAMGrayCoded(16, 3 * 10**4, nmodes=1) d = np.diff(np.unique(s.symbols.real)).min() @pytest.mark.parametrize("N1, N2, adjust", [(None, 1000, "tx"), (None, 1000, "rx"), (1000, None, "tx"), (1000, None, "rx")]) def test_length(self, N1, N2, adjust): sig = self.s[0] N = self.s.shape[1] syms = self.s.symbols[0] tx, rx = ber_functions.sync_and_adjust(sig[:N1], sig[:N2], adjust=adjust) if N1 is None and adjust == "tx": assert (tx.shape[0] == N2) and (rx.shape[0] == N2) elif N2 is None and adjust == "rx": assert (tx.shape[0] == N1) and (rx.shape[0] == N1) else: assert (tx.shape[0] == N) and (rx.shape[0] == N) @pytest.mark.parametrize(("rx_longer", "adjust"), [ (True, "tx"), pytest.param(False, "tx", marks=pytest.mark.xfail( reason="to short array throws off offset find")), pytest.param(True, "rx", marks=pytest.mark.xfail( reason="to short array throws off offset find")), (False, 'rx'), (True, "tx"), (None, "rx"), (None, "tx"), ]) def test_slices(self, rx_longer, adjust): x = np.arange(1000.) xx = np.tile(x, 3) y = xx[110:1000 + 3 * 110] ym = xx[110:1000 - 3 * 110] y_equal = xx[110:1000 + 1 * 110] if rx_longer is None: tx = x rx = y_equal else: if adjust == "tx": if rx_longer: rx = y tx = x else: rx = ym tx = x elif adjust == "rx": if rx_longer: rx = x tx = ym else: rx = x tx = y tx, rx = ber_functions.sync_and_adjust(tx, rx, adjust=adjust) npt.assert_array_almost_equal(tx, rx) @pytest.mark.parametrize("N", [234, 1000, 2001]) @pytest.mark.parametrize("rx_longer", [True, False, None]) def test_slices_data_tx(self, N, rx_longer): s = signals.SignalQAMGrayCoded(4, 2**16)[0] ss = np.tile(s, 3) if rx_longer is None: y = ss[N:2**16 + N] elif rx_longer: y = ss[N:2**16 + 2 * N] else: y = ss[N:2**16 - 2 * N] tx, rx = ber_functions.sync_and_adjust(s, y, adjust="tx") npt.assert_array_almost_equal(tx, rx) @pytest.mark.parametrize("N", [234, 1000, 2001]) @pytest.mark.parametrize("tx_longer", [True, False, None]) def test_slices_data_rx(self, N, tx_longer): s = signals.SignalQAMGrayCoded(4, 2**16)[0] ss = np.tile(s, 3) if tx_longer is None: y = ss[N:2**16 + N] elif tx_longer: y = ss[N:2**16 + 2 * N] else: y = ss[N:2**16 - 2 * N] tx, rx = ber_functions.sync_and_adjust(y, s, adjust="rx") npt.assert_array_almost_equal(tx, rx) @pytest.mark.parametrize("rx_longer", [True, False, None]) @pytest.mark.parametrize("adjust", ['tx', 'rx']) def test_slices_length(self, rx_longer, adjust): x = np.arange(1000.) xx = np.tile(x, 3) y = xx[11:1000 + 3 * 11] y_equal = xx[11:1000 + 1 * 11] if rx_longer is None: tx = x rx = y_equal else: if adjust == "tx": if rx_longer: rx = y tx = x else: rx = x tx = y elif adjust == "rx": if rx_longer: rx = y tx = x else: rx = x tx = y tx, rx = ber_functions.sync_and_adjust(tx, rx, adjust=adjust) assert tx.shape == rx.shape @pytest.mark.parametrize("N0", [(None, 1000), (1000, None)]) @pytest.mark.parametrize("shiftN", [0, 43, 150, 800]) @pytest.mark.parametrize("adjust", ['rx', 'tx']) def test_length_with_shift(self, N0, shiftN, adjust): sig = self.s[0] N = self.s.shape[1] N1, N2 = N0 sign = np.roll(sig, shiftN) tx, rx = ber_functions.sync_and_adjust(sig[:N1], sign[:N2], adjust=adjust) if N1 is None and adjust is "tx": assert (tx.shape[0] == N2) and (rx.shape[0] == N2) elif N2 is None and adjust is "rx": assert (tx.shape[0] == N1) and (rx.shape[0] == N1) else: assert (tx.shape[0] == N) and (rx.shape[0] == N) @pytest.mark.parametrize("shiftN", [ np.random.randint(l * (2**15 - 1) // 2 + 1, (l + 1) * (2**15 - 1) // 2) for l in range(4) ] + [48630]) @pytest.mark.parametrize("adjust", ["tx", "rx"]) def test_flip(self, adjust, shiftN): Np = 2**15 - 1 N = 2 * Np s = signals.SignalQAMGrayCoded(16, N, bitclass=signals.PRBSBits) sig = s[0] syms = s.symbols[0] syms2 = np.roll(syms, shift=shiftN) tx, rx = ber_functions.sync_and_adjust(syms, syms2, adjust=adjust) npt.assert_allclose(tx, rx) tx, rx = ber_functions.sync_and_adjust(syms2, syms, adjust=adjust) npt.assert_allclose(tx, rx) @pytest.mark.parametrize("adjust", ["tx", "rx"]) @pytest.mark.parametrize( "tx_i, rx_i", list(zip(list(range(1, 4)) + 3 * [0], 3 * [0] + list(range(1, 4))))) @pytest.mark.parametrize("N1, N2", [(None, 2**15 - 1), (2**15 - 1, None)]) @pytest.mark.parametrize("shiftN", [ np.random.randint(l * (2**15 - 1) // 2 + 1, (l + 1) * (2**15 - 1) // 2) for l in range(4) ] + [48630]) def test_rotated_and_diff_length(self, adjust, tx_i, rx_i, N1, N2, shiftN): Np = 2**15 - 1 N = 2 * Np s = signals.SignalQAMGrayCoded(16, N, bitclass=signals.PRBSBits) sig = s[0] syms = s.symbols[0] syms2 = np.roll(syms, shift=shiftN) tx, rx = ber_functions.sync_and_adjust(syms[:N1] * 1j**tx_i, syms2[:N2] * 1j**rx_i, adjust=adjust) npt.assert_allclose(tx, rx) @pytest.mark.parametrize("N", [12342]) @pytest.mark.parametrize("plus", [True, False]) def test_ser_with_random_slice(self, N, plus): s = signals.SignalQAMGrayCoded(4, 2**17) ss = np.tile(s, 4) if plus: s2 = ss[0, N:2**17 + 3 * N] else: s2 = ss[0, N:2**17 - 3 * N] npt.assert_allclose(s2.cal_ser(), 0)
def test_bps_twostage(self, ndim): s = signals.SignalQAMGrayCoded(32, 2**16, fb=20e9, nmodes=ndim) s2, ph = cphaserecovery.bps_twostage(s, 32, s.coded_symbols, 10) assert ndim == s2.shape[0]
import matplotlib.pylab as plt from qampy import equalisation, signals, impairments, helpers, phaserec fb = 40.e9 os = 2 fs = os * fb N = 4 * 10**5 mu = 4e-4 theta = np.pi / 5.45 theta2 = np.pi / 4 t_pmd = 75e-12 M = 4 ntaps = 40 snr = 14 sig = signals.SignalQAMGrayCoded(M, N, fb=fb, nmodes=2, dtype=np.complex64) S = sig.resample(fs, renormalise=True, beta=0.1) S = impairments.apply_phase_noise(S, 100e3) S = impairments.change_snr(S, snr) SS = impairments.apply_PMD(S, theta, t_pmd) wxy, err = equalisation.equalise_signal(SS, mu, Ntaps=ntaps, TrSyms=None, method="mcma", adaptive_step=True) wxy_m, err_m = equalisation.equalise_signal(SS, mu, TrSyms=None, Ntaps=ntaps,
def test_find_freq_offset(self, ndim): s = signals.SignalQAMGrayCoded(16, 2**16, fb=20e9, nmodes=ndim) fo = cphaserecovery.find_freq_offset(s) assert ndim == fo.shape[0]
def test_quantize_precision(dtype, benchmark): s = signals.SignalQAMGrayCoded(128, 2**20, dtype=dtype) o = benchmark(cython_equalisation.make_decision, s[0], s.coded_symbols) npt.assert_array_almost_equal(s.symbols[0], o)
def test_viterbi_1d(self): s = signals.SignalQAMGrayCoded(4, 2**16, fb=20e9, nmodes=1) s2, ph = cphaserecovery.viterbiviterbi(s.flatten(), 10, 4) assert 2**16 == s2.shape[0]
from plotSetup import plot_setup_SNR, plot_setup_linewidth import pdb #Signal properties DACrate = 92.e9 #DAC Sampling frequency (fs) at AWG output M = 4 #M-QAM nmodes = 2 #Number of polarizations fb = 10 * 10**9 #Baud rate or Symbol rate (float) N = 2**18 / ( DACrate / fb ) #Number of symbols per polarization; This is set to be 2^18 symbols of data (2^18 Sa) after resampling. Nsc = 1 os = DACrate / (fb / Nsc) #Transmitter side sig = signals.SignalQAMGrayCoded(M, N, nmodes, fb) #Generating data Tx = sig.resample(DACrate, beta=0.1, renormalise=True) #DAC resampling shift = np.random.randint(-N / 2, N / 2, 1) Tx = delay(Tx, shift=0, nmodes=nmodes) #Shifting signal to simulate delay Tx = interpolate_signal(Tx) #Interpolating signal Tx = extend_signal(Tx, xtnd_w_zero=0) #Extending signal to fit 2^18 symbols export_signal(DACrate, Tx, M, N, nmodes, fb, compress=False) #Exporting transmitted signal into files #Receiver side Rx = import_signal(M, fb, Nsc, DACrate) #Importing signal resampled_sig = receiver_resample_signal(Rx) #Resampling signal ############################################# ## Setting up signal quality metrics snr = np.linspace(5, 16, 12)
def test_phase_partition_16qam(self): s = signals.SignalQAMGrayCoded(16, 2**16, fb=20e9, nmodes=1) s2, ph = cphaserecovery.phase_partition_16qam(s.flatten(), 10) assert 2**16 == s2.shape[0]
from functions import * import numpy as np from datetime import datetime import random print("\nStart, current time is " + datetime.now().strftime("%H:%M:%S")) if not USE_REAL_DATA: print( "Generating 2^{:d} random bits per polarization in {:d}-QAM format...". format(int(np.log2(AMOUNT_OF_SYMBOLS)), MODULATION_SIZE)) # Generate random signal and keep adding glass fiber impairments sig_original = signals.SignalQAMGrayCoded(MODULATION_SIZE, AMOUNT_OF_SYMBOLS, fb=SYMBOL_RATE, nmodes=2) sig = sig_original if PLOT_PICTURES: plot_constellation( sig, "Signal constellation without distortions\n $F_{symbol}=" + "{:d}GBd$, #symbols=2^{:d}".format(int( SYMBOL_RATE / 1e9), int(np.log2(AMOUNT_OF_SYMBOLS))), "sig") plot_time( sig, "X-polarization over time without\n distortions $F_{symbol}=" + "{:d}GBd$".format(int(SYMBOL_RATE / 1e9)), "sig_time") if USE_PULSESHAPING: print("Pulse-shaping the signal...")
os = 2 fs = os * fb N = 10**6 theta = np.pi / 4.6 M = 32 snr = 25 muCMA = 1e-3 muRDE = 1.e-3 ntaps = 11 t_pmd = 20.e-12 #Ncma = N//4//os -int(1.5*ntaps) Ncma = 10000 Nrde = N // 2 // os - int(1.5 * ntaps) #S, symbols, bits = QAM.generate_signal(N, snr, baudrate=fb, samplingrate=fs, PRBSorder=(15,23)) sig = signals.SignalQAMGrayCoded(M, N, fb=fb, nmodes=2) S = sig.resample(fs, beta=0.1, renormalise=True) S = impairments.change_snr(S, snr) SS = impairments.apply_PMD(S, theta, t_pmd) E, wx, (err, err_rde) = equalisation.dual_mode_equalisation(SS, (muCMA, muRDE), ntaps, methods=("mcma", "sbd"), adaptive=(True, True)) E = helpers.normalise_and_center(E) evm = E.cal_evm() evm_s = S[:, ::2].cal_evm() #sys.exit()
def test_bps_twostage(self, ndim): s = signals.SignalQAMGrayCoded(32, 2**16, fb=20e9, nmodes=ndim) s2, ph = phaserec.bps_twostage(s, 32, s.coded_symbols, 10) assert type(s2) is type(s)
def test_viterbi(self, ndim): s = signals.SignalQAMGrayCoded(4, 2 ** 16, fb=20e9, nmodes=ndim) s2, ph = phaserec.viterbiviterbi(s, 10) assert type(s2) is type(s)
def testavgpow(self, M): s = signals.SignalQAMGrayCoded(M, 2**18) p = (abs(s)**2).mean() npt.assert_almost_equal(p, 1, 2)