def test_derivatives(): x, y, vary = test_dat(multichannel=True) # ======================= # # xd = _np.linspace(0, 15, 100) # _plt.plot(x, y, "o") # p , e = curve_fit(_ms.piecewise_2line, x, y) # _plt.plot(xd, _ms.piecewise_2line(xd, *p)) # from model_spec import model_chieff as fmodel # # mn = 1 # model_number # np = 4 # npoly # yd, gd, info = fmodel(af=None, XX=xd, model_number=mn, npoly=np) # p0 = info.af # # p, e = fit_leastsq(p0, x, y, fmodel, (mn, np, 1) ) # _plt.plot(xd, fmodel(*p, XX=xd, model_number=mn, npoly=np, nargout=1) ) # yxp, yxvp, dydxp, vardydxp = spline_bs(x, y, vary, x, nmonti=30000, func="pchip") # yxp, yxvp, dydxp, vardydxp = spline_bs(x, y, vary, x, nmonti=30000, func="pchip") # yxp, yxvp, dydxp, vardydxp = spline_bs(x, y, vary, x, nmonti=300, func="spline") dydx, vardydx = deriv_bsgaussian(x, y, vary, axis=0, nmonti=300, sigma=0.8, mode='nearest') yxp, yxvp = deriv_bsgaussian(x, y, vary, axis=0, nmonti=300, sigma=0.8, mode='nearest', derivorder=0) _, _, yx, yxv = _ut.trapz_var(x, dydx, None, vardydx) yx += (y[0] - _ut.interp(x, yx, ei=None, xo=x[0])) # ==== # _plt.figure() ax1 = _plt.subplot(2,1,1) ax1.plot(x, y, "ko") # Integrals ax1.plot(x, yx, 'k-', x, yx+_np.sqrt(yxv), 'k--', x, yx-_np.sqrt(yxv), 'k--') ax1.plot(x, yxp, 'g-', x, yxp+_np.sqrt(yxvp), 'g--', x, yxp-_np.sqrt(yxvp), 'g--') # Derivatives ax2 = _plt.subplot(2,1,2, sharex=ax1) ax2.plot(x, dydx, 'k-', x, dydx+_np.sqrt(vardydx), 'k--', x, dydx-_np.sqrt(vardydx), 'k--')
def upsample(u_t, Fs, Fs_new, plotit=False): # Upsample a signal to a higher sampling rate # Use cubic interpolation to increase the sampling rate nt = len(u_t) tt = _np.arange(0, nt, 1) / Fs ti = _np.arange(tt[0], tt[-1], 1 / Fs_new) # _ut.interp(xi,yi,ei,xo) u_n = _ut.interp(tt, u_t, ei=None, xo=ti) # TODO!: Add quadratic interpolation # uinterp = interp1d(tt, u_t, kind='cubic', axis=0) # u_n = uinterp(ti) return u_n
def downsample(u_t, Fs, Fs_new, plotit=False): """ The proper way to downsample a signal. First low-pass filter the signal Interpolate / Decimate the signal down to the new sampling frequency """ tau = 2 / Fs_new nt = len(u_t) tt = _np.arange(0, nt, 1) / Fs # tt = tt.reshape(nt, 1) # tt = (_np.arange(0, 1/Fs, nt)).reshape(nt, 1) try: nch = _np.size(u_t, axis=1) except: nch = 1 u_t = u_t.reshape(nt, nch) # end try # ----------- # #2nd order LPF gets converted to a 4th order LPF by filtfilt lowpass_n, lowpass_d = _dsp.butter(2, 2.0 / (Fs * tau), btype='low') if plotit: # ------- # #Calculate the frequency response of the lowpass filter, w, h = _dsp.freqz(lowpass_n, lowpass_d, worN=12000) # #Convert to frequency vector from rad/sample w = (Fs / (2.0 * _np.pi)) * w # ------- # _plt.figure(num=3951) # _fig.clf() _ax1 = _plt.subplot(3, 1, 1) _ax1.plot(tt, u_t, 'k') _ax1.set_ylabel('Signal', color='k') _ax1.set_xlabel('t [s]') _ax2 = _plt.subplot(3, 1, 2) _ax2.plot(w, 20 * _np.log10(abs(h)), 'b') _ax2.plot(1.0 / tau, 0.5 * _np.sqrt(2), 'ko') _ax2.set_ylabel('|LPF| [dB]', color='b') _ax2.set_xlabel('Frequency [Hz]') _ax2.set_title('Digital LPF frequency response (Stage 1)') _plt.xscale('log') _plt.grid(which='both', axis='both') _plt.axvline(1.0 / tau, color='k') _plt.grid() _plt.axis('tight') # endif plotit # nskip = int(_np.round(Fs/Fs_new)) # ti = tt[0:nt:nskip] ti = _np.arange(0, nt / Fs, 1 / Fs_new) u_n = _np.zeros((len(ti), nch), dtype=_np.float64) for ii in range(nch): # (Non-Causal) LPF u_t[:, ii] = _dsp.filtfilt(lowpass_n, lowpass_d, u_t[:, ii]) # _ut.interp(xi,yi,ei,xo) u_n[:, ii] = _ut.interp(tt, u_t[:, ii], ei=None, xo=ti) # uinterp = interp1d(tt, u_t[:, ii], kind='cubic', axis=0) # u_n[:, ii] = uinterp(ti) #endif if plotit: _ax1.plot(ti, u_n, 'b-') _ax3 = _plt.subplot(3, 1, 3, sharex=_ax1) _ax3.plot(tt, u_t, 'k') _ax3.set_ylabel('Filt. Signal', color='k') _ax3.set_xlabel('t [s]') # _plt.show(hfig, block=False) _plt.draw() # _plt.show() # endif plotit return u_n
def test_derivatives(): x, y, vary = test_dat(multichannel=True) # ======================= # dydx0, vardydx0 = findiff1d(x, y, vary, order=1) dydx2, vardydx2 = findiff1d(x, y, vary, order=2) dydx4, vardydx4 = findiff1d(x, y, vary, order=4) # ndydx0, nvardydx0 = findiffnp(x, y, vary, order=1) ndydx2, nvardydx2 = findiffnp(x, y, vary, order=2) dydx5, vardydx5 = findiff1dr(x, y, vary) # integrate derivative and compare to source _, _, yx0, yxv0 = _ut.trapz_var(x, dydx0, None, vardydx0) yx0 += (y[0] - _ut.interp(x, yx0, ei=None, xo=x[0])) _, _, nyx0, nyxv0 = _ut.trapz_var(x, ndydx0, None, nvardydx0) nyx0 += (y[0] - _ut.interp(x, nyx0, ei=None, xo=x[0])) # 2nd order _, _, yx2, yxv2 = _ut.trapz_var(x, dydx2, None, vardydx2) # _, _, yx2, yxv2 = _ut.trapz_var(x, yx2, None, yxv2) yx2 += (y[0] - _ut.interp(x, yx2, ei=None, xo=x[0])) _, _, nyx2, nyxv2 = _ut.trapz_var(x, ndydx2, None, nvardydx2) # _, _, nyx2, nyxv2 = _ut.trapz_var(x, nyx2, None, nyxv2) nyx2 += (y[0] - _ut.interp(x, nyx2, ei=None, xo=x[0])) # 4th order _, _, yx4, yxv4 = _ut.trapz_var(x, dydx4, None, vardydx4) yx4 += (y[0] - _ut.interp(x, yx4, ei=None, xo=x[0])) # Cylinrdical _, _, yx5, yxv5 = _ut.trapz_var(x, dydx5, None, vardydx5) yx5 += (y[0] - _ut.interp(x, yx5, ei=None, xo=x[0])) # ==== # _plt.figure() ax1 = _plt.subplot(2, 1, 1) ax1.plot(x, y, "ko") # Integrals ax1.plot(x, yx0, 'r-', x, yx0 + _np.sqrt(yxv0), 'r--', x, yx0 - _np.sqrt(yxv0), 'r--') ax1.plot(x, nyx0, 'b-', x, nyx0 + _np.sqrt(nyxv0), 'b--', x, nyx0 - _np.sqrt(nyxv0), 'b--') ax1.plot(x, yx2, 'g-', x, yx2 + _np.sqrt(yxv2), 'g--', x, yx2 - _np.sqrt(yxv2), 'g--') ax1.plot(x, nyx2, 'm-', x, nyx2 + _np.sqrt(nyxv2), 'm--', x, nyx2 - _np.sqrt(nyxv2), 'm--') ax1.plot(x, yx4, 'y-', x, yx4 + _np.sqrt(yxv4), 'y--', x, yx4 - _np.sqrt(yxv4), 'y--') ax1.plot(x, yx5, 'g.-', x, yx5 + _np.sqrt(yxv5), 'g.-', x, yx5 - _np.sqrt(yxv5), 'g.-') # Derivatives ax2 = _plt.subplot(2, 1, 2) ax2.plot(x, dydx0, 'r-', x, dydx0 + _np.sqrt(vardydx0), 'r--', x, dydx0 - _np.sqrt(vardydx0), 'r--') ax2.plot(x, ndydx0, 'b-', x, ndydx0 + _np.sqrt(nvardydx0), 'b--', x, ndydx0 - _np.sqrt(nvardydx0), 'b--') ax2.plot(x, dydx2, 'g-', x, dydx2 + _np.sqrt(vardydx2), 'g--', x, dydx2 - _np.sqrt(vardydx2), 'g--') ax2.plot(x, ndydx2, 'm-', x, ndydx2 + _np.sqrt(nvardydx2), 'm--', x, ndydx2 - _np.sqrt(nvardydx2), 'm--') ax2.plot(x, dydx4, 'y-', x, dydx4 + _np.sqrt(vardydx4), 'y--', x, dydx4 - _np.sqrt(vardydx4), 'y--') ax2.plot(x, dydx5, 'g.-', x, dydx5 + _np.sqrt(vardydx5), 'g.-', x, dydx5 - _np.sqrt(vardydx5), 'g.-')
def fit_profile(roa, ne, varne, rvec, loggradient=True, derivfunc=None): if derivfunc is None: derivfunc = findiffnp # endif # ================= # # Preconditioning # # ================= # xfit = _ut.cylsym_odd(roa) if loggradient: yfit = _ut.cylsym_even(_np.log(ne)) vfit = _ut.cylsym_even(varne / (ne**2.0)) else: yfit = _ut.cylsym_even(ne) vfit = _ut.cylsym_even(varne) # endif ne = _np.atleast_2d(ne) roa = _np.atleast_2d(roa) rvec = _np.atleast_2d(rvec) xfit = _np.atleast_2d(xfit) yfit = _np.atleast_2d(yfit) vfit = _np.atleast_2d(vfit) if _np.size(ne, axis=0) == 1: ne = ne.T # endif if _np.size(roa, axis=0) == 1: roa = roa.T # endif if _np.size(rvec, axis=0) == 1: rvec = rvec.T # endif if _np.size(xfit, axis=0) == 1: xfit = xfit.T # endif if _np.size(yfit, axis=0) == 1: yfit = yfit.T # endif if _np.size(vfit, axis=0) == 1: vfit = vfit.T # endif ysh = _np.shape(yfit) if _np.shape(yfit) != _np.shape(xfit): xfit = _np.tile(xfit, ysh[1]) if _np.size(rvec, axis=1) != _np.size(yfit, axis=1): rvec = _np.tile(rvec, ysh[1]) if _np.size(roa, axis=1) != _np.size(ne, axis=1): roa = _np.tile(roa, ysh[1]) # endif # =============== # dyfdr = _np.zeros((len(rvec), ysh[1]), dtype=_np.float64) vardyfdr = _np.zeros_like(dyfdr) yf = _np.zeros_like(dyfdr) varyf = _np.zeros_like(vardyfdr) for ii in range(_np.size(dyfdr, axis=1)): # if ii == 1: # print('what') # # endif dydr, vardydr = derivfunc(xfit[:, ii], yfit[:, ii], vfit[:, ii]) # dydr, vardydr = deriv_bsgaussian(xfit[:,ii], yfit[:,ii], vfit[:,ii]) dyfdr[:, ii], vardyfdr[:, ii] = _ut.interp(xfit[:, ii], dydr, _np.sqrt(vardydr), rvec[:, ii]) _, _, yf[:, ii], varyf[:, ii] = _ut.trapz_var(rvec[:, ii], dyfdr[:, ii], None, vardyfdr[:, ii], dim=0) yf[:, ii] += (ne[0, ii] - _ut.interp(rvec[:, ii], yf[:, ii], None, roa[0, ii])) # endfor if _np.size(yf, axis=1) == 1: yf = yf.reshape(len(rvec), ) dyfdr = dyfdr.reshape(len(rvec), ) varyf = varyf.reshape(len(rvec), ) vardyfdr = vardyfdr.reshape(len(rvec), ) # end if return yf, dyfdr, varyf, vardyfdr
def interp_profile(roa, ne, varne, rvec, loggradient=True, **kwargs): # ================= # # Preconditioning # # ================= # roa = roa.copy() ne = ne.copy() varne = varne.copy() rvec = rvec.copy() if roa[0] == 0.0: xfit = _np.concatenate((-_np.flipud(roa), roa[1:])) yfit = _np.concatenate((_np.flipud(ne), ne[1:])) vfit = _np.concatenate((_np.flipud(varne), varne[1:])) else: xfit = _ut.cylsym_odd(roa) yfit = _ut.cylsym_even(ne) vfit = _ut.cylsym_even(varne) # end if if loggradient: yfit = _np.log(ne) vfit = varne / (ne**2.0) # endif ne = _np.atleast_2d(ne) roa = _np.atleast_2d(roa) rvec = _np.atleast_2d(rvec) xfit = _np.atleast_2d(xfit) yfit = _np.atleast_2d(yfit) vfit = _np.atleast_2d(vfit) if _np.size(ne, axis=0) == 1: ne = ne.T # endif if _np.size(roa, axis=0) == 1: roa = roa.T # endif if _np.size(rvec, axis=0) == 1: rvec = rvec.T # endif if _np.size(xfit, axis=0) == 1: xfit = xfit.T # endif if _np.size(yfit, axis=0) == 1: yfit = yfit.T # endif if _np.size(vfit, axis=0) == 1: vfit = vfit.T # endif ysh = _np.shape(yfit) if _np.shape(yfit) != _np.shape(xfit): xfit = _np.tile(xfit, ysh[1]) if _np.size(rvec, axis=1) != _np.size(yfit, axis=1): rvec = _np.tile(rvec, ysh[1]) if _np.size(roa, axis=1) != _np.size(ne, axis=1): roa = _np.tile(roa, ysh[1]) # endif # =============== # dyfdr = _np.zeros((len(rvec), ysh[1]), dtype=_np.float64) vardyfdr = _np.zeros_like(dyfdr) yf = _np.zeros_like(dyfdr) varyf = _np.zeros_like(vardyfdr) for ii in range(_np.size(dyfdr, axis=1)): y, vy = deriv_bsgaussian(xfit[:, ii], yfit[:, ii], vfit[:, ii], derivorder=0, **kwargs) dydr, vardydr = deriv_bsgaussian(xfit[:, ii], yfit[:, ii], vfit[:, ii], derivorder=1, **kwargs) yf[:, ii], varyf[:, ii] = _ut.interp(xfit[:, ii], y, _np.sqrt(vy), rvec[:, ii]) dyfdr[:, ii], vardyfdr[:, ii] = _ut.interp(xfit[:, ii], dydr, _np.sqrt(vardydr), rvec[:, ii]) # _, _, yf[:,ii], varyf[:,ii] = _ut.trapz_var1d(rvec[:,ii], dyfdr[:,ii], None, vardyfdr[:,ii]) ## _, _, yf[:,ii], varyf[:,ii] = _ut.trapz_var(rvec[:,ii], dyfdr[:,ii], None, vardyfdr[:,ii], dim=0) # # yf[:,ii] += (ne[0,ii]-_ut.interp(rvec[:,ii], yf[:,ii], None, roa[0,ii])) # endfor if _np.size(yf, axis=1) == 1: yf = yf.reshape(len(rvec), ) dyfdr = dyfdr.reshape(len(rvec), ) varyf = varyf.reshape(len(rvec), ) vardyfdr = vardyfdr.reshape(len(rvec), ) # end if return yf, dyfdr, varyf, vardyfdr