-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysis.py
830 lines (724 loc) · 23.7 KB
/
analysis.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
from NImodules import IV_Retrieve_py, IV_Retrieve_Current_py,IV_Retrieve_Titles_log_py
import pylab as plt
import sys
import numpy as np
import os
import scipy.ndimage as nd
from scipy import optimize,arccos, sin, cos
#cw-
def get_data_path(start_path):
file_path = []
for root, dirs, files in os.walk(start_path, topdown=True):
for name in files:
file_name = os.path.join(root, name)
#file_name.replace(splash,"\\")
file_path.append(file_name)
for name in dirs:
file_name = os.path.join(root, name)
#file_name.replace(splash,"\\")
#file_path.append(file_name)
return file_path
def CW_analysis(title,log_name,start):#title = str, log_name = "0 to %s step 1" %log, start = str
raw_data = IV_Retrieve_py([start,log_name])[0]
C_array_position = []
for Current in title:
if Current =="Current(A)" or Current =="I(nA)" or Current =="I setting (A)" or Current == "Current, mA":
C_array_position.append("c")
elif Current == "Time(s)":
C_array_position.append("x")
elif Current == "S21 Amp(mU)":
C_array_position.append("y")
else:
C_array_position.append(" ")
C_Array = IV_Retrieve_Current_py([start,C_array_position,log_name])[0]#output()
CW_list = []
for log in range(len(raw_data)):
k = 0
for i in raw_data[log][1][2]:
k = i+k
CW_list.append(k/float(len(raw_data[log][1][2])))
J = 0
current_array = []
CW_array = []
I_position = 0
while J == 0:
add = 0
separate_array = []
CW_separate_array = []
while add == 0:
separate_array.append(C_Array[I_position])
CW_separate_array.append(CW_list[I_position])
I_position += 1
if I_position == len(C_Array)-1:
J = 1
add = 1
elif C_Array[I_position-1] > C_Array[I_position]:
add = 1
current_array.append(separate_array)
CW_array.append(CW_separate_array)
return current_array,CW_array #output[[current],[average]],current = [],[]....,average = [],[],.....
#freq
def connect_phase(freq,phase):
Phase = []
move = 0
j = 0
connect = 0
while j == 0:
k = 0
if len(Phase) == len(phase):
j = 1
else:
while abs(phase[move + k - 1] - phase[move + k]) <= 5. :
Phase.append(phase[k + move]-2*np.pi*connect)
#if abs(phase[0][move + k - 1] - phase[0][move + k]) ==
k += 1
if len(Phase) == len(phase):
break
if len(Phase) == len(phase):
j = 1
else:
connect += 1
Phase.append(phase[k+move]-2*np.pi*connect)
move += k + 1
z = np.polyfit(freq,Phase,1)
p = np.poly1d(z)
Phase = Phase-p(freq)
return Phase
def freq_analysis(start,log_name,log):
raw_data = IV_Retrieve_py([start,log_name])[0]
Freq = raw_data[log][0][2]
Amp = raw_data[log][1][2]
phase = raw_data[log][3][2]
return Freq, Amp, phase
#get labview data----------------------------------
def open_data(start):
title_log = IV_Retrieve_Titles_log_py(start)
title = title_log[0]
log = title_log[1]
log_name = "0 to %s step 1" %log
freq = []
Amp = []
phase = []
for i in range(log):
data = freq_analysis(start,log_name,i)
freq += data[0]
Amp += data[1]
phase += data[2]
return freq,Amp,phase
def peak_and_deep(v, delta, x = None):
maxnum = []
minnum = []
maxpos = {}
minpos = {}
if x is None:
x = np.arange(len(v))
v = np.asarray(v)
if len(v) != len(x):
sys.exit('Input vectors v and x must have same length')
if not np.isscalar(delta):
sys.exit('Input argument delta must be a scalar')
if delta <= 0:
sys.exit('Input argument delta must be positive')
mn, mx = np.Inf, -np.Inf
mnpos, mxpos = np.NaN, np.NaN
lookformax = True
for i in np.arange(len(v)):
this = v[i]
if this > mx:
mx = this
mxpos = x[i]
if this < mn:
mn = this
mnpos = x[i]
if lookformax:
if this < mx-delta:
maxnum.append(mx)
maxpos[mx] = mxpos
mn = this
mnpos = x[i]
lookformax = False
else:
if this > mn+delta:
minnum.append(mn)
minpos[mn] = mnpos
mx = this
mxpos = x[i]
lookformax = True
peakpos = []
deeppos = []
maxnum.sort()
minnum.sort(reverse=True)
for POS in maxnum:
peakpos.append(maxpos[POS])
for pos in minnum:
deeppos.append(minpos[pos])
return peakpos, maxnum, deeppos, minnum
def get_data(start_path):
file_path = [start_path]
for root, dirs, files in os.walk(start_path, topdown=True):
for name in files:
file_name = os.path.join(root, name)
#file_name.replace(splash,"\\")
file_path.append(file_name)
for name in dirs:
file_name = os.path.join(root, name)
#file_name.replace(splash,"\\")
#file_path.append(file_name)
return file_path
def MMF(Y,rn,bg):
stru_len_op = int(bg)
stru_len_clo = int(bg*1.5)
stru_ele_op = np.linspace(0,0,stru_len_op)
stru_ele_clo = np.linspace(0,0,stru_len_clo)
#triangular wave
tri_wave = []
amp = 1.0
width = 1
samp = rn
asym = 0.5
points = 1
while points <= samp:
Xi = 0.1*points
if 0 <= Xi and Xi <= width*asym:
tri_wave.append(amp*Xi/(width*asym))
elif Xi > width*asym and Xi <width:
tri_wave.append(amp*(width-Xi)/(width*(1-asym)))
else:
tri_wave.append(0)
points += 1
#low-pass
op_flat = nd.grey_opening(Y,size = (stru_len_op),structure = stru_ele_op)
clo_flat = nd.grey_closing(op_flat,size = (stru_len_clo),structure = stru_ele_clo)
reducing = []
for reduce in range(len(Y)):
reducing.append(Y[reduce] - clo_flat[reduce])
op_tri = nd.grey_opening(reducing,size = (rn),structure = tri_wave)
clo_tri = nd.grey_closing(reducing,size = (rn),structure = tri_wave)
after_stru_ele =np.linspace(0,0,rn)
op_than_clo = nd.grey_closing(op_tri,size = (rn),structure = after_stru_ele)
clo_than_op = nd.grey_opening(clo_tri,size = (rn),structure = after_stru_ele)
plusing = []
for plus in range(len(op_than_clo)):
plusing.append((op_than_clo[plus]+clo_than_op[plus])/2.0)
return plusing, clo_flat
def save_txt(data_name,data):
f = open(data_name,"w")
for i in data:
f.write(" " + str(i))
return None
def fre_point(freq, pos):
for i in freq:
if freq[pos] == i:
return i
def lin_pha(freq,phase):
Phase = connect_phase(freq,phase)
z = np.polyfit(freq,Phase,1)
p = np.poly1d(z)
Phase = Phase-p(freq)
return Phase
def R_I(data,RAN,Range=50):
freq = data[0]
Amp = data[1]
phase = data[2]
Phase = lin_pha(freq,phase)
Range = int(Range/2)
R = []
I = []
RAN = int(RAN)
for i in range(RAN-Range,RAN+Range):
R.append(Amp[i]*np.cos(Phase[i]))
I.append(Amp[i]*np.sin(Phase[i]))
return R,I
def smooth(R,I,smo):
smooth_R = np.copy(R)
for i in range(len(R)):
sameR = 0
for k in range(smo):
if i == k:
smooth_R[i] = smooth_R[i]
sameR = 1
if sameR == 0:
for k in range(1,smo):
smooth_R[i] += smooth_R[i-k]
smooth_R[i] /= smo
smooth_I = np.copy(I)
for i in range(len(R)):
sameI = 0
for k in range(smo):
if i == k:
smooth_I[i] = smooth_I[i]
sameI = 1
if sameI == 0:
for k in range(1,smo):
smooth_I[i] += smooth_I[i-k]
smooth_I[i] /= smo
return smooth_R,smooth_I
def theta(R,I):
L = 0
RI = smooth(R,I,3)
R = RI[0]
I = RI[1]
for length in range(len(R)):
if length == 0:
continue
else:
X_len = R[length]-R[length-1]
X = X_len**2
Y_len = I[length]-I[length-1]
Y = Y_len**2
long = (X+Y)**0.5
L += long
half_L = L/2.
temp_L = 0
for half in range(len(R)):
if half == 0:
continue
else:
X_len = R[half]-R[half-1]
temp_x = X_len**2
Y_len = I[half]-I[half-1]
temp_y = Y_len**2
temp = (temp_x + temp_y)**0.5
temp_L += temp
if temp_L >= half_L:
xm = R[half]
ym = I[half]
break
x1 = R[40]
y1 = I[40]
xN = R[10]
yN = I[10]
X = (y1-yN)/2. + (x1+xm)*(x1-xm)/(2.*(y1-ym)) - (xN+xm)*(xN-xm)/(2.*(yN-ym))
X = X/((x1-xm)/(y1-ym) - (xN-xm)/(yN-ym))
Y = (x1-xN)/2. + (y1+ym)*(y1-ym)/(2.*(x1-xm)) - (yN+ym)*(yN-ym)/(2.*(xN-xm))
Y = Y/((y1-ym)/(x1-xm) - (yN-ym)/(xN-xm))
r = ((xm-X)**2 + (ym-Y)**2)**0.5
Theta = L/r
return Theta*180/np.pi
def circle_fit(R,I):
RI = smooth(R,I,3)
smo_R = RI[0]
smo_I = RI[1]
L = 0
for length in range(len(smo_R)):
if length == 0:
continue
else:
X_len = smo_R[length]-smo_R[length-1]
X = X_len**2
Y_len = smo_I[length]-smo_I[length-1]
Y = Y_len**2
long = (X+Y)**0.5
L += long
Aa = []
for i in range(len(smo_R)):
Aa.append([])
Aa[i].append(2*smo_R[i])
Aa[i].append(2*smo_I[i])
Aa[i].append(1)
Aa = np.array(Aa)
Bb = []
for i in range(len(smo_R)):
z = smo_R[i]**2 + smo_I[i]**2
Bb.append(z)
Bb = np.array(Bb)
answer = np.linalg.inv(np.dot(Aa.T,Aa))
answer = np.dot(answer,Aa.T)
answer = np.dot(answer,Bb)
r = answer[2]+answer[1]**2 + answer[0]**2
r = r**0.5
Theta = L/r*180/np.pi
center = [answer[0],answer[1]]
return r, Theta, center
def cen_th(R,I):
circle = circle_fit(R,I)
center = circle[2]
L = 0
RI = smooth(R,I,3)
R = RI[0]
I = RI[1]
for length in range(len(R)):
if length == 0:
continue
else:
X_len = R[length]-R[length-1]
X = X_len**2
Y_len = I[length]-I[length-1]
Y = Y_len**2
long = (X+Y)**0.5
L += long
half_L = L/2.
temp_L = 0
for half in range(len(R)):
if half == 0:
continue
else:
X_len = R[half]-R[half-1]
temp_x = X_len**2
Y_len = I[half]-I[half-1]
temp_y = Y_len**2
temp = (temp_x + temp_y)**0.5
temp_L += temp
if temp_L >= half_L:
xm = R[half]
ym = I[half]
break
vec = [xm-center[0],ym-center[1]]
if vec[1] <=0:
way = -1
else:
way = 1
to_one = (vec[0]**2+vec[1]**2)**0.5
for_th = vec[0]/to_one
center_th = arccos(for_th)
return center_th,way
def ura(R,I):
vector = np.array([R[1]-R[-1],I[1]-I[-1]])
vec_val = (vector[0]**2+vector[1]**2)**0.5
vector /= vec_val
th = 0
for i in range(0,len(R)-1):
vector1 = np.array([R[i]-R[-1],I[i]-I[-1]])
vec_val = (vector1[0]**2+vector1[1]**2)**0.5
vector1 /= vec_val
COS = np.dot(vector,vector1)
th += np.arccos(COS)
vector = vector1
Theta = th*180/np.pi
return Theta
def calc_R(x,y, xc, yc):
""" calculate the distance of each 2D points from the center (xc, yc) """
return np.sqrt((x-xc)**2 + (y-yc)**2)
def f(c, x, y):
""" calculate the algebraic distance between the data points and the mean circle centered at c=(xc, yc) """
Ri = calc_R(x, y, *c)
return Ri - Ri.mean()
def leastsq_circle(x,y):
# coordinates of the barycenter
x_m = np.mean(x)
y_m = np.mean(y)
center_estimate = x_m, y_m
center, ier = optimize.leastsq(f, center_estimate, args=(x,y))
xc, yc = center
Ri = calc_R(x, y, *center)
R = Ri.mean()
residu = np.sum((Ri - R)**2)
return xc, yc, R, residu
def for_error(x,y,bo=0):
fitting = circle_fit(x,y)
radius = fitting[0]
Theta = fitting[1]
center = fitting[2]
xc = center[0]
yc = center[1]
center_theta = cen_th(x,y)[0]
if cen_th(x,y)[1] == -1:
center_theta = 2*np.pi-center_theta
theta_fit = np.linspace(center_theta - Theta*3.14/360,center_theta + Theta*3.14/360,len(x))
x_fit = xc + radius*np.cos(theta_fit)
y_fit = yc + radius*np.sin(theta_fit)
'''
plt.plot(x,y)
plt.plot(x_fit,y_fit)
plt.show()
'''
if bo == 1:
return x_fit,y_fit
error = []
for i in range(len(x)):
temp_list = []
for ii in range(len(x)):
temp = (x_fit[i]-x[ii])**2 + (y_fit[i]-y[ii])**2
temp_list.append(temp)
temp_min = min(temp_list)
for find in range(len(x)):
if temp_list[find] == temp_min:
error.append(temp_min**0.5)
dif = 0
for i in error:
dif += i/50
return dif
def scatter(theta_list,step):
x = []
Max = max(theta_list)
for i in range(int((Max+1)/step)):
x.append(i*step)
high = np.zeros(int((Max+1)/step))
for n in theta_list:
for k in range(int((Max+1)/step)):
if k == int((Max+1)/step)-1:
continue
else:
if x[k] <= n and x[k+1] >= n:
high[k] += 1
X = np.linspace(0,Max,int((Max+1)/step))
return X,high
def plot_lim(R,I):
plt.figure(figsize = (4,4))
plt.plot(R,I)
x_space = (max(R) - min(R))
y_space = (max(I) - min(I))
if x_space >= y_space:
plt.ylim((min(I),x_space+min(I)))
plt.xlim((min(R),max(R)))
elif y_space >= x_space:
plt.ylim((min(I),max(I)))
plt.xlim((min(R),y_space+min(R)))
return None
def ROT_UP(R,I):#before vector
L = 0
for length in range(len(R)):
if length == 0:
continue
else:
X_len = R[length]-R[length-1]
X = X_len**2
Y_len = I[length]-I[length-1]
Y = Y_len**2
long = (X+Y)**0.5
L += long
half_L = L/2.
temp_L = 0
for half in range(len(R)):
if half == 0:
continue
else:
X_len = R[half]-R[half-1]
temp_x = X_len**2
Y_len = I[half]-I[half-1]
temp_y = Y_len**2
temp = (temp_x + temp_y)**0.5
temp_L += temp
if temp_L >= half_L:
xm = R[half]
ym = I[half]
break
x1 = R[40]
y1 = I[40]
xN = R[10]
yN = I[10]
X = (y1-yN)/2. + (x1+xm)*(x1-xm)/(2.*(y1-ym)) - (xN+xm)*(xN-xm)/(2.*(yN-ym))
X = X/((x1-xm)/(y1-ym) - (xN-xm)/(yN-ym))
Y = (x1-xN)/2. + (y1+ym)*(y1-ym)/(2.*(x1-xm)) - (yN+ym)*(yN-ym)/(2.*(xN-xm))
Y = Y/((y1-ym)/(x1-xm) - (yN-ym)/(xN-xm))
vector = [X-xm,Y-ym]
up = np.array([0,1])
for_one = (vector[0]**2+vector[1]**2)**0.5
vector = np.array(vector)/for_one
COS = np.dot(up,vector)
TH = np.arccos(COS)
ROT_R = []
ROT_I = []
if X - xm > 0:
A = np.sin(TH)
B = np.cos(TH)
else:
A = np.sin(2*np.pi-TH)
B = np.cos(2*np.pi-TH)
turning = [[B,-A],[A,B]]
turning = np.array(turning)
for turn in range(len(R)):
vec = np.array([R[turn],I[turn]])
turn = np.dot(turning,vec)
ROT_R.append(turn[0])
ROT_I.append(turn[1])
return ROT_R, ROT_I
def bef_twoD2(data):
freq = data[0]
Amp = data[1]
phase = data[2]
Phase = lin_pha(freq,phase)
x_space = []
y_space = []
for ran in range(25,len(freq)-25):
R = []
I = []
for i in range(ran-25,ran+25):
R.append(Amp[i]*np.cos(Phase[i]))
I.append(Amp[i]*np.sin(Phase[i]))
x_space.append(max(R)-min(R))
y_space.append(max(I)-min(I))
return max(x_space), max(y_space)
def two_D2(R,I,NU,n):
N = int(n)
x_nu = NU[0]
y_nu = NU[1]
sorted_R = (max(R) + min(R))/2
sorted_I = (max(I) + min(I))/2
if x_nu > y_nu:
x_axis = np.linspace(sorted_R-x_nu*0.5,sorted_R+x_nu*0.5,N)
y_axis = np.linspace(sorted_I-x_nu*0.5,sorted_I+x_nu*0.5,N)
step = x_nu/float(n)
else:
x_axis = np.linspace(sorted_R-y_nu*0.5,sorted_R+y_nu*0.5,N)
y_axis = np.linspace(sorted_I-y_nu*0.5,sorted_I+y_nu*0.5,N)
step = y_nu/float(n)
x_pos = []
for R_mov in range(len(R)):
for x_mov in range(N):
if R[R_mov] >= x_axis[x_mov] -0.6*step and R[R_mov] <= x_axis[x_mov]+0.6*step:
break
x_pos.append(x_mov)
y_pos = []
for I_mov in range(len(I)):
for y_mov in range(N):
if I[I_mov] >= y_axis[y_mov] -0.6*step and I[I_mov] <= y_axis[y_mov]+0.6*step:
break
y_pos.append(y_mov)
twoD = np.zeros((N,N))
for i in range(len(R)):
twoD[x_pos[i]][y_pos[i]] = 1
if i == len(R)-1:
continue
else:
couple = [x_pos[i+1]-x_pos[i],y_pos[i+1]-y_pos[i]]
if abs(couple[0]) >= abs(couple[1]) and couple[1] != 0:
st = abs(couple[0]/couple[1])
k = 1
elif abs(couple[1]) >= abs(couple[0]) and couple[0] != 0:
st = abs(couple[1]/couple[0])
k = 0
else:
st = 1
k = 2
#------------------------
if st -int(st) >= 0.5:
st = int(st)+1
else:
st = int(st)
#-----------------------
if k == 1:
vec = abs(couple[0]/st)
if vec -int(vec) >= 0.5:
vec = int(vec) + 1
else:
vec = int(vec)
elif k == 0:
vec = abs(couple[1]/st)
if vec -int(vec) >= 0.5:
vec = int(vec) + 1
else:
vec = int(vec)
elif k == 2:
if couple[1] == 0:
vec = abs(couple[0])
if couple[0] == 0:
vec = abs(couple[1])
step = [0,0]
#------------------
for line in range(vec):
for ev_ste in range(st):
if k == 1 :
step[0] += 1
elif k == 0:
step[1] += 1
elif k == 2 and couple[1] == 0:
step[0] += 1
elif k == 2 and couple[0] == 0:
step[1] += 1
if couple[0] >= 0 and couple[1] >= 0:
dot = [step[0] + x_pos[i],step[1] + y_pos[i]]
elif couple[0] >= 0 and couple[1] <= 0:
dot = [step[0] + x_pos[i],-step[1] + y_pos[i]]
elif couple[0] <= 0 and couple[1] >= 0:
dot = [-step[0] + x_pos[i],step[1] + y_pos[i]]
elif couple[0] <= 0 and couple[1] <= 0:
dot = [-step[0] + x_pos[i],-step[1] + y_pos[i]]
if dot[0] >= N:
dot[0] = N-1
if dot[1] >= N:
dot[1] = N-1
twoD[dot[0]][dot[1]] = 1
if k == 1:
step[1] += 1
elif k == 0:
step[0] += 1
return twoD
#get RI matrix------------------------------------
def make_twoD(data,start,stop,n,Range = 50):
twoMat = []
freq = data[0]
Amp = data[1]
phase = data[2]
Phase = lin_pha(freq,phase)
Range /= 2
Range = int(Range)
x_space = []
y_space = []
for ran in range(Range,len(freq)-Range):
R = []
I = []
for i in range(ran-Range,ran+Range):
R.append(Amp[i]*np.cos(Phase[i]))
I.append(Amp[i]*np.sin(Phase[i]))
x_space.append(max(R)-min(R))
y_space.append(max(I)-min(I))
NU = [max(x_space), max(y_space)]
for i in range(start,stop):
R = []
I = []
for ii in range(i-Range,i+Range):
R.append(Amp[ii]*np.cos(Phase[ii]))
I.append(Amp[ii]*np.sin(Phase[ii]))
twoMat.append(two_D2(R,I,NU,n))
return twoMat
def dots(twoD):
num = 0
for i in twoD:
for ii in i:
if ii == 1:
num += 1
return num
def save_data(start,file_name,k,TF = 0):
title_log = IV_Retrieve_Titles_log_py(start)# take log
log = title_log[1]
log_name = "0 to %s step 1" %log
freq = []
Amp = []
phase = []
data = freq_analysis(start,log_name,0) #get data
freq = data[0]
Amp = data[1]
phase = data[2]
if TF == "connect log":
for i in range(log-k):
data = freq_analysis(start,log_name,i+1)
freq += data[0]
Amp += data[1]
phase += data[2]
file = "./txt_data/"+file_name
folder = os.path.exists(file)
if not folder:
os.makedirs(file)
data = [freq,Amp,phase]
data_na = ["freq","Amp","phase"]
k = 0
for name in data_na:
f = open( "./txt_data/"+file_name+"/"+name+".txt","w")
for i in data[k]:
f.write(" " + str(i))
k+=1
return None
def for_origin(file_name,name,data):
file = "./txt_data/origin/"+file_name
folder = os.path.exists(file)
if not folder:
os.makedirs(file)
k = 0
f = open( "./txt_data/origin/"+file_name+"/"+name+".txt","w")
for i in range(len(data[0])):
f.write( str(data[0][i]) + " " + str(data[1][i]) + "\n")
return None
def two_axis(data,start,stop):
fig = plt.figure(figsize=(8,4))
ax1 = fig.add_subplot(111)
ax1.plot(data[0][start:stop],data[2][start:stop],label = "phase")
ax1.set_ylabel('phase after mmf')
ax1.set_title("after mmf from %s to %s" %(round(data[0][start],2),round(data[0][stop],2)))
#plt.xlim(4.5,5)
ax2 = ax1.twinx() # this is the important function
ax2.plot(data[0][start:stop],data[1][start:stop],label = "amp",color = 'r')
ax2.set_ylabel('amp after mmf')
ax2.set_xlabel('freq')
plt.legend()
plt.show()