Package ivs :: Package timeseries :: Module windowfunctions
[hide private]
[frames] | no frames]

Source Code for Module ivs.timeseries.windowfunctions

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3  Window functions. 
  4   
  5  A window with a high dynamic range is a window that can distinguish peaks in 
  6  a broad range of amplitudes. Usually, this goes together with loss in resolution 
  7  and detection power (higher noise). 
  8   
  9  Example usage: calculate the window function of timeseries, using different 
 10  windows. 
 11      >>> import tfreq 
 12      >>> from pylab import figure,show,legend,plot,xlim 
 13      >>> times = linspace(0,150,10000) 
 14      >>> signal = ones(len(times)) 
 15      >>> windows = ['rectangular','hann','hamming','cosine','bartlett'] 
 16      >>> windows += ['nuttall','blackman-harris'] 
 17      >>> for window in windows: 
 18      ...   pergram = tfreq.scargle(times,signal,fn=1.5,df=0.0005,norm='amplitude',window=window)[1] 
 19      ...   p=figure(1) 
 20      ...   p=plot(pergram[0],pergram[1],'-',label=window) 
 21      ...   p=figure(2) 
 22      ...   p=plot(log10(pergram[0]),log10(pergram[1]),'-',label=window) 
 23      >>> p=figure(1);p=legend();p=xlim(0,1.5) 
 24      >>> p=figure(2);p=legend(loc='lower left');p=xlim(-2.5,-0.3) 
 25  """ 
 26  import numpy as np 
 27  import logging 
 28   
 29  logger = logging.getLogger("IVS.TS.WINF") 
 30   
 31  #{ Main wrapper 
32 -def getWindowFunction(name,times):
33 return globals()[name.lower()](times)
34 35 #} 36 37 #{ Low dynamic range
38 -def rectangular(times):
39 """ 40 Rectangular (no) window 41 """ 42 logger.debug("Selected rectangular window") 43 return np.ones(len(times))
44 #} 45 46 #{ Moderate dynamic range
47 -def hamming(times):
48 """ 49 Hamming window 50 """ 51 a = 0.54836 52 window_width = times.ptp() 53 window = a - (1-a) * np.cos(2*np.pi*(times-times[0])/window_width) 54 logger.debug("Selected Hamming window") 55 return window
56
57 -def hann(times):
58 """ 59 Hann or Hanning window 60 """ 61 n = times-times[0] 62 N_1 = times.ptp() 63 window = 0.5* (1-np.cos(2*np.pi*n)/N_1) 64 logger.debug("Selected Hann window") 65 return window
66
67 -def cosine(times):
68 """ 69 Cosine window 70 """ 71 n = times-times[0] 72 N_1 = times.ptp() 73 window = np.cos(np.pi*n/N_1 - np.pi/2) 74 logger.debug("Selected Cosine window") 75 return window
76
77 -def bartlett(times):
78 """ 79 Bartlett window (zero valued end-points) 80 """ 81 n = times-times[0] 82 N_1 = times.ptp() 83 window = 2./N_1 * (N_1/2. - np.abs(n-N_1/2.)) 84 logger.debug("Selected Bartlett window") 85 return window
86 87 #} 88 #{ High dynamic range
89 -def nuttall(times):
90 """ 91 Nuttall window 92 """ 93 a0=0.355768 94 a1=0.487396 95 a2=0.144232 96 a3=0.012604 97 n = times-times[0] 98 N_1 = times.ptp() 99 window = a0 - a1*np.cos(2*np.pi*n/N_1) + a2*np.cos(4*np.pi*n/N_1) - a3*np.cos(6*np.pi*n/N_1) 100 logger.debug("Selected Nuttall window") 101 return window
102
103 -def blackman_harris(times):
104 """ 105 Blackman Harris window 106 """ 107 a0 = 0.3635819 108 a1 = 0.4891775 109 a2 = 0.1365995 110 a3 = 0.0106411 111 n = times-times[0] 112 N_1 = times.ptp() 113 window = a0 - a1*np.cos(2*np.pi*n/N_1) + a2*np.cos(4*pi*n/N_1) - a3*np.cos(6*np.pi*n/N_1) 114 logger.debug("Selected Blackman-Harris window") 115 return window
116 #} 117
118 -def test():
119 """ 120 >>> from pylab import show 121 >>> p=show() 122 """ 123 import doctest 124 doctest.testmod()
125 126 if __name__ == "__main__": 127 test() 128