Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#### Convenience Functions to be moved to kerneltools #### 

2import numpy as np 

3 

4def forrt(X, m=None): 

5 """ 

6 RFFT with order like Munro (1976) FORTT routine. 

7 """ 

8 if m is None: 

9 m = len(X) 

10 y = np.fft.rfft(X, m) / m 

11 return np.r_[y.real, y[1:-1].imag] 

12 

13def revrt(X, m=None): 

14 """ 

15 Inverse of forrt. Equivalent to Munro (1976) REVRT routine. 

16 """ 

17 if m is None: 

18 m = len(X) 

19 i = int(m // 2 + 1) 

20 y = X[:i] + np.r_[0, X[i:], 0] * 1j 

21 return np.fft.irfft(y)*m 

22 

23def silverman_transform(bw, M, RANGE): 

24 """ 

25 FFT of Gaussian kernel following to Silverman AS 176. 

26 

27 Notes 

28 ----- 

29 Underflow is intentional as a dampener. 

30 """ 

31 J = np.arange(M/2+1) 

32 FAC1 = 2*(np.pi*bw/RANGE)**2 

33 JFAC = J**2*FAC1 

34 BC = 1 - 1. / 3 * (J * 1./M*np.pi)**2 

35 FAC = np.exp(-JFAC)/BC 

36 kern_est = np.r_[FAC, FAC[1:-1]] 

37 return kern_est 

38 

39def counts(x, v): 

40 """ 

41 Counts the number of elements of x that fall within the grid points v 

42 

43 Notes 

44 ----- 

45 Using np.digitize and np.bincount 

46 """ 

47 idx = np.digitize(x, v) 

48 try: # numpy 1.6 

49 return np.bincount(idx, minlength=len(v)) 

50 except: 

51 bc = np.bincount(idx) 

52 return np.r_[bc, np.zeros(len(v) - len(bc))] 

53 

54def kdesum(x, axis=0): 

55 return np.asarray([np.sum(x[i] - x, axis) for i in range(len(x))])