def test_default(self): f = resource_stream('pydsm.delsig', 'tests/Data/test_simulateDSM_0.npz') d = np.load(f)['arr_0'] f.close() # Take H as in H = synthesizeNTF(5, 32, 1) H = (np.array([ 0.99604531 + 0.08884669j, 0.99604531 - 0.08884669j, 0.99860302 + 0.05283948j, 0.99860302 - 0.05283948j, 1.00000000 + 0.j ]), np.array([ 0.80655696 + 0.11982271j, 0.80655696 - 0.11982271j, 0.89807098 + 0.21981939j, 0.89807098 - 0.21981939j, 0.77776708 + 0.j ]), 1) N = 8192 f = 85 u = 0.5 * np.sin(2. * np.pi * f / N * np.arange(N)) v, d1, d2, d3 = simulateDSM(u, H) np.testing.assert_equal(v, d)
# Generate signals for time domain simulations print("Generate input signals ", end=" ") s1 = A1 * np.sin(2 * np.pi * f1 * tt / fphi) print("* ", end="") s2 = A2 * np.sin(2 * np.pi * f2 * tt / fphi) print("* ", end="") clk = 2 * (tt % 2) - 1 print("# ", end="") s2r = s2 * clk print("*") dither_sigma = 1e-9 # Use a little dither dither = np.random.randn(len(tt)) * dither_sigma # Do the simulation print("Simulating DSM", end=" ") xx1 = simulateDSM(s1 + s2r + dither, ntf_dual_a)[0] print("* ", end=""), xx2 = xx1 * clk print("*") # Plot a fragment of the modulator output - same as Fig. 5c plt.figure() plt.plot(tt[100:150], xx1[100:150], linestyle='steps') plt.xlabel(r'$n$', x=1.) plt.ylabel(r'$x(nT)$', x=1.) plt.ylim(-1.2, 1.2) plt.suptitle('Fragment of modulator output - 1st signal') # Reconstruct the signals print("Applying a low pass filter") yy1 = signal.lfilter(hz[0], hz[1], xx1)
# amplitudes # Plot in paper obtained with # c1=0 c2=0 c3=0.11111*np.sin(2*np.pi*fb*nn/fphi)*1E-3 # # Constant component c1 = 0.11111*0 # Slow component c2 = 0*np.sin(2*np.pi*fa*nn/fphi)*1E-3 # Fast component c3 = 0.11111*np.sin(2*np.pi*fb*nn/fphi)*1E-3 # dither dither = 2.5E-6*randn(tmax+1) sig = c1+c2+c3+dither print("Simulating DSM") ds1 = simulateDSM(sig, ntf1)[0] ds2 = simulateDSM(sig, ntf2)[0] ndt = tplot2-tplot0+1 dcval1 = np.sum(ds1[tplot0:tplot2])/ndt dcval2 = np.sum(ds2[tplot0:tplot2])/ndt print('Accuracy in the recovery of DC components') print('recovered DC on-off', dcval1) print('recovered DC low-dc-noise', dcval2) print('original DC', np.sum(sig[tplot0:tplot2])/ndt) print("Computing spectra") NFFT = int(2**(np.ceil(np.log(nfft_periods*1./fa*fphi)/np.log(2)))) # This is a bit rough... there may be some residual power at the # signal frequencies. To obtain a good plot make c1, c2, c3 very small (psd1, freqs) = mlab.psd((ds1-sig)[tplot0:tplot2], Fs=2*fphi, NFFT=NFFT,
print("{:.4e}\t{:.4e}\t{:.4e}\t{:.4e}".format(n_pow_06_0043, n_pow_06_02, n_pow_06_06, n_pow_06_delsig)) print("{:2.2f}dB\t{:2.2f}dB\t{:2.2f}dB\t{:2.2f}dB".format( dbp(sig_pow_06 / n_pow_06_0043), dbp(sig_pow_06 / n_pow_06_02), dbp(sig_pow_06 / n_pow_06_06), dbp(sig_pow_06 / n_pow_06_delsig))) # Repeating the tests in the time domain with the nonlinear modulator model # and the linearized motor model # Setting up DS simulation tt = np.asarray(list(range(int(Tstop)))) uu = A * np.sin(2 * np.pi * fmot / fphi * tt) dither = np.random.randn(len(uu)) * dither_sigma uud = uu + dither # Run the simulations print("Simulating modulator with NTF optimized for sigma=0.043") xx_opti0043 = simulateDSM(uud, ntf0043)[0] print("Simulating modulator with NTF optimized for sigma=0.2") xx_opti02 = simulateDSM(uud, ntf02)[0] print("Simulating modulator with NTF optimized for sigma=0.6") xx_opti06 = simulateDSM(uud, ntf06)[0] print("Simulating modulator with benchmark NTF") xx_delsig = simulateDSM(uud, delsig_ntf)[0] print("Applying the filters from the linearized motor model") print("Using the transfer function corresponding to sigma=0.043") uud_filt0043 = sp.signal.lfilter(hz0043[0], hz0043[1], uud) xx_opti_filt0043_0043 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_opti0043) xx_opti_filt0043_02 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_opti02) xx_opti_filt0043_06 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_opti06) xx_delsig_filt0043 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_delsig) print("Using the filter for sigma=0.2") uud_filt02 = sp.signal.lfilter(hz02[0], hz02[1], uud)
dbp(0.5*A1**2+0.5*A2**2)-dbp(noise_power_opti_1))) # Start and stop time for DS simulation Tstop = 100E3 Tstart = 40E3 dither_sigma = 1e-6 # Set up DSM simulation tt = np.asarray(range(int(Tstop))) uu = A1*np.sin(2*np.pi*fsig1/fphi*tt)+A2*np.sin(2*np.pi*fsig2/fphi*tt) dither = np.random.randn(len(uu))*dither_sigma uud = uu+dither # Simulate the DSM print("Simulating optimal NTF") xx_opti = simulateDSM(uud, ntf_opti)[0] print("Applying the reconstrution filter") hz_ab = sp.signal.zpk2tf(*hz) uu_filt = sp.signal.lfilter(hz_ab[0], hz_ab[1], uu) xx_opti_filt = sp.signal.lfilter(hz_ab[0], hz_ab[1], xx_opti) plt.figure() plt.hold(True) plt.plot(tt[Tstart:Tstart+4*OSR], uu_filt[Tstart:Tstart+4*OSR], 'b', label="Filtered input") plt.plot(tt[Tstart:Tstart+4*OSR], xx_opti_filt[Tstart:Tstart+4*OSR], 'r', label="Filtered Optimal DSM output") plt.legend(loc="best") plt.suptitle("Portion of time domain behavior")
print("Test time:", len(tt), 'points') # Generate signals for time domain simulations print("Generate input signals ", end="") s1 = A1*np.sin(2*np.pi*f1*tt/fphi) print("* ", end="") s2 = A2*np.sin(2*np.pi*f2*tt/fphi) print("* ", end="") clk = 2*(tt % 2)-1 print("*") dither_sigma = 1e-9 # Use a little dither dither = np.random.randn(len(tt))*dither_sigma # Do the simulation print("Simulating DSM ", end="") xx1 = simulateDSM(s1+dither, ntf_a)[0] print("* ", end="") xx2 = simulateDSM(s2+dither, ntf_a)[0] print("*") # Plot a fragment of the modulator output - same as Fig. 5c plt.figure() plt.plot(tt[100:150], xx1[100:150], linestyle='steps') plt.xlabel(r'$n$', x=1.) plt.ylabel(r'$x(nT)$', x=1.) plt.ylim(-1.2, 1.2) plt.suptitle('Fragment of modulator output - 1st signal') # Reconstruct the signals print("Applying a low pass filter") yy1 = signal.lfilter(hz[0], hz[1], xx1)
print("Test time:", len(tt), 'points') # Generate signals for time domain simulations print("Generate input signals ", end="") s1 = A1 * np.sin(2 * np.pi * f1 * tt / fphi) print("* ", end="") s2 = A2 * np.sin(2 * np.pi * f2 * tt / fphi) print("* ", end="") clk = 2 * (tt % 2) - 1 print("*") dither_sigma = 1e-9 # Use a little dither dither = np.random.randn(len(tt)) * dither_sigma # Do the simulation print("Simulating DSM ", end="") xx1 = simulateDSM(s1 + dither, ntf_a)[0] print("* ", end="") xx2 = simulateDSM(s2 + dither, ntf_a)[0] print("*") # Plot a fragment of the modulator output - same as Fig. 5c plt.figure() plt.plot(tt[100:150], xx1[100:150], linestyle='steps') plt.xlabel(r'$n$', x=1.) plt.ylabel(r'$x(nT)$', x=1.) plt.ylim(-1.2, 1.2) plt.suptitle('Fragment of modulator output - 1st signal') # Reconstruct the signals print("Applying a low pass filter") yy1 = signal.lfilter(hz[0], hz[1], xx1)
dither_sigma = 0 # Use very little signals, otherwise it is troublesome to # remove them completely to evaluate the quantization noise A = 0.01 DT = 4 * fphi / fsig # Setting up DS simulation tt = np.asarray(list(range(int(Tstop)))) uu = A * np.sin(2 * np.pi * fsig / fphi * tt) dither = np.random.randn(len(uu)) * dither_sigma uud = uu + dither # Simulating the DS # Some of the commented bits are used to obtain the quantizazion noise pdf print("Simulating optimal NTF") xx_opti = simulateDSM(uud, opti_ntf)[0] xx_ref = simulateDSM(uud, dunn_ntf)[0] NFFT = 4096 * 32 # Possibly this can be improved by extracting the tone from the # output to do the subtraction (psd1, freqs) = mlab.psd((xx_opti - uu)[Tstart:Tstop], Fs=fphi, NFFT=NFFT, noverlap=NFFT / 2, scale_by_freq=True) (psd2, reqs) = mlab.psd((xx_ref - uu)[Tstart:Tstop], Fs=fphi, NFFT=NFFT, noverlap=NFFT / 2, scale_by_freq=True)
dbp(0.5*A**2)-dbp(noise_power_delsig_1))) # Start and stop time for DS simulation Tstop = 100E3 Tstart = 40E3 dither_sigma = 1e-6 # Set up DSM simulation tt = np.asarray(range(int(Tstop))) uu = A*np.sin(2*np.pi*fsig/fphi*tt) dither = np.random.randn(len(uu))*dither_sigma uud = uu+dither # Simulate the DSM print("Simulating optimal NTF") xx_opti = simulateDSM(uud, ntf_opti)[0] print("Simulating delsig NTF") xx_delsig = simulateDSM(uud, ntf_delsig)[0] print("Applying the output filter") uu_filt = sp.signal.lfilter(hz[0], hz[1], uu) xx_opti_filt = sp.signal.lfilter(hz[0], hz[1], xx_opti) xx_delsig_filt = sp.signal.lfilter(hz[0], hz[1], xx_delsig) plt.figure() plt.plot(tt[Tstart:Tstart+4*OSR], uu_filt[Tstart:Tstart+4*OSR], 'b', label="Filtered input") plt.plot(tt[Tstart:Tstart+4*OSR], xx_opti_filt[Tstart:Tstart+4*OSR], 'r', label="Filtered Optimal DSM output") plt.plot(tt[Tstart:Tstart+4*OSR], xx_delsig_filt[Tstart:Tstart+4*OSR], 'g', label="Filtered Delsig DSM output")
dbp(0.5 * A**2) - dbp(noise_power_delsig_1))) # Start and stop time for DS simulation Tstop = 100E3 Tstart = 40E3 dither_sigma = 1e-6 # Set up DSM simulation tt = np.asarray(list(range(int(Tstop)))) uu = A * np.sin(2 * np.pi * fsig / fphi * tt) dither = np.random.randn(len(uu)) * dither_sigma uud = uu + dither # Simulate the DSM print("Simulating optimal NTF") xx_opti = simulateDSM(uud, ntf_opti)[0] print("Simulating delsig NTF") xx_delsig = simulateDSM(uud, ntf_delsig)[0] print("Applying the output filter") uu_filt = sp.signal.lfilter(hz[0], hz[1], uu) xx_opti_filt = sp.signal.lfilter(hz[0], hz[1], xx_opti) xx_delsig_filt = sp.signal.lfilter(hz[0], hz[1], xx_delsig) plt.figure() plt.plot(tt[Tstart:Tstart + 4 * OSR], uu_filt[Tstart:Tstart + 4 * OSR], 'b', label="Filtered input") plt.plot(tt[Tstart:Tstart + 4 * OSR], xx_opti_filt[Tstart:Tstart + 4 * OSR],
# Generate signals for time domain simulations print("Generate input signals ", end=" ") s1 = A1*np.sin(2*np.pi*f1*tt/fphi) print("* ", end="") s2 = A2*np.sin(2*np.pi*f2*tt/fphi) print("* ", end="") clk = 2*(tt % 2)-1 print("# ", end="") s2r = s2*clk print("*") dither_sigma = 1e-9 # Use a little dither dither = np.random.randn(len(tt))*dither_sigma # Do the simulation print("Simulating DSM", end=" ") xx1 = simulateDSM(s1+s2r+dither, ntf_dual_a)[0] print("* ", end=""), xx2 = xx1*clk print("*") # Plot a fragment of the modulator output - same as Fig. 5c plt.figure() plt.plot(tt[100:150], xx1[100:150], linestyle='steps') plt.xlabel(r'$n$', x=1.) plt.ylabel(r'$x(nT)$', x=1.) plt.ylim(-1.2, 1.2) plt.suptitle('Fragment of modulator output - 1st signal') # Reconstruct the signals print("Applying a low pass filter") yy1 = signal.lfilter(hz[0], hz[1], xx1)
# amplitudes # Plot in paper obtained with # c1=0 c2=0 c3=0.11111*np.sin(2*np.pi*fb*nn/fphi)*1E-3 # # Constant component c1 = 0.11111 * 0 # Slow component c2 = 0 * np.sin(2 * np.pi * fa * nn / fphi) * 1E-3 # Fast component c3 = 0.11111 * np.sin(2 * np.pi * fb * nn / fphi) * 1E-3 # dither dither = 2.5E-6 * randn(tmax + 1) sig = c1 + c2 + c3 + dither print("Simulating DSM") ds1 = simulateDSM(sig, ntf1)[0] ds2 = simulateDSM(sig, ntf2)[0] ndt = tplot2 - tplot0 + 1 dcval1 = np.sum(ds1[tplot0:tplot2]) / ndt dcval2 = np.sum(ds2[tplot0:tplot2]) / ndt print('Accuracy in the recovery of DC components') print('recovered DC on-off', dcval1) print('recovered DC low-dc-noise', dcval2) print('original DC', np.sum(sig[tplot0:tplot2]) / ndt) print("Computing spectra") NFFT = int(2**(np.ceil(np.log(nfft_periods * 1. / fa * fphi) / np.log(2)))) # This is a bit rough... there may be some residual power at the # signal frequencies. To obtain a good plot make c1, c2, c3 very small (psd1, freqs) = mlab.psd((ds1 - sig)[tplot0:tplot2],
print("{:.4e}\t{:.4e}\t{:.4e}\t{:.4e}".format( n_pow_06_0043, n_pow_06_02, n_pow_06_06, n_pow_06_delsig)) print("{:2.2f}dB\t{:2.2f}dB\t{:2.2f}dB\t{:2.2f}dB".format( dbp(sig_pow_06/n_pow_06_0043), dbp(sig_pow_06/n_pow_06_02), dbp(sig_pow_06/n_pow_06_06), dbp(sig_pow_06/n_pow_06_delsig))) # Repeating the tests in the time domain with the nonlinear modulator model # and the linearized motor model # Setting up DS simulation tt = np.asarray(range(int(Tstop))) uu = A*np.sin(2*np.pi*fmot/fphi*tt) dither = np.random.randn(len(uu))*dither_sigma uud = uu+dither # Run the simulations print("Simulating modulator with NTF optimized for sigma=0.043") xx_opti0043 = simulateDSM(uud, ntf0043)[0] print("Simulating modulator with NTF optimized for sigma=0.2") xx_opti02 = simulateDSM(uud, ntf02)[0] print("Simulating modulator with NTF optimized for sigma=0.6") xx_opti06 = simulateDSM(uud, ntf06)[0] print("Simulating modulator with benchmark NTF") xx_delsig = simulateDSM(uud, delsig_ntf)[0] print("Applying the filters from the linearized motor model") print("Using the transfer function corresponding to sigma=0.043") uud_filt0043 = sp.signal.lfilter(hz0043[0], hz0043[1], uud) xx_opti_filt0043_0043 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_opti0043) xx_opti_filt0043_02 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_opti02) xx_opti_filt0043_06 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_opti06) xx_delsig_filt0043 = sp.signal.lfilter(hz0043[0], hz0043[1], xx_delsig) print("Using the filter for sigma=0.2") uud_filt02 = sp.signal.lfilter(hz02[0], hz02[1], uud)
dbp(0.5*A1**2+0.5*A2**2)-dbp(noise_power_opti_1))) # Start and stop time for DS simulation Tstop = 100E3 Tstart = 40E3 dither_sigma = 1e-6 # Set up DSM simulation tt = np.asarray(list(range(int(Tstop)))) uu = A1*np.sin(2*np.pi*fsig1/fphi*tt)+A2*np.sin(2*np.pi*fsig2/fphi*tt) dither = np.random.randn(len(uu))*dither_sigma uud = uu+dither # Simulate the DSM print("Simulating optimal NTF") xx_opti = simulateDSM(uud, ntf_opti)[0] print("Applying the reconstrution filter") hz_ab = sp.signal.zpk2tf(*hz) uu_filt = sp.signal.lfilter(hz_ab[0], hz_ab[1], uu) xx_opti_filt = sp.signal.lfilter(hz_ab[0], hz_ab[1], xx_opti) plt.figure() plt.hold(True) plt.plot(tt[Tstart:Tstart+4*OSR], uu_filt[Tstart:Tstart+4*OSR], 'b', label="Filtered input") plt.plot(tt[Tstart:Tstart+4*OSR], xx_opti_filt[Tstart:Tstart+4*OSR], 'r', label="Filtered Optimal DSM output") plt.legend(loc="best") plt.suptitle("Portion of time domain behavior")