def test_rescale_signal_ndscale(self, nmodes): s = signals.SignalQAMGrayCoded(64, 2**12, nmodes=nmodes) s /= abs(s.max()) scale = np.random.randint(1, 4, nmodes) s2 = helpers.rescale_signal(s, scale) for i in range(nmodes): assert np.isclose(abs(s2[i].imag).max(), scale[i]) assert np.isclose(abs(s2[i].real).max(), scale[i])
def quantize_signal_New(sig_in, nbits=6, rescale_in=True, rescale_out=True): """ Function so simulate limited resultion using DACs and ADCs, limit quantization error to (-delta/2,delta/2) and set decision threshold at mid-point between two quantization levels. Parameters: sig_in: Input signal, numpy array, notice: input signal should be rescale to (-1,1) nbits: Quantization resolution rescale_in: Rescale input signal to (-1,1) rescale_out: Rescale output signal to (-input_max_swing,input_max_swing) Returns: sig_out: Output quantized waveform """ # 2**nbits interval within (-1,1), output swing is (-1+delta/2,1-delta/2) # Create a 2D signal sig_in = np.atleast_2d(sig_in) npols = sig_in.shape[0] # Rescale to sig = np.zeros((npols, sig_in.shape[1]), dtype=sig_in.dtype) if rescale_in: sig = rescale_signal(sig_in, swing=1) # Clipping exist if signal range is larger than (-1,1) swing = 2 delta = swing / 2**nbits levels_out = np.linspace(-1 + delta / 2, 1 - delta / 2, 2**nbits) levels_dec = levels_out + delta / 2 sig_out = np.zeros(sig.shape, dtype=sig_in.dtype) for pol in range(npols): sig_quant_re = levels_out[np.digitize(sig[pol].real, levels_dec[:-1], right=False)] sig_quant_im = levels_out[np.digitize(sig[pol].imag, levels_dec[:-1], right=False)] sig_out[pol] = sig_quant_re + 1j * sig_quant_im if not np.iscomplexobj(sig): sig_out = sig_out.real if rescale_out: max_swing = np.maximum(abs(sig_in.real).max(), abs(sig_in.imag).max()) sig_out = sig_out * max_swing return sig_in.recreate_from_np_array(sig_out)
def test_rescale_signal_altern(self, nmodes): s = signals.SignalQAMGrayCoded(64, 2**12, nmodes=nmodes) s /= abs(s.max()) scale = np.random.randint(1, 4, nmodes) for i in range(nmodes): if i % 2: s[i].imag *= scale[i] else: s[i].real *= scale[i] s2 = helpers.rescale_signal(s) for i in range(nmodes): if i % 2: assert np.isclose(abs(s2[i].imag).max(), 1) assert np.isclose(abs(s2[i].real).max(), 1 / scale[i]) else: assert np.isclose(abs(s2[i].real).max(), 1) assert np.isclose(abs(s2[i].imag).max(), 1 / scale[i])
def sim_DAC_response(sig, fs, enob=5, clip_rat=1, quant_bits=0, **dac_params): """ Function to simulate DAC response, including quantization noise (ENOB) and frequency response. Parameters ---------- sig: array_like Input signal fs: float Sampling frequency of the signal enob: float, optional Effective number of bits of the DAC (i.e. 6 bits.) modelled as AWGN. If enob=0 only quantize. If both enob and quant_bits are given, quantize first and then add enob noise. clip_rat: float, optional Ratio of signal left after clipping. (i.e. clip_rat=0.8 means 20% of the signal is clipped) (default 1: no clipping) quant_bits: float, optional Number of bits in the quantizer, only applied if not =0. (Default: don't qpply quantization) dac_params: dict, optional Parameters for the DAC response check apply_DAC_filter for the keyword parameters. If this is empty than do not apply the DAC response Returns ------- filter_sig: array_like Quantized, clipped and filtered output signal """ if np.isclose(clip_rat, 1): sig_clip = sig else: sig_res = rescale_signal(sig, 1 / clip_rat) sig_clip = clipper(sig_res, 1) if not np.isclose(quant_bits, 0): sig_clip = quantize_signal_New(sig_clip, nbits=quant_bits, rescale_in=True, rescale_out=True) if not np.isclose(enob, 0): sig_clip = apply_enob_as_awgn(sig_clip, enob) if dac_params: filter_sig = apply_DAC_filter(sig_clip, fs, **dac_params) else: filter_sig = sig_clip return filter_sig
def test_rescale_signal(self, nmodes): s = signals.SignalQAMGrayCoded(64, 2**12, nmodes=nmodes) s2 = helpers.rescale_signal(s, 100) assert s.shape == (s2.shape[0], s2.shape[1])
def test_rescale_signal(self): s = signals.SignalQAMGrayCoded(64, 2**12) s2 = helpers.rescale_signal(s, 100) assert type(s) is type(s2)