Source code :: CMWT

[Return]
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from matplotlib.pyplot import *
from numpy import *
#from scipy.fftpack import rfft, irfft,fftshift,ifft
from numpy.fft import rfft, irfft,fftshift,ifft
from _extend import extend
import sys
from scipy import signal   

"""
    Continuous Multi-Wavelet analyzis  0.1
    
    Description:
    The continius wavelet analysis od the multiple signal is calculated. Instead
    of the single one dimensional wavelet a n-dimensional Morlet wavelet with shifted component is created.
    The moving scalar product with such wavelets helps to identify and amplify the  correctly time shifted 
    signals and suppress the other. 



"""




__all__ = ["NTM_CWT", "angularfreq", "scales", "compute_s0"]


def angularfreq(N, dt):
    """Compute angular frequencies.

    :Parameters:   
      N : integer
        number of data samples
      dt : float
        time step
    
    :Returns:
      angular frequencies :  1d numpy array
    """

    # See (5) at page 64.
    
    N2 = N / 2.0
    w = empty(N)

    for i in range(w.shape[0]):       
        if i <= N2:
            w[i] = (2 * pi * i) / (N * dt)
        else:
            w[i] = (2 * pi * (i - N)) / (N * dt)

    return w


def scales(N, dj, dt, s0):
    """Compute scales.

    :Parameters:
      N : integer
        number of data samples
      dj : float
        scale resolution
      dt : float
        time step

    :Returns:
      scales : 1d numpy array
        scales
    """

    #  See (9) and (10) at page 67.

    J = floor(dj**-1 * log2((N * dt) / s0))
    s = empty(J + 1)
    
    for i in range(s.shape[0]):
        s[i] = s0 * 2**(i * dj)
    
    return s


def compute_s0(dt, p):
    """Compute s0.
    
    :Parameters:    
      dt :float
        time step
      p : float
        omega0 ('morlet') or order ('paul', 'dog')


    :Returns:    
      s0 : float
    """


    return (dt * (p + sqrt(2 + p**2))) / (2 * pi)
    
   

    
    
    
    
    
  

def NTM_CWT((x, dt, dj, p,m , res, fmin,fmax)):

    n_detec = size(x,1)
  
    lenght = size(x,0)
    lenght_ext = int(2**ceil(log2(lenght)))

    x = signal.detrend(x, axis=0, type='linear') 

	
    x_new =  extend(x, method='zeros')


   
    w = angularfreq(lenght_ext, dt)
    s0 = compute_s0(dt, p)
    s = scales(lenght_ext, dj, dt, s0)
    freq = (p + sqrt(2.0 + p**2))/(4*pi * s)	
    
    ind = where((freq>fmin)&(freq<fmax))
    s = s[ind]
  
    x = rfft(x_new, axis=0)
    x[::2]*= -1  #magic :-)


    step  = max(int(lenght_ext/res),1)
   
    stmp = zeros(1)
 
    faze = arange(n_detec)/double(n_detec)*m
    faze = matrix(faze)
    
    wft = zeros((len(w)),dtype=cfloat )
    

    exp_phase = exp(-2*pi*1j*faze)
    f_max = 1/dt	
    spec = list()
    for i in range(len(s)):
	#sys.stdout.write('\r %.1f%%'%(i*100./(len(s))))
        #sys.stdout.flush()
	interv = (((abs(s[i]*w[:len(w)/2]-p)) < 5))	
	arg = s[i]*w[interv]-p 
	
	arg += 1e-2/(1-w[interv]/w[len(w)/2]+0.001)

	wavelet= (1+sign(w[interv]))*exp(-(arg )**2/2)*sqrt(abs(s[i])/dt) 
	
	#tady by šlo udělat aby pro každou frekvenci bylo jiné okno
	wavelet = matrix(wavelet, copy = False)

	wft[interv] = sum(multiply(x[interv],wavelet.T*exp_phase),1) 
	wft = ifft(wft)
	
	spec.append(fftshift(wft[::step]))
	
	wft[:] = 0
	

    spec = array(spec, copy = False)
    spec/= n_detec
    
    n_edge = (lenght_ext/float(lenght)-1)*size(spec,1)/2
   
    spec = spec[:,int(n_edge):-int(n_edge)]
  

  
    s*=max(abs(m),1)
  
    return spec, s
    
    
    
    
    
    
    
    
    
      
    
    
    
    
    
    
    
    
    
    
    

Navigation