More work on the xcorr (cross-correlation) function, but still needs more testing.

这个提交包含在:
Craig Warren
2016-07-14 09:43:14 +01:00
父节点 2b220df19f
当前提交 423f095633

查看文件

@@ -64,6 +64,7 @@ def maxvalue(filename, args):
return maxvalue return maxvalue
def maxabsvalue(filename, args): def maxabsvalue(filename, args):
"""Maximum absolute value from a response. """Maximum absolute value from a response.
@@ -98,12 +99,11 @@ def xcorr(filename, args):
xcorrmax (float): Maximum value from specific outputs xcorrmax (float): Maximum value from specific outputs
""" """
# Load (from text file) and normalise the reference response # Load (from text file) the reference response
with open(args['refresp'], 'r') as f: with open(args['refresp'], 'r') as f:
refdata = np.loadtxt(f) refdata = np.loadtxt(f)
reftime = refdata[:,0] * 1e-9 reftime = refdata[:,0] * 1e-9
refresp = refdata[:,1] refresp = refdata[:,1]
refresp /= np.amax(np.abs(refresp))
# Load response from output file # Load response from output file
f = h5py.File(filename, 'r') f = h5py.File(filename, 'r')
@@ -117,44 +117,54 @@ def xcorr(filename, args):
modelresp = output[outputname] modelresp = output[outputname]
# Convert field value (V/m) to voltage # Convert field value (V/m) to voltage
if outputname == 'Ex': if outputname == 'Ex':
modelresp *= -1 * f.attrs['dx, dy, dz'][0] modelresp *= -f.attrs['dx, dy, dz'][0]
elif outputname == 'Ey': elif outputname == 'Ey':
modelresp *= -1 * f.attrs['dx, dy, dz'][1] modelresp *= -f.attrs['dx, dy, dz'][1]
if outputname == 'Ez': elif outputname == 'Ez':
modelresp *= -1 * f.attrs['dx, dy, dz'][2] modelresp *= -f.attrs['dx, dy, dz'][2]
# Normalise respose from output file # Normalise reference respose and response from output file
modelresp /= np.amax(np.abs(modelresp)) # refresp /= np.amax(np.abs(refresp))
# modelresp /= np.amax(np.abs(modelresp))
# Make both responses the same length in time # Make both responses the same length in time
if reftime[-1] > modeltime[-1]: # if reftime[-1] > modeltime[-1]:
reftime = np.arange(0, f.attrs['dt'] * f.attrs['Iterations'], reftime[-1] / len(reftime)) # reftime = np.arange(0, f.attrs['dt'] * f.attrs['Iterations'], reftime[-1] / len(reftime))
refresp = refresp[0:len(reftime)] # refresp = refresp[0:len(reftime)]
elif modeltime[-1] > reftime[-1]: # elif modeltime[-1] > reftime[-1]:
modeltime = np.arange(0, reftime[-1], f.attrs['dt']) # modeltime = np.arange(0, reftime[-1], f.attrs['dt'])
modelresp = modelresp[0:len(modeltime)] # modelresp = modelresp[0:len(modeltime)]
#
# # Downsample the response with the higher sampling rate
# if len(modeltime) < len(reftime):
# refresp = signal.resample(refresp, len(modelresp))
# elif len(reftime) < len(modeltime):
# modelresp = signal.resample(modelresp, len(refresp))
# Downsample the response with the higher sampling rate # Prepare data for normalized cross-correlation
if len(modeltime) < len(reftime): refresp = (refresp - np.mean(refresp)) / (np.std(refresp) * len(refresp))
refresp = signal.resample(refresp, len(modelresp)) modelresp = (modelresp - np.mean(modelresp)) / np.std(modelresp)
elif len(reftime) < len(modeltime):
modelresp = signal.resample(modelresp, len(refresp))
# Plots responses for checking # Plots responses for checking
# fig, ax = plt.subplots(subplot_kw=dict(xlabel='Iterations', ylabel='Voltage [V]'), figsize=(20, 10), facecolor='w', edgecolor='w') #fig, ax = plt.subplots(subplot_kw=dict(xlabel='Iterations', ylabel='Voltage [V]'), figsize=(20, 10), facecolor='w', edgecolor='w')
# ax.plot(refresp,'r', lw=2, label='refresp') #ax.plot(refresp,'r', lw=2, label='refresp')
# ax.plot(modelresp,'b', lw=2, label='modelresp') #ax.plot(modelresp,'b', lw=2, label='modelresp')
# ax.grid() #ax.grid()
# plt.show() #plt.show()
# Calculate cross-correlation # Calculate cross-correlation
xcorr = signal.correlate(refresp, modelresp) xcorr = signal.correlate(refresp, modelresp)
# Set any NaNs to zero
xcorr = np.nan_to_num(xcorr)
# Plot cross-correlation for checking # Plot cross-correlation for checking
# fig, ax = plt.subplots(subplot_kw=dict(xlabel='Iterations', ylabel='Voltage [V]'), figsize=(20, 10), facecolor='w', edgecolor='w') # fig, ax = plt.subplots(subplot_kw=dict(xlabel='Iterations', ylabel='Voltage [V]'), figsize=(20, 10), facecolor='w', edgecolor='w')
# ax.plot(xcorr,'r', lw=2, label='xcorr') # ax.plot(xcorr,'r', lw=2, label='xcorr')
# ax.grid() # ax.grid()
# plt.show() # plt.show()
xcorrmax = np.amax(xcorr) / 100
xcorrmax = np.amax(xcorr)
return xcorrmax return xcorrmax