forked from Smithsonian/adc_tests
/
rww_tools.py
775 lines (712 loc) · 23.4 KB
/
rww_tools.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
# run 'ipython --pylab -i'
# and then in ipython: '%run -i rww_tools.py'
# the -i there causes rww_tools.py to be run in the interactive name space
import sys
import os
import time
from corr import katcp_wrapper
from corr.snap import snapshots_get
from struct import pack, unpack
from scipy.signal import correlate
from scipy.fftpack import fft, rfft
#import katcp_wrapper
roach2=katcp_wrapper.FpgaClient('roach2-00.cfa.harvard.edu')
zdok=0
import adc5g
import matplotlib.pyplot as plt
from matplotlib import mlab
import numpy as np
from numpy import math
import fit_cores
lanio = "lanio 131.142.9.146 "
freq = 10.070801
pwr = 1.0
numpoints=16384
samp_freq = 5000.0
snap_name = "scope_raw_0_snap"
prog_name = 'adc5g_test.bof'
def dosnap(fr=0, name="t", rpt = 1, donot_clear=False, plot=True):
"""
Takes a snapshot and uses fit_cores to fit a sine function to each
core separately assuming a CW signal is connected to the input. The
offset, gain and phase differences are reoprted for each core as
well as the average of all four.
The parameters are:
fr The frequency of the signal generator. It will default to the last
frequency set by set_freq()
name the name of the file into which the snapshot is written. 5 other
files are written. Name.c1 .. name.c4 contain themeasurements from
cores a, b, c and d. Note that data is taken from cores in the order
a, c, b, d. A line is appended to the file name.fit containing
signal freq, average zero, average amplitude followed by triplets
of zero, amplitude and phase differences for cores a, b, c and d
rpt The number of repeats. Defaults to 1. The c1 .. c4 files mentioned
above are overwritten with each repeat, but new rows of data are added
to the .fit file for each pass.
"""
global freq
avg_pwr_sinad = 0
if fr == 0:
fr = freq
for i in range(rpt):
snap=adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2)
if(plot):
plt.clf()
plt.plot(snap)
plt.show(block = False)
rmsSnap = np.std(snap)
loadingFactor = -20.0*math.log10(128/rmsSnap)
print "Rms = %f, loading factor = %f" % (rmsSnap,loadingFactor)
np.savetxt(name, snap,fmt='%d')
ogp, pwr_sinad = fit_cores.fit_snap(fr, samp_freq, name,\
clear_avgs = i == 0 and not donot_clear, prnt = i == rpt-1)
avg_pwr_sinad += pwr_sinad
return ogp, avg_pwr_sinad/rpt
def dosim(freq=10.070801, name="sim", rpt = 1, exact=True):
"""
Do the same analysis as dosnap, but on simulated data.
The arguments have the same meaning as dosnap except that freq is not
coupled to the global variable. A random phase is generated for each pass
"""
for i in range(rpt):
snap=get_sim_data(freq, exact)
np.savetxt(name, snap,fmt='%d')
fit_cores.fit_snap(freq, samp_freq, name, i == 0)
def simpsd(freq=318.0, rpt = 1, exact=True):
"""
Make a simulated snapshot and do the psd analysis on it. The
sine wave will have a randon start phase.
"""
for i in range(rpt):
data = get_sim_data(freq, exact)
power, freqs = mlab.psd(data, numpoints, Fs=samp_freq*1e6, \
detrend=mlab.detrend_mean, scale_by_freq=True)
plt.clf()
if i == 0:
sp = power
else:
sp += power
sp /= rpt
print "about to plot", len(freqs)
plt.step(freqs, 10*np.log10(sp))
plt.show(block = False)
fd = open("sim.psd", 'w')
for i in range(len(sp)):
print >>fd, "%7.2f %6.1f" % (freqs[i]/1e6, 10*np.log10(sp[i]))
def get_sim_data(freq, exact=True):
"""
Make a simulated snapshot of data
"""
if exact:
offs = [0,0,0,0]
gains = [1,1,1,1]
else:
offs = [.2, .3, -.2, -.1]
gains = [1.001, .9984, .999, 1.002]
del_phi = 2 * math.pi * freq / samp_freq
data = np.empty((numpoints), dtype='int32')
phase = 2*math.pi * np.random.uniform()
for n in range(numpoints):
core = n&3
data[n] = (math.floor(0.5 + 119.0 * math.sin(del_phi * n + phase) + \
offs[core]))*gains[core]
return data
def dotest(plotcore = 1):
"""
Put the adc in test mode and get a sample of the test vector. Plot core 1
by default.
"""
global snap_name
adc5g.set_spi_control(roach2, zdok, test=1)
cores = (corea, corec, coreb, cored) = adc5g.get_test_vector(roach2, [snap_name,])
if plotcore == 2:
plotcore = 3
elif plotcore == 3:
plotcore = 2
plt.clf()
plt.plot(cores[plotcore])
plt.show(block=False)
adc5g.set_spi_control(roach2, zdok)
def dopsd(nfft = numpoints, rpt = 10):
"""
Takes a snapshot, then computes, plots and writes out the Power Spectral
Density functions. The psd function is written into a file named "psd".
This file will be overwritten with each call. Arguments:
nfft The number of points in the psd function. Defaults to 16384. Since
a snapshot has 16384 points, this is the maximum which should be used
rpt The numper of mesurements to be averaged for the plot and output file.
"""
for i in range(rpt):
power, freqs = adc5g.get_psd(roach2, snap_name, samp_freq*1e6, 8, nfft)
if i == 0:
sp = power
else:
sp += power
sp /= rpt
plt.step(freqs, 10*np.log10(sp))
plt.show(block = False)
data = np.column_stack((freqs/1e6, 10*np.log10(sp)))
np.savetxt("psd", data, fmt=('%7.2f', '%6.1f'))
def dopsdcores(nfft = numpoints/4, rpt = 10):
for i in range(rpt):
snap=adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2)
power, freqs = mlab.psd(snap, nfft*4, Fs=samp_freq*1e6, detrend=mlab.detrend_mean, \
scale_by_freq=False)
if i == 0:
psd_all = power[:1+nfft/2]
else:
psd_all += power[:1+nfft/2]
power, freqs = mlab.psd(snap[0:: 4], nfft, Fs=samp_freq*.25e6, detrend=mlab.detrend_mean,\
scale_by_freq=True)
if i == 0:
psd1 = power
else:
psd1 += power
power, freqs = mlab.psd(snap[1:: 4], nfft, Fs=samp_freq*.25e6, detrend=mlab.detrend_mean,\
scale_by_freq=True)
if i == 0:
psd2 = power
else:
psd2 += power
power, freqs = mlab.psd(snap[2:: 4], nfft, Fs=samp_freq*.25e6, detrend=mlab.detrend_mean,\
scale_by_freq=True)
if i == 0:
psd3 = power
else:
psd3 += power
power, freqs = mlab.psd(snap[3:: 4], nfft, Fs=samp_freq*.25e6, detrend=mlab.detrend_mean,\
scale_by_freq=True)
if i == 0:
psd4 = power
else:
psd4 += power
data = np.column_stack((freqs*1e-6, 10*np.log10(psd_all/rpt), \
10*np.log10(psd1/rpt), \
10*np.log10(psd2/rpt), 10*np.log10(psd3/rpt), 10*np.log10(psd4/rpt)))
np.savetxt("psd_cores", data, fmt=('%7.2f'))
def hist_from_snapshots(rpt = 10):
# hist_all = np.zeros(256,dtype=int)
hist1 = np.zeros(256,dtype=int)
hist2 = np.zeros(256,dtype=int)
hist3 = np.zeros(256,dtype=int)
hist4 = np.zeros(256,dtype=int)
for i in range(rpt):
snap=adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2)
snap = 128 + np.array(snap)
# hist = np.bincount(snap, minlength=256)
# hist_all += hist
hist = np.bincount(snap[0:: 4], minlength=256)
hist1 += hist
hist = np.bincount(snap[1:: 4], minlength=256)
hist2 += hist
hist = np.bincount(snap[2:: 4], minlength=256)
hist3 += hist
hist = np.bincount(snap[3:: 4], minlength=256)
hist4 += hist
data=np.column_stack((np.arange(-128., 128, dtype=int), hist1, hist2,
hist3, hist4))
np.savetxt("hist_cores", data, fmt=("%d"))
# print "all ",np.sum(hist_all[0:128]), np.sum(hist_all[128:256])
print "core a ",np.sum(hist1[0:128]), np.sum(hist1[129:256])
print "core b ",np.sum(hist3[0:128]), np.sum(hist3[129:256])
print "core c ",np.sum(hist2[0:128]), np.sum(hist2[129:256])
print "core d ",np.sum(hist4[0:128]), np.sum(hist4[129:256])
# For now get_histogram has cores b and c reversed.
def get_hist(fname="hist_cores"):
data = np.empty(shape=(256,5), dtype=int)
for c in range(4):
data[:, c+1] = adc5g.get_histogram(roach2, zdok, "acbd"[c])
data[:,0] = range(-128, 128)
np.savetxt(fname, data, fmt=("%d"))
def multifreq(start=100, end=560, step=50, repeat=5, do_sfdr=False):
"""
Calls dosnap for a range of frequenciesi in MHz. The actual frequencies are
picked to have an odd number of cycles in the 16384 point snapshot.
"""
sfd = open('sinad', 'a')
f = samp_freq / numpoints
nstart = int(0.5+start/f)
nend = int(0.5+end/f)
nstep = int(0.5+step/f)
for n in range(nstart, nend, nstep):
freq = f*n
set_freq(freq)
# ogp, avg_pwr_sinad = dosnap(rpt=repeat, donot_clear = False)
ogp, avg_pwr_sinad = dosnap(rpt=repeat, donot_clear = n!=nstart, plot=False)
sinad = 10.0*np.log10(avg_pwr_sinad)
print >>sfd, "%8.3f %7.2f" % (freq, sinad)
if do_sfdr:
dopsd(rpt=3)
fit_cores.dosfdr(freq)
np.savetxt("ogp.meas", ogp[3:], fmt="%8.4f")
fit_cores.fit_inl()
def freqResp(start=100, end=2400, delta=50, repeat=10,powerlevel=7):
"""
Runs adc5g.get_psd for a range of frequenciesi in MHz.
The actual frequencies are picked to have an odd number of
cycles in the 16384 point snapshot. (This part is copied from multifreq()).
Writes out freq and max() of spectrum to freqResponse.dat file.
"""
set_pwr(powerlevel)
frfile = open('freqResponse.dat', 'a')
f = samp_freq / numpoints
nstart = int(0.5+start/f)
nend = int(0.5+end/f)
nstep = int(0.5+delta/f)
for n in range(nstart, nend, nstep):
freq = f*n
set_freq(freq)
# dopsd(rpt=3)
for i in range(repeat):
power, freqs = adc5g.get_psd(roach2, snap_name, samp_freq*1e6, 8, numpoints)
if i == 0:
sp = power
else:
sp += power
sp /= repeat
power=10*log10(sp)
# step(freqs, power)
peakpower=max(power)
print freq,peakpower
output="%f %f\n" % (freq,peakpower)
frfile.write(output)
frfile.close()
def multipwr(start = 1, end = -40, step = -3, repeat=10):
"""
Calls dosnap for a range of powers
"""
for n in range(start, end, step):
set_pwr(n)
dosnap(rpt=repeat)
def update_ogp(fname = 'ogp.meas', set=True):
"""
Retreive the ogp data from the ADC and add in the corrections from
the measured ogp (in ogp.meas). Store in the file 'ogp'
"""
cur_ogp = get_ogp_array()
meas_ogp = np.genfromtxt(fname)
# Correct for the ~1.4X larger effect of the phase registers than expected
for i in (2,5,8,11):
meas_ogp[i] *= 0.65
np.savetxt('ogp', cur_ogp+meas_ogp, fmt="%8.4f")
if set:
set_ogp()
def update_inl(fname = 'inl.meas', set=True):
"""
Retreive the INL data from the ADC and add in the corrections from
the measured inl (in inl.meas). Store in the file 'inl'
"""
cur_inl = get_inl_array()
meas_inl = np.genfromtxt(fname)
for level in range(17):
cur_inl[level][1:] += meas_inl[level][1:]
np.savetxt("inl", cur_inl, fmt=('%3d','%7.4f','%7.4f','%7.4f','%7.4f'))
if set:
set_inl()
def program():
"""
Program the roach2 with the standard program. After this, calibrate()
should be called
"""
global prog_name, roach2, samp_freq
roach2.progdev(prog_name)
adc5g.set_spi_control(roach2, zdok)
if prog_name[:8] == 'sma_corr':
print "this is correlator code"
roach2.write_int('source_ctrl', 18)
roach2.write_int('scope_ctrl', 1536)
samp_freq = 2288.
numpoints = 32768
set_zdok(zdok)
def calibrate(verbose=False):
"""
Call Rurik's routine to calibrate the time delay at the adc interface.
"""
global zdok
adc5g.set_test_mode(roach2, 0)
adc5g.set_test_mode(roach2, 1)
adc5g.sync_adc(roach2)
save_zdok = zdok
set_zdok(0)
opt0, glitches0 = adc5g.calibrate_mmcm_phase(roach2, 0, \
[snap_name,])
if verbose or (opt0 == None):
print "zodk0 ", opt0, glitches0
else:
print "zodk0", opt0
set_zdok(1)
opt1, glitches1 = adc5g.calibrate_mmcm_phase(roach2, 1, \
[snap_name,])
if verbose or (opt1 == None):
print "zodk1 ", opt1, glitches1
else:
print "zodk1", opt1
set_zdok(save_zdok)
adc5g.unset_test_mode(roach2, 0)
adc5g.unset_test_mode(roach2, 1)
# t = adc5g.calibrate_mmcm_phase(roach2, zdok, [snap_name,], bitwidth=8)
# print t
def clear_ogp():
"""
Clear all of the Offset, Gain and Phase corrections registers on the adc.
"""
for core in range(1,5):
adc5g.set_spi_gain(roach2,zdok, core, 0)
adc5g.set_spi_offset(roach2,zdok, core, 0)
adc5g.set_spi_phase(roach2,zdok, core, 0)
def get_ogp():
"""
Use get_ogp_array to get the Offset, Gain and Phase corrections
and print them.
"""
ogp = get_ogp_array()
print "zero(mV) amp(%%) dly(ps) (adj by .4, .14, .11)"
print "core A %7.4f %7.4f %8.4f" % (ogp[0], ogp[1], ogp[2])
print "core B %7.4f %7.4f %8.4f" % (ogp[3], ogp[4], ogp[5])
print "core C %7.4f %7.4f %8.4f" % (ogp[6], ogp[7], ogp[8])
print "core D %7.4f %7.4f %8.4f" % (ogp[9], ogp[10], ogp[11])
def set_ogp(fname = 'ogp'):
"""
Clear the control register and then load the offset, gain and phase
registers for each core. These values are hard coded for now.
"""
adc5g.set_spi_control(roach2, zdok)
t = np.genfromtxt(fname)
set_offs(t[0], t[3], t[6], t[9])
set_gains(t[1], t[4], t[7], t[10])
set_phase(t[2], t[5], t[8], t[11])
def clear_inl():
"""
Clear the INL registers on teh ADC
"""
offs = [0.0]*17
for chan in range(1,5):
adc5g.set_inl_registers(roach2, zdok, chan, offs)
def get_inl():
"""
Use get_inl_array to get the INL registers from the ADC and then
print them.
"""
a = get_inl_array()
print "lvl A B C D"
for level in range(17):
print "%3d %5.2f %5.2f %5.2f %5.2f" % tuple(a[level])
def set_inl(fname = 'inl'):
"""
Set the INL registers for all four cores from a file containing 17 rows
of 5 columns. The first column contains the level and is ignored.
Columns 2-5 contain the inl correction for cores a-d
"""
c = np.genfromtxt(fname, usecols=(1,2,3,4), unpack=True)
adc5g.set_inl_registers(roach2,zdok,1,c[0])
adc5g.set_inl_registers(roach2,zdok,2,c[1])
adc5g.set_inl_registers(roach2,zdok,3,c[2])
adc5g.set_inl_registers(roach2,zdok,4,c[3])
def set_freq(fr, centered = True, prnt=True):
"""
Set the synthesizer frequency (MHz) and save the value for use by dosnap(),
etc. If centered is True, pick the closest frequency in the center of a
channel with an odd number of cycles in a snapshot.
"""
global freq
if centered:
base_freq = samp_freq / numpoints
n = 2*int(fr/(2.0*base_freq))+1
freq = base_freq*n
print "n, freq = ", n, freq
else:
freq=fr
os.system(lanio + "\":FREQ " + str(freq) + " MHz\"")
if prnt:
print "%.6f" % (freq)
time.sleep(0.5)
def get_freq():
"""
Retreive the frequency from the Agilent Synthesizer and print it (in Hz).
"""
print os.system(lanio + "\"FREQ?\"")
def set_pwr(p):
"""
Set the synthesizer power and save the value for use by dosnap(), etc.
"""
global pwr
pwr = p
os.system(lanio + "\":POW " + str(p) + " dBm\"")
os.system(lanio + "\":OUTP 1\"")
def get_pwr():
"""
Retreive the power level from the Agilent Synthesizer and print it.
"""
print os.system(lanio + "\"POW?\"")
def set_offs(o1, o2, o3, o4):
"""
Set the offsets for each core in the order a, b, c, d.
"""
t = float(o1)
print math.floor(.5+t*255/100.)+0x80,
adc5g.set_spi_offset(roach2,zdok, 1, t)
t = float(o2)
print math.floor(.5+t*255/100.)+0x80,
adc5g.set_spi_offset(roach2,zdok, 2, t)
t = float(o3)
print math.floor(.5+t*255/100.)+0x80,
adc5g.set_spi_offset(roach2,zdok, 3, t)
t = float(o4)
print math.floor(.5+t*255/100.)+0x80
adc5g.set_spi_offset(roach2,zdok, 4, t)
def get_offs():
"""
Get and print the offsets for the four cores of the ADC.
"""
for i in range(1,5):
print "%.3f " % adc5g.get_spi_offset(roach2,zdok,i),
print
def set_gains(g1, g2, g3, g4):
"""
Set the gains for each core in the order a, b, c, d.
"""
t = float(g1)
print math.floor(.5+t*255/36.)+0x80,
adc5g.set_spi_gain(roach2,zdok, 1, t)
t = float(g2)
print math.floor(.5+t*255/36.)+0x80,
adc5g.set_spi_gain(roach2,zdok, 2, t)
t = float(g3)
print math.floor(.5+t*255/36.)+0x80,
adc5g.set_spi_gain(roach2,zdok, 3, t)
t = float(g4)
print math.floor(.5+t*255/36.)+0x80
adc5g.set_spi_gain(roach2,zdok, 4, t)
def get_gains():
"""
Get and print the gains for the four cores of the ADC.
"""
for i in range(1,5):
print "%.3f " % adc5g.get_spi_gain(roach2,zdok,i),
print
def set_phase(p1, p2, p3, p4):
"""
Set the phases (delays) for each core in the order a, b, c, d.
"""
t = float(p1)
print math.floor(.5+t*255/28.)+0x80,
adc5g.set_spi_phase(roach2,zdok, 1, t)
t = float(p2)
print math.floor(.5+t*255/28.)+0x80,
adc5g.set_spi_phase(roach2,zdok, 2, t)
t = float(p3)
print math.floor(.5+t*255/28.)+0x80,
adc5g.set_spi_phase(roach2,zdok, 3, t)
t = float(p4)
print math.floor(.5+t*255/28.)+0x80
adc5g.set_spi_phase(roach2,zdok, 4, t)
def get_phase():
"""
Get and print the delays for the four cores of the ADC.
"""
for i in range(1,5):
print "%.3f " % adc5g.get_spi_phase(roach2,zdok,i),
print
def get_inl_array():
"""
Read the INL corrections from the adc and put in an array
"""
inl = np.zeros((5,17), dtype='float')
for chan in range(1,5):
inl[chan] = adc5g.get_inl_registers(roach2, zdok, chan)
inl[0] = range(0, 257,16)
return inl.transpose()
def get_ogp_array():
"""
Read the Offset, Gain and Phase corrections for each core from the ADC
and return in a 1D array
"""
ogp = np.zeros((12), dtype='float')
indx = 0
for chan in range(1,5):
ogp[indx] = adc5g.get_spi_offset(roach2,zdok,chan)
indx += 1
ogp[indx] = adc5g.get_spi_gain(roach2,zdok,chan)
indx += 1
ogp[indx] = adc5g.get_spi_phase(roach2,zdok,chan)
indx += 1
return ogp
def set_zdok(zd):
global zdok, snap_name, prog_name
if prog_name[:8] == 'sma_corr':
snap_name = "scope_snap%d" % (zd)
else:
snap_name = "scope_raw_%d_snap" % (zd)
zdok = zd
def get_zdok():
print "zdok %d, snapshot %s" % (zdok, snap_name)
def setup(r_name='roach2-00', prg_nam='sma_corr_2014_Apr_21_1603.bof.gz'):
global roach2, prog_name, zdok, samp_freq, numpoints
roach2=katcp_wrapper.FpgaClient(r_name)
connected = roach2.wait_connected(timeout=2)
if connected == False:
raise RuntimeError("Unable to connect to %s" %(r_name))
# print "Unable to connect to %s" %(r_name)
# return connected
prog_name = prg_nam
set_zdok(zdok)
if prog_name[:8] == 'sma_corr':
print "this is correlator code"
samp_freq = 2288.
numpoints = 32768
# return connected
def og_from_noise(fname="ogp.noise", rpt=100):
"""
Take a number of snapshots of noise. Analyze for offset and gain
for each core separately.
"""
sum_result = np.zeros((15), dtype=float)
sum_cnt = 0
for n in range(rpt):
result = np.zeros((15), dtype=float)
snap=adc5g.get_snapshot(roach2, snap_name, man_trig=True, wait_period=2)
if(rpt == 1):
np.savetxt("t.og_noise", snap,fmt='%d')
l=float(len(snap))
snap_off=np.sum(snap)/l
snap_amp=np.sum(abs(snap-snap_off))/l
result[0]=snap_off*(-500.0/256.0)
result[1]=snap_amp
for core in range(4):
# This will actually sample the cores in the order A,C,B,D
# index will fix this up when data is put in the result array
index=(3,9,6,12)[core]
c=snap[core::4]
l=float(len(c))
off=np.sum(c)/l
result[index] = off*(-500.0/256.0)
amp=np.sum(abs(c-off))/l
result[index+1]= 100.0*(snap_amp-amp)/snap_amp
sum_result += result
sum_cnt += 1
print "%.4f "*15 % tuple(result)
sum_result /= sum_cnt
print "%.4f "*15 % tuple(sum_result)
np.savetxt(fname, sum_result[3:], fmt="%8.4f")
def phase_curve():
f= 28/255.
ofd = open('phasecurve', 'w')
p = {}
for i in range(1,5):
p[i] = adc5g.get_spi_phase(roach2,zdok,i)
for i in range(-10,11):
set_phase(p[1]+ f*i,p[2] -f*i,p[3],p[4])
ogp, gar = dosnap(rpt=5, plot=False)
print >>ofd, "%.3f %.3f %.3f" % (f*i, ogp[5], ogp[8])
set_phase(p[1],p[2],p[3],p[4])
def dohist(base_name='hist', type='sin', gethist=True, plt=True):
hc_name=base_name+'_cores'
if gethist:
get_hist(fname=hc_name)
res = np.empty([5, 256], dtype=float)
res[0] = np.arange(256, dtype=float)
z_fact = 500.0/256.0
(a1,z1), res[1] =fit_cores.fit_hist(1,type, hc_name)
(a2,z2), res[2] =fit_cores.fit_hist(2,type, hc_name)
(a3,z3), res[3] =fit_cores.fit_hist(3,type, hc_name)
(a4,z4), res[4] =fit_cores.fit_hist(4,type, hc_name)
avamp = (a1+a2+a3+a4)/4.0
# Reverse the amplitude and zero differences so they can be applied to the
# offset and gain registers directly. The phase registers don't need the
# reversal
a1p = 100*(avamp -a1)/avamp
a2p = 100*(avamp -a2)/avamp
a3p = 100*(avamp -a3)/avamp
a4p = 100*(avamp -a4)/avamp
ogp=np.array([z_fact*z1, a1p, 0, z_fact*z2, a2p, 0, z_fact*z3, a3p, 0, \
z_fact*z4, a4p, 0])
avz=(z1+z2+z3+z4)*z_fact/4.0
print "#avg %7.4f %7.4f %8.4f" % (ogp[1], avamp, 0)
print "core A %7.4f %7.4f %8.4f" % tuple(ogp[0:3])
print "core B %7.4f %7.4f %8.4f" % tuple(ogp[3:6])
print "core C %7.4f %7.4f %8.4f" % tuple(ogp[6:9])
print "core D %7.4f %7.4f %8.4f" % tuple(ogp[9:12])
np.savetxt(base_name+"_ogp.meas", ogp, fmt= "%8.4f")
r_name=base_name+'.res'
np.savetxt(r_name, np.transpose(res), fmt='%3i %6.3f %6.3f %6.3f %6.3f')
fit_cores.fit_inl(fname=r_name)
if plt:
plotres(r_name)
def plotres(fname="hist.res",title=""):
res = np.genfromtxt(fname, unpack=True)
plt.clf()
plt.plot(res[0][1:-1], res[1][1:-1], label='core a')
plt.plot(res[0][1:-1], res[2][1:-1], label='core b')
plt.plot(res[0][1:-1], res[3][1:-1], label='core c')
plt.plot(res[0][1:-1], res[4][1:-1], label='core d')
plt.legend(loc=0)
if title != "":
plt.title(title)
plt.show(block=False)
def get_snaps():
global raw0, raw1
data = snapshots_get([roach2, roach2], ['scope_raw_0_snap', \
'scope_raw_1_snap'])
raw0 = np.array(unpack('%ib' % data['lengths'][0], data['data'][0]), \
dtype=float)
raw1 = np.array(unpack('%ib' % data['lengths'][1], data['data'][1]), \
dtype=float)
def set_adc_delay(cntr_chan = 0):
del0 = 0
del1 = 0
if(cntr_chan > 0):
del1 = cntr_chan
else:
del0 = -cntr_chan
roach2.write('cdelay_ctrl', pack('>I', (1<<31) + (1<<30) + \
(del1<<15) + del0),0)
def xcorr_snaps(set_delay = True):
global raw0, raw1, xcn
if(set_delay):
set_adc_delay(0)
get_snaps()
xcn = xcorr(raw0, raw1, maxlags=100, normed=True)
maxchan = np.argmax(xcn[1])
cntr_chan = xcn[0][maxchan]
if(set_delay):
set_adc_delay(cntr_chan)
print xcn[1][maxchan], cntr_chan
def fx_snaps(n = 10):
global raw0, raw1, xc, xn
for cnt in range(n):
get_snaps()
ft0=fft(raw0)[:8192]
ft1=fft(raw1)[:8192]
if cnt == 0:
xc = ft0*conjugate(ft1)
ac0 = ft0*conjugate(ft0)
ac1 = ft1*conjugate(ft1)
# ac0 = abs(ft0)*abs(ft0)
# ac1 = abs(ft1)*abs(ft1)
else:
xc += ft0*conjugate(ft1)
ac0 += ft0*conjugate(ft0)
ac1 += ft1*conjugate(ft1)
# ac0 += abs(ft0)*abs(ft0)
# ac1 += abs(ft1)*abs(ft1)
xn = xc/(sqrt(ac0*ac1))
np.savetxt('xn.txt', xn.view(float).reshape(-1, 2))
print np.mean(abs(xn)), "+-", np.std(abs(xn))
if len(sys.argv) >= 2:
print "argv[1] = ", sys.argv[1]
if sys.argv[1][:2] == "-x":
fn = sys.argv[1][2:]
print "About to execute commands in = ",fn
execfile(fn)
if __name__ == "__main__":
command = sys.argv[1]
for roach2_host in sys.argv[2:]:
roach2 = katcp_wrapper.FpgaClient(roach2_host)
roach2.wait_connected()
for zdok in [0, 1]:
set_zdok(zdok)
clear_ogp()
if command == "update":
print "Running og_from_noise for %s:zdok=%d" % (roach2_host, zdok)
og_from_noise("og%d.noise" % zdok)
set_ogp(fname="og%d.noise" % zdok)