def demo_kalman_voltimeter(): """ Examples -------- >>> demo_kalman_voltimeter() >>> plt.close() """ V0 = 12 h = np.atleast_2d(1) # voltimeter measure the voltage itself q = 1e-9 # variance of process noise as the car operates r = 0.05**2 # variance of measurement error b = 0 # no system input u = 0 # no system input filt = Kalman(R=r, A=1, Q=q, H=h, B=b) # Generate random voltages and watch the filter operate. n = 50 truth = np.random.randn(n) * np.sqrt(q) + V0 z = truth + np.random.randn(n) * np.sqrt(r) # measurement x = np.zeros(n) for i, zi in enumerate(z): x[i] = filt(zi, u) # perform a Kalman filter iteration _hz = plt.plot(z, 'r.', label='observations') # a-posteriori state estimates: _hx = plt.plot(x, 'b-', label='Kalman output') _ht = plt.plot(truth, 'g-', label='true voltage') plt.legend() plt.title('Automobile Voltimeter Example')
def test_kalman(): V0 = 12 h = np.atleast_2d(1) # voltimeter measure the voltage itself q = 1e-9 # variance of process noise as the car operates r = 0.05 ** 2 # variance of measurement error b = 0 # no system input u = 0 # no system input filt = Kalman(R=r, A=1, Q=q, H=h, B=b) # Generate random voltages and watch the filter operate. n = 50 truth = np.random.randn(n) * np.sqrt(q) + V0 z = truth + np.random.randn(n) * np.sqrt(r) # measurement x = np.zeros(n) for i, zi in enumerate(z): x[i] = filt(zi, u) # perform a Kalman filter iteration _hz = plt.plot(z, 'r.', label='observations') # a-posteriori state estimates: _hx = plt.plot(x, 'b-', label='Kalman output') _ht = plt.plot(truth, 'g-', label='true voltage') plt.legend() plt.title('Automobile Voltimeter Example') plt.show('hold')
def demo_kalman_sine(): """Kalman Filter demonstration with sine signal. Examples -------- >>> demo_kalman_sine() >>> plt.close() """ sd = 0.5 dt = 0.1 w = 1 T = np.arange(0, 30 + dt / 2, dt) n = len(T) X = 3 * np.sin(w * T) Y = X + sd * np.random.randn(n) ''' Initialize KF to values x = 0 dx/dt = 0 with great uncertainty in derivative ''' M = np.zeros((2, 1)) P = np.diag([0.1, 2]) R = sd**2 H = np.atleast_2d([1, 0]) q = 0.1 F = np.atleast_2d([[0, 1], [0, 0]]) A, Q = lti_disc(F, L=None, Q=np.diag([0, q]), dt=dt) # Track and animate m = M.shape[0] _MM = np.zeros((m, n)) _PP = np.zeros((m, m, n)) '''In this demonstration we estimate a stationary sine signal from noisy measurements by using the classical Kalman filter.' ''' filt = Kalman(R=R, x=M, P=P, A=A, Q=Q, H=H, B=0) # Generate random voltages and watch the filter operate. # n = 50 # truth = np.random.randn(n) * np.sqrt(q) + V0 # z = truth + np.random.randn(n) * np.sqrt(r) # measurement truth = X z = Y x = np.zeros((n, m)) for i, zi in enumerate(z): x[i] = np.ravel(filt(zi, u=0)) _hz = plt.plot(z, 'r.', label='observations') # a-posteriori state estimates: _hx = plt.plot(x[:, 0], 'b-', label='Kalman output') _ht = plt.plot(truth, 'g-', label='true voltage') plt.legend() plt.title('Automobile Voltimeter Example')
def test_smoothn_1d(): x = np.linspace(0, 100, 2 ** 8) y = np.cos(x / 10) + (x / 50) ** 2 + np.random.randn(x.size) / 10 y[np.r_[70, 75, 80]] = np.array([5.5, 5, 6]) z = smoothn(y) # Regular smoothing zr = smoothn(y, robust=True) # Robust smoothing plt.subplot(121), unused_h = plt.plot(x, y, 'r.', x, z, 'k', linewidth=2) plt.title('Regular smoothing') plt.subplot(122) plt.plot(x, y, 'r.', x, zr, 'k', linewidth=2) plt.title('Robust smoothing') plt.show('hold')
def test_kalman_sine(): """Kalman Filter demonstration with sine signal.""" sd = 1. dt = 0.1 w = 1 T = np.arange(0, 30 + dt / 2, dt) n = len(T) X = np.sin(w * T) Y = X + sd * np.random.randn(n) ''' Initialize KF to values x = 0 dx/dt = 0 with great uncertainty in derivative ''' M = np.zeros((2, 1)) P = np.diag([0.1, 2]) R = sd ** 2 H = np.atleast_2d([1, 0]) q = 0.1 F = np.atleast_2d([[0, 1], [0, 0]]) A, Q = lti_disc(F, L=None, Q=np.diag([0, q]), dt=dt) # Track and animate m = M.shape[0] _MM = np.zeros((m, n)) _PP = np.zeros((m, m, n)) '''In this demonstration we estimate a stationary sine signal from noisy measurements by using the classical Kalman filter.' ''' filt = Kalman(R=R, x=M, P=P, A=A, Q=Q, H=H, B=0) # Generate random voltages and watch the filter operate. # n = 50 # truth = np.random.randn(n) * np.sqrt(q) + V0 # z = truth + np.random.randn(n) * np.sqrt(r) # measurement truth = X z = Y x = np.zeros((n, m)) for i, zi in enumerate(z): x[i] = filt(zi, u=0).ravel() _hz = plt.plot(z, 'r.', label='observations') # a-posteriori state estimates: _hx = plt.plot(x[:, 0], 'b-', label='Kalman output') _ht = plt.plot(truth, 'g-', label='true voltage') plt.legend() plt.title('Automobile Voltimeter Example') plt.show()
def plotecdf(self, symb1="r-", symb2="b."): """ Plot Empirical and fitted Cumulative Distribution Function The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the empirical CDF should resemble the model CDF. Other distribution types will introduce deviations in the plot. """ n = len(self.data) F = (arange(1, n + 1)) / n plotbackend.plot(self.data, F, symb2, self.data, self.cdf(self.data), symb1) plotbackend.xlabel("x") plotbackend.ylabel("F(x) (%s)" % self.dist.name) plotbackend.title("Empirical CDF plot")
def plotecdf(self, symb1='r-', symb2='b.'): ''' Plot Empirical and fitted Cumulative Distribution Function The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the empirical CDF should resemble the model CDF. Other distribution types will introduce deviations in the plot. ''' n = len(self.data) F = (arange(1, n + 1)) / n plotbackend.plot(self.data, F, symb2, self.data, self.cdf(self.data), symb1) plotbackend.xlabel('x') plotbackend.ylabel('F(x) (%s)' % self.dist.name) plotbackend.title('Empirical CDF plot')
def plotesf(self, symb1="r-", symb2="b."): """ Plot Empirical and fitted Survival Function The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the empirical CDF should resemble the model CDF. Other distribution types will introduce deviations in the plot. """ n = len(self.data) SF = (arange(n, 0, -1)) / n plotbackend.semilogy(self.data, SF, symb2, self.data, self.sf(self.data), symb1) # plotbackend.plot(self.data,SF,'b.',self.data,self.sf(self.data),'r-') plotbackend.xlabel("x") plotbackend.ylabel("F(x) (%s)" % self.dist.name) plotbackend.title("Empirical SF plot")
def plotesf(self, symb1='r-', symb2='b.'): ''' Plot Empirical and fitted Survival Function The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the empirical CDF should resemble the model CDF. Other distribution types will introduce deviations in the plot. ''' n = len(self.data) SF = (arange(n, 0, -1)) / n plotbackend.semilogy( self.data, SF, symb2, self.data, self.sf(self.data), symb1) # plotbackend.plot(self.data,SF,'b.',self.data,self.sf(self.data),'r-') plotbackend.xlabel('x') plotbackend.ylabel('F(x) (%s)' % self.dist.name) plotbackend.title('Empirical SF plot')
def plotresq(self, symb1="r-", symb2="b."): """PLOTRESQ displays a residual quantile plot. The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the plot will be linear. Other distribution types will introduce curvature in the plot. """ n = len(self.data) eprob = (arange(1, n + 1) - 0.5) / n y = self.ppf(eprob) y1 = self.data[[0, -1]] plotbackend.plot(self.data, y, symb2, y1, y1, symb1) plotbackend.xlabel("Empirical") plotbackend.ylabel("Model (%s)" % self.dist.name) plotbackend.title("Residual Quantile Plot") plotbackend.axis("tight") plotbackend.axis("equal")
def plotepdf(self, symb1="r-", symb2="b-"): """Plot Empirical and fitted Probability Density Function The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the histogram should resemble the model density. Other distribution types will introduce deviations in the plot. """ x, pdf = self._get_empirical_pdf() ymax = pdf.max() # plotbackend.hist(self.data,normed=True,fill=False) plotbackend.plot(self.data, self.pdf(self.data), symb1, x, pdf, symb2) ax = list(plotbackend.axis()) ax[3] = min(ymax * 1.3, ax[3]) plotbackend.axis(ax) plotbackend.xlabel("x") plotbackend.ylabel("f(x) (%s)" % self.dist.name) plotbackend.title("Density plot")
def plotresq(self, symb1='r-', symb2='b.'): '''PLOTRESQ displays a residual quantile plot. The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the plot will be linear. Other distribution types will introduce curvature in the plot. ''' n = len(self.data) eprob = (arange(1, n + 1) - 0.5) / n y = self.ppf(eprob) y1 = self.data[[0, -1]] plotbackend.plot(self.data, y, symb2, y1, y1, symb1) plotbackend.xlabel('Empirical') plotbackend.ylabel('Model (%s)' % self.dist.name) plotbackend.title('Residual Quantile Plot') plotbackend.axis('tight') plotbackend.axis('equal')
def plotepdf(self, symb1='r-', symb2='b-'): '''Plot Empirical and fitted Probability Density Function The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the histogram should resemble the model density. Other distribution types will introduce deviations in the plot. ''' x, pdf = self._get_empirical_pdf() ymax = pdf.max() # plotbackend.hist(self.data,normed=True,fill=False) plotbackend.plot(self.data, self.pdf(self.data), symb1, x, pdf, symb2) ax = list(plotbackend.axis()) ax[3] = min(ymax * 1.3, ax[3]) plotbackend.axis(ax) plotbackend.xlabel('x') plotbackend.ylabel('f(x) (%s)' % self.dist.name) plotbackend.title('Density plot')
def plotresprb(self, symb1="r-", symb2="b."): """ PLOTRESPRB displays a residual probability plot. The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the plot will be linear. Other distribution types will introduce curvature in the plot. """ n = len(self.data) # ecdf = (0.5:n-0.5)/n; ecdf = arange(1, n + 1) / (n + 1) mcdf = self.cdf(self.data) p1 = [0, 1] plotbackend.plot(ecdf, mcdf, symb2, p1, p1, symb1) plotbackend.xlabel("Empirical") plotbackend.ylabel("Model (%s)" % self.dist.name) plotbackend.title("Residual Probability Plot") plotbackend.axis("equal") plotbackend.axis([0, 1, 0, 1])
def demo_smoothn_on_1d_cos(): """ Examples -------- >>> demo_smoothn_on_1d_cos() >>> plt.close() """ x = np.linspace(0, 100, 2**8) y = np.cos(x / 10) + (x / 50)**2 + np.random.randn(np.size(x)) / 10 y[np.r_[70, 75, 80]] = np.array([5.5, 5, 6]) z = smoothn(y) # Regular smoothing zr = smoothn(y, robust=True) # Robust smoothing _h0 = plt.subplot(121), _h = plt.plot(x, y, 'r.', x, z, 'k', linewidth=2) plt.title('Regular smoothing') plt.subplot(122) plt.plot(x, y, 'r.', x, zr, 'k', linewidth=2) plt.title('Robust smoothing')
def plotresprb(self, symb1='r-', symb2='b.'): ''' PLOTRESPRB displays a residual probability plot. The purpose of the plot is to graphically assess whether the data could come from the fitted distribution. If so the plot will be linear. Other distribution types will introduce curvature in the plot. ''' n = len(self.data) # ecdf = (0.5:n-0.5)/n; ecdf = arange(1, n + 1) / (n + 1) mcdf = self.cdf(self.data) p1 = [0, 1] plotbackend.plot(ecdf, mcdf, symb2, p1, p1, symb1) plotbackend.xlabel('Empirical') plotbackend.ylabel('Model (%s)' % self.dist.name) plotbackend.title('Residual Probability Plot') plotbackend.axis('equal') plotbackend.axis([0, 1, 0, 1])
def test_hampel(): randint = np.random.randint Y = 5000 + np.random.randn(1000) outliers = randint(0, 1000, size=(10,)) Y[outliers] = Y[outliers] + randint(1000, size=(10,)) YY, res = HampelFilter(dx=3, t=3, fulloutput=True)(Y) YY1, res1 = HampelFilter(dx=1, t=3, adaptive=0.1, fulloutput=True)(Y) YY2, res2 = HampelFilter(dx=3, t=0, fulloutput=True)(Y) # median plt.figure(1) plot_hampel(Y, YY, res) plt.title('Standard HampelFilter') plt.figure(2) plot_hampel(Y, YY1, res1) plt.title('Adaptive HampelFilter') plt.figure(3) plot_hampel(Y, YY2, res2) plt.title('Median filter') plt.show('hold')
def demo_savitzky_on_noisy_chirp(): """ Examples -------- >>> demo_savitzky_on_noisy_chirp() >>> plt.close() """ plt.figure(figsize=(7, 12)) # generate chirp signal tvec = np.arange(0, 6.28, .02) true_signal = np.sin(tvec * (2.0 + tvec)) true_d_signal = (2 + tvec) * np.cos(tvec * (2.0 + tvec)) # add noise to signal noise = np.random.normal(size=true_signal.shape) signal = true_signal + .15 * noise # plot signal plt.subplot(311) plt.plot(signal) plt.title('signal') # smooth and plot signal plt.subplot(312) savgol = SavitzkyGolay(n=8, degree=4) s_signal = savgol.smooth(signal) s2 = smoothn(signal, robust=True) plt.plot(s_signal) plt.plot(s2) plt.plot(true_signal, 'r--') plt.title('smoothed signal') # smooth derivative of signal and plot it plt.subplot(313) savgol1 = SavitzkyGolay(n=8, degree=1, diff_order=1) dt = tvec[1] - tvec[0] d_signal = savgol1.smooth(signal) / dt plt.plot(d_signal) plt.plot(true_d_signal, 'r--') plt.title('smoothed derivative of signal')
def demo_hampel(): """ Examples -------- >>> demo_hampel() >>> plt.close() """ randint = np.random.randint Y = 5000 + np.random.randn(1000) outliers = randint(0, 1000, size=(10, )) Y[outliers] = Y[outliers] + randint(1000, size=(10, )) YY, res = HampelFilter(dx=3, t=3, fulloutput=True)(Y) YY1, res1 = HampelFilter(dx=1, t=3, adaptive=0.1, fulloutput=True)(Y) YY2, res2 = HampelFilter(dx=3, t=0, fulloutput=True)(Y) # median plt.figure(1) plot_hampel(Y, YY, res) plt.title('Standard HampelFilter') plt.figure(2) plot_hampel(Y, YY1, res1) plt.title('Adaptive HampelFilter') plt.figure(3) plot_hampel(Y, YY2, res2) plt.title('Median filter')
m_sea = ts.data.mean() f0_sea = np.interp(m_sea, lc.args, lc.data) extr_sea = len(tp.data) / (2 * T_sea) alfa_sea = f0_sea / extr_sea print('alfa = %g ' % alfa_sea) #! Section 4.3.2 Extraction of rainflow cycles #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #! Min-max and rainflow cycle plots #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ mM_rfc = tp.cycle_pairs(h=0.3) plt.clf() plt.subplot(122), mM.plot() plt.title('min-max cycle pairs') plt.subplot(121), mM_rfc.plot() plt.title('Rainflow filtered cycles') plt.show() #! Min-max and rainflow cycle distributions #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import wafo.misc as wm ampmM_sea = mM.amplitudes() ampRFC_sea = mM_rfc.amplitudes() plt.clf() plt.subplot(121) wm.plot_histgrm(ampmM_sea, 25) ylim = plt.gca().get_ylim() plt.title('min-max amplitude distribution')
plt.show() #disp('Block = 3'),pause(pstate) ## # Return values in the Gumbel distribution plt.clf() T = np.r_[1:100000] sT = gum[0] - gum[1] * np.log(-np.log1p(-1. / T)) plt.semilogx(T, sT) plt.hold(True) # ws.edf(Hs).plot() Nmax = len(Hs) N = np.r_[1:Nmax + 1] plt.plot(Nmax / N, sorted(Hs, reverse=True), '.') plt.title('Return values in the Gumbel model') plt.xlabel('Return period') plt.ylabel('Return value') #wafostamp([],'(ER)') plt.show() #disp('Block = 4'),pause(pstate) ## Section 5.2 Generalized Pareto and Extreme Value distributions ## Section 5.2.1 Generalized Extreme Value distribution # Empirical distribution of significant wave-height with estimated # Generalized Extreme Value distribution, gev = ws.genextreme.fit2(Hs) gev.plotfitsummary() # wafostamp([],'(ER)') # disp('Block = 5a'),pause(pstate)
m_sea = ts.data.mean() f0_sea = np.interp(m_sea, lc.args,lc.data) extr_sea = len(tp.data)/(2*T_sea) alfa_sea = f0_sea/extr_sea print('alfa = %g ' % alfa_sea) #! Section 4.3.2 Extraction of rainflow cycles #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #! Min-max and rainflow cycle plots #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ mM_rfc = tp.cycle_pairs(h=0.3) plt.clf() plt.subplot(122), mM.plot() plt.title('min-max cycle pairs') plt.subplot(121), mM_rfc.plot() plt.title('Rainflow filtered cycles') plt.show() #! Min-max and rainflow cycle distributions #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import wafo.misc as wm ampmM_sea = mM.amplitudes() ampRFC_sea = mM_rfc.amplitudes() plt.clf() plt.subplot(121) wm.plot_histgrm(ampmM_sea,25) ylim = plt.gca().get_ylim() plt.title('min-max amplitude distribution')
Tcrcr, ix = ts.wave_periods(vh=0, pdef='c2c', wdef='tw', rate=8) Tc, ixc = ts.wave_periods(vh=0, pdef='u2d', wdef='tw', rate=8) #! Histogram of crestperiod compared to the kernel density estimate #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import wafo.kdetools as wk plt.clf() print(Tc.mean()) print(Tc.max()) t = np.linspace(0.01,8,200); ftc = wk.TKDE(Tc, L2=0, inc=128) plt.plot(t,ftc.eval_grid(t), t, ftc.eval_grid_fast(t),'-.') wm.plot_histgrm(Tc, normed=True) plt.title('Kernel Density Estimates') plt.xlabel('Tc [s]') plt.axis([0, 8, 0, 0.5]) plt.show() #! Extreme waves - model check: the highest and steepest wave #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ plt.clf() S, H = ts.wave_height_steepness(kind=0) indS = S.argmax() indH = H.argmax() ts.plot_sp_wave([indH, indS],'k.') plt.show() #! Does the highest wave contradict a transformed Gaussian model? #!----------------------------------------------------------------
Tcrcr, ix = ts.wave_periods(vh=0, pdef='c2c', wdef='tw', rate=8) Tc, ixc = ts.wave_periods(vh=0, pdef='u2d', wdef='tw', rate=8) #! Histogram of crestperiod compared to the kernel density estimate #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import wafo.kdetools as wk plt.clf() print(Tc.mean()) print(Tc.max()) t = np.linspace(0.01, 8, 200) ftc = wk.TKDE(Tc, L2=0, inc=128) plt.plot(t, ftc.eval_grid(t), t, ftc.eval_grid_fast(t), '-.') wm.plot_histgrm(Tc, normed=True) plt.title('Kernel Density Estimates') plt.xlabel('Tc [s]') plt.axis([0, 8, 0, 0.5]) plt.show() #! Extreme waves - model check: the highest and steepest wave #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ plt.clf() S, H = ts.wave_height_steepness(kind=0) indS = S.argmax() indH = H.argmax() ts.plot_sp_wave([indH, indS], 'k.') plt.show() #! Does the highest wave contradict a transformed Gaussian model? #!----------------------------------------------------------------
#rfc = tp2rfc(tp); #plot(rfc(:, 2), rfc(:, 1), '.') #wafostamp('', '(ER)') #hold off #disp('Block = 9'), pause(pstate) #! Section 1.4.5 Extreme value statistics #!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Plot of yura87 data plt.clf() import wafo.data as wd xn = wd.yura87() #xn = load('yura87.dat'); plt.subplot(211) plt.plot(xn[::30, 0] / 3600, xn[::30, 1], '.') plt.title('Water level') plt.ylabel('(m)') #! Formation of 5 min maxima yura = xn[:85500, 1] yura = np.reshape(yura, (285, 300)).T maxyura = yura.max(axis=0) plt.subplot(212) plt.plot(xn[299:85500:300, 0] / 3600, maxyura, '.') plt.xlabel('Time (h)') plt.ylabel('(m)') plt.title('Maximum 5 min water level') plt.show() #! Estimation of GEV for yuramax plt.clf()
plt.show() #disp('Block = 3'),pause(pstate) ## # Return values in the Gumbel distribution plt.clf() T = np.r_[1:100000] sT = gum[0] - gum[1] * np.log(-np.log1p(-1./T)) plt.semilogx(T, sT) plt.hold(True) # ws.edf(Hs).plot() Nmax = len(Hs) N = np.r_[1:Nmax+1] plt.plot(Nmax/N, sorted(Hs, reverse=True), '.') plt.title('Return values in the Gumbel model') plt.xlabel('Return period') plt.ylabel('Return value') #wafostamp([],'(ER)') plt.show() #disp('Block = 4'),pause(pstate) ## Section 5.2 Generalized Pareto and Extreme Value distributions ## Section 5.2.1 Generalized Extreme Value distribution # Empirical distribution of significant wave-height with estimated # Generalized Extreme Value distribution, gev = ws.genextreme.fit2(Hs) gev.plotfitsummary() # wafostamp([],'(ER)') # disp('Block = 5a'),pause(pstate)