initial check-in
This commit is contained in:
@ -0,0 +1,178 @@
|
||||
# -*- coding: iso-8859-1 -*-
|
||||
|
||||
TXEnableDelay = 2e-6
|
||||
TXEnableValue = 0b0001 # TTL line blanking RF amplifier (bit 0)
|
||||
TXPulseValue = 0b0010 # TTL line triggering RF pulses (bit 1)
|
||||
ADCSensitivity = 2 # voltage span for ADC
|
||||
|
||||
def experiment(): # saturation-recovery with soild-echo detection
|
||||
|
||||
# set up acquisition parameters:
|
||||
pars = {}
|
||||
pars['P90'] = 2.3e-6 # 90-degree pulse length (s)
|
||||
pars['SF'] = 46.7e6 # spectrometer frequency (Hz)
|
||||
pars['O1'] = 5.6e3 # offset from SF (Hz)
|
||||
pars['SW'] = 10e6 # spectral window (Hz)
|
||||
pars['SI'] = 1*1024 # number of acquisition points
|
||||
pars['NS'] = 16 # number of scans
|
||||
pars['DS'] = 0 # number of dummy scans
|
||||
pars['TAU'] = 1 # delay for recovery (s)
|
||||
pars['D3'] = 20e-6 # echo delay (s)
|
||||
pars['D4'] = 0 # echo pre-aquisition delay (s)
|
||||
pars['PHA'] = -30 # receiver phase (degree)
|
||||
# -*- these ain't variable: -*-
|
||||
pars['NECH'] = 40 # number of saturation pulses
|
||||
pars['D1'] = 100e-3 # starting interval in saturation sequence (s)
|
||||
pars['D2'] = 1e-4 # end interval in saturation sequence (s)
|
||||
pars['DATADIR'] = '/home/fprak/Students/' # data directory
|
||||
pars['OUTFILE'] = None # output file name
|
||||
|
||||
# specify a variable parameter (optional):
|
||||
pars['VAR_PAR'] = 'TAU' # variable parameter name (a string)
|
||||
start = 1e-4 # starting value
|
||||
stop = 1e-0 # end value
|
||||
steps = 10 # number of values
|
||||
log_scale = True # log scale flag
|
||||
stag_range = True # staggered range flag
|
||||
|
||||
# check parameters for safety:
|
||||
if pars['PHA'] < 0:
|
||||
pars['PHA'] = 360 + pars['PHA']
|
||||
|
||||
if pars['P90'] > 20e-6:
|
||||
raise Exception("pulse too long!!!")
|
||||
|
||||
# check whether a variable parameter is named:
|
||||
var_key = pars.get('VAR_PAR')
|
||||
if var_key == 'P90' and (start > 20e-6 or stop > 20e-6):
|
||||
raise Exception("Pulse too long!!!")
|
||||
|
||||
if pars['NS']%8 != 0:
|
||||
pars['NS'] = int(round(pars['NS'] / 8) + 1) * 8
|
||||
print 'Number of scans changed to ',pars['NS'],' due to phase cycling'
|
||||
|
||||
if pars['D1'] < pars['D2']:
|
||||
raise Exception("D1 must be greater than D2!")
|
||||
|
||||
sat_length = sum(log_range(pars['D1'],pars['D2'],pars['NECH']))
|
||||
if sat_length > 1.:
|
||||
raise Exception("saturation sequence too long!!!")
|
||||
|
||||
# start the experiment:
|
||||
if var_key:
|
||||
# this is an arrayed experiment:
|
||||
if log_scale:
|
||||
array = log_range(start, stop, steps)
|
||||
else:
|
||||
array = lin_range(start, stop, steps)
|
||||
|
||||
if stag_range:
|
||||
array = staggered_range(array, size = 2)
|
||||
|
||||
# estimate the experiment time:
|
||||
if var_key == 'TAU':
|
||||
seconds = ((sat_length + pars['D3']*2) * steps + sum(array)) * (pars['NS'] + pars['DS'])
|
||||
elif var_key == 'D3':
|
||||
seconds = ((sat_length + pars['TAU']) * steps + sum(array)*2) * (pars['NS'] + pars['DS'])
|
||||
else:
|
||||
seconds = (sat_length + pars['TAU'] + pars['D3']*2) * steps * (pars['NS']+ pars['DS'])
|
||||
m, s = divmod(seconds, 60)
|
||||
h, m = divmod(m, 60)
|
||||
print '%s%02d:%02d:%02d' % ('Experiment time estimated: ', h, m, s)
|
||||
|
||||
# loop for a variable parameter:
|
||||
for index, pars[var_key] in enumerate(array):
|
||||
print 'Arrayed experiment for '+var_key+': run = '+str(index+1)+\
|
||||
' out of '+str(array.size)+': value = '+str(pars[var_key])
|
||||
# loop for accumulation:
|
||||
for run in xrange(pars['NS']+pars['DS']):
|
||||
yield satrec2_experiment(pars, run)
|
||||
synchronize()
|
||||
else:
|
||||
# estimate the experiment time:
|
||||
seconds = (sat_length + pars['TAU'] + pars['D3']*2) * (pars['NS']+ pars['DS'])
|
||||
m, s = divmod(seconds, 60)
|
||||
h, m = divmod(m, 60)
|
||||
print '%s%02d:%02d:%02d' % ('Experiment time estimated: ', h, m, s)
|
||||
|
||||
# loop for accumulation:
|
||||
for run in xrange(pars['NS']+pars['DS']):
|
||||
yield satrec2_experiment(pars, run)
|
||||
|
||||
|
||||
# the pulse program:
|
||||
|
||||
def satrec2_experiment(pars, run):
|
||||
e=Experiment()
|
||||
|
||||
dummy_scans = pars.get('DS')
|
||||
if dummy_scans:
|
||||
run -= dummy_scans
|
||||
|
||||
pars['PROG'] = 'satrec2_experiment'
|
||||
|
||||
# phase lists:
|
||||
pars['PH1'] = [ 0] # saturation pulses
|
||||
pars['PH3'] = [ 0, 180, 0, 180, 90, 270, 90, 270] # 1st 90-degree pulse
|
||||
pars['PH4'] = [90, 90, 270, 270, 0, 0, 180, 180] # 2nd 90-degree pulse
|
||||
pars['PH2'] = [ 0, 180, 0, 180, 90, 270, 90, 270] # receiver
|
||||
|
||||
# read in variables:
|
||||
P90 = pars['P90']
|
||||
SF = pars['SF']
|
||||
O1 = pars['O1']
|
||||
NECH = pars['NECH']
|
||||
D1 = pars['D1']
|
||||
D2 = pars['D2']
|
||||
D3 = pars['D3']
|
||||
D4 = pars['D4']
|
||||
TAU = pars['TAU']
|
||||
PH1 = pars['PH1'][run%len(pars['PH1'])]
|
||||
PH3 = pars['PH3'][run%len(pars['PH3'])]
|
||||
PH4 = pars['PH4'][run%len(pars['PH4'])]
|
||||
PH2 = pars['PH2'][run%len(pars['PH2'])]
|
||||
PHA = pars['PHA']
|
||||
|
||||
# set variable delay list for saturation pulses:
|
||||
vdlist = log_range(D2, D1, NECH-1)
|
||||
|
||||
# set sampling parameters:
|
||||
SI = pars['SI']
|
||||
SW = pars['SW']
|
||||
while SW <= 10e6 and SI < 256*1024:
|
||||
SI *= 2
|
||||
SW *= 2
|
||||
|
||||
# the pulse sequence:
|
||||
|
||||
# saturation:
|
||||
e.set_frequency(SF+O1, phase=PH1) # set frequency and phase for saturation pulses
|
||||
e.ttl_pulse(TXEnableDelay, value=TXEnableValue) # enable RF amplifier
|
||||
e.ttl_pulse(P90, value=TXEnableValue|TXPulseValue) # apply 90-degree pulse
|
||||
for delay in vdlist[::-1]:
|
||||
e.wait(delay-P90-TXEnableDelay) # wait for next saturation pulse
|
||||
e.ttl_pulse(TXEnableDelay, value=TXEnableValue) # enable RF amplifier
|
||||
e.ttl_pulse(P90, value=TXEnableValue|TXPulseValue) # apply 90-degree pulse
|
||||
|
||||
# recovery:
|
||||
e.wait(TAU) # wait for tau
|
||||
e.set_phase(PH3) # set phase for next pulse
|
||||
|
||||
# echo detection:
|
||||
e.ttl_pulse(TXEnableDelay, value=TXEnableValue) # enable RF amplifier
|
||||
e.ttl_pulse(P90, value=TXEnableValue|TXPulseValue) # apply 90-degree pulse
|
||||
e.wait(D3-P90/2-TXEnableDelay) # echo delay
|
||||
e.set_phase(PH4) # set phase for next pulse
|
||||
e.ttl_pulse(TXEnableDelay, value=TXEnableValue) # enable RF amplifier
|
||||
e.ttl_pulse(P90, value=TXEnableValue|TXPulseValue) # apply 90-degree pulse
|
||||
e.set_phase(PHA) # set phase for receiver
|
||||
e.wait(D3-P90/2+D4) # echo delay
|
||||
e.record(SI, SW, sensitivity=ADCSensitivity) # acquisition
|
||||
|
||||
# write experiment attributes:
|
||||
for key in pars.keys():
|
||||
e.set_description(key, pars[key]) # pulse sequence parameters
|
||||
e.set_description('run', run) # current scan
|
||||
e.set_description('rec_phase', -PH2) # current receiver phase
|
||||
|
||||
return e
|
@ -0,0 +1,208 @@
|
||||
# -*- coding: iso-8859-1 -*-
|
||||
|
||||
from numpy import *
|
||||
from scipy.signal import *
|
||||
from scipy.optimize import *
|
||||
from os import path, rename
|
||||
|
||||
def result():
|
||||
|
||||
measurement = MeasurementResult('Magnetization')
|
||||
|
||||
measurement_range = [0.0, 10e-6]
|
||||
measurement_ranging = False
|
||||
|
||||
suffix = '' # output file name's suffix and...
|
||||
counter = 1 # counter for arrayed experiments
|
||||
var_key = '' # variable parameter name
|
||||
|
||||
# loop over the incoming results:
|
||||
for timesignal in results:
|
||||
if not isinstance(timesignal,ADC_Result):
|
||||
continue
|
||||
|
||||
# read experiment parameters:
|
||||
pars = timesignal.get_description_dictionary()
|
||||
|
||||
# ---------------- digital filter ------------------
|
||||
|
||||
# get actual sampling rate of timesignal:
|
||||
sampling_rate = timesignal.get_sampling_rate()
|
||||
|
||||
# get user-defined spectrum width:
|
||||
spec_width = pars['SW']
|
||||
|
||||
# specify cutoff frequency, in relative units:
|
||||
cutoff = spec_width / sampling_rate
|
||||
|
||||
if cutoff < 1: # no filter applied otherwise
|
||||
|
||||
# number of filter's coefficients:
|
||||
numtaps = 29
|
||||
|
||||
# use firwin to create a lowpass FIR filter:
|
||||
fir_coeff = firwin(numtaps, cutoff)
|
||||
|
||||
# downsize x according to user-defined spectral window:
|
||||
skip = int(sampling_rate / spec_width)
|
||||
timesignal.x = timesignal.x[::skip]
|
||||
|
||||
for i in range(2):
|
||||
# apply the filter to ith channel:
|
||||
timesignal.y[i] = lfilter(fir_coeff, 1.0, timesignal.y[i])
|
||||
|
||||
# zeroize first N-1 "corrupted" samples:
|
||||
timesignal.y[i][:numtaps-1] = 0.0
|
||||
|
||||
# circular left shift of y:
|
||||
timesignal.y[i] = roll(timesignal.y[i], -(numtaps-1))
|
||||
|
||||
# downsize y to user-defined number of samples (SI):
|
||||
timesignal.y[i] = timesignal.y[i][::skip]
|
||||
|
||||
# update the sampling_rate attribute of the signal's:
|
||||
timesignal.set_sampling_rate(spec_width)
|
||||
|
||||
# ----------------------------------------------------
|
||||
|
||||
# rotate timesignal according to current receiver's phase:
|
||||
timesignal.phase(pars['rec_phase'])
|
||||
|
||||
# provide timesignal to the display tab:
|
||||
data['Current Scan'] = timesignal
|
||||
|
||||
# accumulate...
|
||||
if not locals().get('accu'):
|
||||
accu = Accumulation()
|
||||
|
||||
# skip dummy scans, if any:
|
||||
if pars['run'] < 0: continue
|
||||
|
||||
# add up:
|
||||
accu += timesignal
|
||||
|
||||
# provide accumulation to the display tab:
|
||||
data['Accumulation'] = accu
|
||||
|
||||
# check how many scans are done:
|
||||
if accu.n == pars['NS']: # accumulation is complete
|
||||
|
||||
# make a copy:
|
||||
fid = accu + 0
|
||||
|
||||
# compute the initial phase of FID:
|
||||
phi0 = arctan2(fid.y[1][0], fid.y[0][0]) * 180 / pi
|
||||
if not locals().get('ref'): ref = phi0
|
||||
print 'phi0 = ', phi0
|
||||
|
||||
# rotate FID to maximize Re (optional):
|
||||
#fid.phase(-phi0)
|
||||
|
||||
# do FFT:
|
||||
fid.exp_window(line_broadening=10)
|
||||
spectrum = fid.fft(samples=2*pars['SI'])
|
||||
|
||||
# try zero-order phase correction:
|
||||
spectrum.phase(-phi0)
|
||||
|
||||
# provide spectrum to the display tab:
|
||||
data['Spectrum'] = spectrum
|
||||
|
||||
# check whether it is an arrayed experiment:
|
||||
var_key = pars.get('VAR_PAR')
|
||||
if var_key:
|
||||
# get variable parameter's value:
|
||||
var_value = pars.get(var_key)
|
||||
|
||||
# provide signal recorded with this var_value to the display tab:
|
||||
data['Accumulation'+"/"+var_key+"=%e"%(var_value)] = accu
|
||||
|
||||
# measure signal intensity vs. var_value:
|
||||
if measurement_ranging == True:
|
||||
[start, stop] = accu.get_sampling_rate() * array(measurement_range)
|
||||
measurement[var_value] = sum(accu.y[0][int(start):int(stop)])
|
||||
|
||||
else:
|
||||
measurement[var_value] = sum(accu.y[0][0:31])
|
||||
|
||||
# provide measurement to the display tab:
|
||||
data[measurement.get_title()] = measurement
|
||||
|
||||
# update the file name suffix:
|
||||
suffix = '_' + str(counter)
|
||||
counter += 1
|
||||
|
||||
# save accu if required:
|
||||
outfile = pars.get('OUTFILE')
|
||||
if outfile:
|
||||
datadir = pars.get('DATADIR')
|
||||
|
||||
# write raw data in Simpson format:
|
||||
filename = datadir+outfile+suffix+'.dat'
|
||||
if path.exists(filename):
|
||||
rename(filename, datadir+'~'+outfile+suffix+'.dat')
|
||||
accu.write_to_simpson(filename)
|
||||
|
||||
# write raw data in Tecmag format:
|
||||
# filename = datadir+outfile+'.tnt'
|
||||
# accu.write_to_tecmag(filename, nrecords=20)
|
||||
|
||||
# write parameters in a text file:
|
||||
filename = datadir+outfile+suffix+'.par'
|
||||
if path.exists(filename):
|
||||
rename(filename, datadir+'~'+outfile+suffix+'.par')
|
||||
|
||||
fileobject = open(filename, 'w')
|
||||
for key in sorted(pars.iterkeys()):
|
||||
if key=='run': continue
|
||||
if key=='rec_phase': continue
|
||||
fileobject.write('%s%s%s'%(key,'=', pars[key]))
|
||||
fileobject.write('\n')
|
||||
fileobject.close()
|
||||
|
||||
# reset accumulation:
|
||||
del accu
|
||||
|
||||
if var_key == 'TAU':
|
||||
# mono-exponential recovery fit:
|
||||
xdata = measurement.get_xdata()
|
||||
ydata = measurement.get_ydata()
|
||||
[amplitude, rate, offset] = fitfunc(xdata, ydata)
|
||||
print '%s%02g' % ('Amplitude = ', amplitude)
|
||||
print '%s%02g' % ('T1 [s] = ', 1./rate)
|
||||
|
||||
# update display for the fit:
|
||||
measurement.y = func([amplitude, rate, offset], xdata)
|
||||
data[measurement.get_title()] = measurement
|
||||
|
||||
# the fitting procedure:
|
||||
def fitfunc(xdata, ydata):
|
||||
|
||||
# initialize variable parameters:
|
||||
try:
|
||||
# solve Az = b:
|
||||
A = array((ones(xdata.size/2), xdata[0:xdata.size/2]))
|
||||
b = log(abs(mean(ydata[-2:]) - ydata[0:xdata.size/2]))
|
||||
z = linalg.lstsq(transpose(A), b)
|
||||
amplitude = exp(z[0][0])
|
||||
rate = -z[0][1]
|
||||
except:
|
||||
amplitude = abs(ydata[-1] - ydata[0])
|
||||
rate = 1./(xdata[-1] - xdata[0])
|
||||
offset = min(ydata)
|
||||
p0 = [amplitude, rate, offset]
|
||||
|
||||
# run least-squares optimization:
|
||||
plsq = leastsq(residuals, p0, args=(xdata, ydata))
|
||||
|
||||
return plsq[0] # best-fit parameters
|
||||
|
||||
def residuals(p, xdata, ydata):
|
||||
return ydata - func(p, xdata)
|
||||
|
||||
# here is the function to fit:
|
||||
def func(p, xdata):
|
||||
return p[0]*(1-exp(-p[1]*xdata)) + p[2]
|
||||
|
||||
|
||||
pass
|
Reference in New Issue
Block a user