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 | #!/usr/bin/python2
# -*- coding: utf-8 -*-
""" CREATED: 7/2012
AUTHOR: TOMÃÂÃ
ODSTRÃÂIL
"""
import matplotlib
matplotlib.rcParams['backend'] = 'Agg'
from scipy.signal import *
from numpy import *
from matplotlib.pyplot import *
from cwt import cwt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter,LogLocator
import os
import sys
from pygolem_lite import load_adv, saveconst, Shot
from multiprocessing import Process, Pool, cpu_count
from scipy.stats.mstats import mquantiles
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
matplotlib.rcParams['xtick.major.size'] = 10
matplotlib.rcParams['xtick.minor.size'] = 7
matplotlib.rcParams['ytick.major.size'] = 10
matplotlib.rcParams['ytick.minor.size'] = 7
shot = Shot()['shotno']
if shot > 12079:
calib =-array([ 261, 261, -261, 261]) #T/(V*s)
else:
calib =-array([ 261, 261, -261, -261]) #T/(V*s)
def Spectrogram((omega0,frequenciCutOff,tvec, signal,name,start, end, plasma)):
print name
signal = double(signal)
startAdv = max(start*0.8, tvec[0]*1e3)
endAdv = min((end-start)*1.2+start, tvec[-1]*1e3)
# cut-off signal
ind = (tvec > startAdv*1e-3) & (tvec < endAdv*1e-3)
signal = signal[ind]
tvec = tvec[ind]
dt = mean(diff(tvec))
N = len(tvec)
for i in range(3):
filtered = medfilt(signal, 2*floor(N/500)+1)
res = abs(signal - filtered)
ind = argsort(res)
n_out = floor(N/400)
ind_2 = in1d(range(N), ind[-n_out:]) & (res > 0.2*amax(filtered))
signal[ind_2] = filtered[ind_2] #remove "n_out" most distant points from moving medianZ
print "computing spectrum"
spec, scale = cwt(signal, dt, dj=0.05, wf='morlet', p=omega0, extmethod='none', extlength='powerof2')
print "done"
# approximate scales through frequencies
freq = (omega0 + sqrt(2.0 + omega0**2))/(4*pi * scale[1:])
contrast = 10
#spec = spec[:,(tvec > startAdv )*( tvec <endAdv)]
pole = log(1+contrast*abs(spec))
pole = pole[freq > frequenciCutOff, : ]
freq = freq[freq > frequenciCutOff]
fig = figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.85])
img = ax.imshow(pole, extent=[startAdv,endAdv ,freq[-1], freq[0]], aspect='auto')
ax.set_yscale('log', nonposy='clip')
minorLocator = MultipleLocator(1)
ax.xaxis.set_minor_locator(minorLocator)
ax.plot([start, start], [amin(freq), amax(freq)], 'w--')
ax.plot([end, end], [amin(freq), amax(freq)], 'w--')
ax.axis([startAdv,endAdv,amin(freq), amax(freq)])
ax.set_xlabel('time [ms]')
ax.set_ylabel('Frequency [Hz]')
savefig('data/spectrogram_'+name+'.png')
clf()
ind = (tvec > startAdv*1e-3 ) & ( tvec<endAdv*1e-3)
signal= signal[ ind ]
plot(1e3*tvec[ ind ],cumsum(signal)*1e-6,'k',linewidth=0.1)
#show()
#axvline(linewidth=4)
#minimum = -max(abs(amin(signal)), abs(amax(signal)))
#maximum = -minimum
minimum = mquantiles(signal,0.01)
maximum = mquantiles(signal, 0.99)
minimum -= (maximum - minimum)*0.2
maximum += (maximum - minimum)*0.2
if maximum == minimum:
axis([startAdv,endAdv,None, None])
else:
axis([startAdv,endAdv,minimum, maximum])
#axis([startAdv,endAdv,None, None])
if plasma:
plot([start, start], [minimum, maximum], 'r--')
plot([end, end], [minimum, maximum], 'r--')
xlabel('time [ms]')
ylabel('Intensity [a.u.]')
#show()
savefig('data/signal_'+name+'.png')
clf()
Data = Shot()
plasma = Data['plasma']
if plasma:
start = Data['plasma_start']*1e3
end = Data['plasma_end']*1e3
else:
start = 0
end = 40
tvec, data = Shot()['nistandard6132']
tvec = float_(tvec)
omega0 = 25 #ÃÂÃÂm vÃÂtÃ
¡Ã tÃÂm lepÃ
¡Ã frekvenÃÂnàrozliÃ
¡enàa opaÃÂnÃÂ
frequenciCutOff = 900
try:
os.mkdir('data')
except:
pass
Ndim = size(data,1)
try:
p = Pool(cpu_count())
p.map( Spectrogram, [ (omega0,frequenciCutOff,tvec, data[:,i]*calib[i],str(i),start,end, plasma) for i in range(Ndim)])
p.close()
saveconst('status', 0)
except:
saveconst('status', 1)
raise
os.system('convert -resize 150 data/spectrogram_1.png icon.png')
|