我有两个目的相同的文件,一个在Python中,一个在MATLAB中。读入数据文件后,我希望它们计算和错误,使用detrend
删除常量偏移,然后使用griddata
插入曲面以适应错误。
但是,detrend
和scipy.signal.detrend
的MATLAB版本似乎存在差异,以及MATLAB的griddata
与scipy.interpolate.griddata
之间的差异,我不太了解。
我使用的MATLAB代码如下:
err = posEncUm - posLasUm;
err(:,1) = detrend(err(:,1),'constant');
err(:,2) = detrend(err(:,2),'constant');
[X,Y] = meshgrid(posEncUm(:,1),posEncUm(:,2));
errX = griddata(posEncUm(:,1),posEncUm(:,2),err(:,1),X,Y);
errY = griddata(posEncUm(:,1),posEncUm(:,2),err(:,2),X,Y);
同时,我使用的Python代码是:
err = posEncUm - posLasUm;
err[:,0] = scipy.signal.detrend(err[:,0])
err[:,1] = scipy.signal.detrend(err[:,1])
mn = np.min(posEncUm, axis = 0)
mx = np.max(posEncUm, axis = 0)
X,Y = np.meshgrid(posEncUm[:,0],posEncUm[:,1]) # makes uniform grid
errX = griddata(posEncUm[:,0],posEncUm[:,1],err[:,0],X,Y,interp='linear')
errY = griddata(posEncUm[:,0],posEncUm[:,1],err[:,1],X,Y,interp='linear')
在绘制数据时,我得到了两个非常不同的东西,我不确定这些是哪个因素造成的。
这是MATLAB code生成的内容,这就是Python code 生成的内容。
我将在下面发布我的完整代码,用于MATLAB和Python。 有人能帮我弄清楚出了什么问题吗?
完整的MATLAB代码:
function ES15302_squareness(myDir)
close all;
cd(myDir);
s = load('outdata.dat');
posEncUm = [s(:,1) s(:,3)]/25000;
posLasUm = [-s(:,2) s(:,4)]/25000;
err = posEncUm - posLasUm;
err(:,1) = detrend(err(:,1),'constant');
err(:,2) = detrend(err(:,2),'constant');
[X,Y] = meshgrid(posEncUm(:,1),posEncUm(:,2));
errX = griddata(posEncUm(:,1),posEncUm(:,2),err(:,1),X,Y);
errY = griddata(posEncUm(:,1),posEncUm(:,2),err(:,2),X,Y);
figure();
subplot(4,2,1);
plot(0.001*posEncUm(:,1),err(:,1),'.');
p = polyfit(0.001*posEncUm(:,1),err(:,1),1);
title(sprintf('Slope = %4.1f um / 100 mm',p(1)*100));
xlabel('Encoder Position (X), mm');
ylabel('Laser Error (X), um');
grid on;
subplot(4,2,2);
plot(0.001*posEncUm(:,2),err(:,1),'.');
p = polyfit(0.001*posEncUm(:,2),err(:,1),1);
title(sprintf('Slope = %4.1f um / 100 mm',p(1)*100));
xlabel('Encoder Position (Y), mm');
ylabel('Laser Error (X), um');
grid on;
subplot(4,2,3);
plot(0.001*posEncUm(:,1),err(:,2),'.');
p = polyfit(0.001*posEncUm(:,1),err(:,2),1);
title(sprintf('Slope = %4.1f um / 100 mm',p(1)*100));
xlabel('Encoder Position (X), mm');
ylabel('Laser Error (Y), um');
grid on;
subplot(4,2,4);
plot(0.001*posEncUm(:,2),err(:,2),'.');
p = polyfit(0.001*posEncUm(:,2),err(:,2),1);
title(sprintf('Slope = %4.1f um / 100 mm',p(1)*100));
xlabel('Encoder Position (Y), mm');
ylabel('Laser Error (Y), um');
grid on;
subplot(2,1,2);
quiver(0.001*X,0.001*Y,errX,errY,5);
axis square
grid on;
xlabel('Encoder Pos (X), mm');
ylabel('Encoder Pos (Y), mm');
% plot the figures
if isempty(strfind(myDir,'\')),
plotID = myDir;
else
index = max(strfind(myDir,'\'))+1;
plotID = myDir(index:end);
end
set(gcf,'PaperPosition',[0.5 0.5 7.5 10]);
stampitproprietary(plotID);
eval(sprintf('print -dpdf squareness_%s.pdf',plotID));
cd ..
return;
% -------------------------------------------------------------------------
h = findobj('Type','figure');
cur_fig = get(0,'CurrentFigure');
warning off
for i = 1:length(h)
set(0,'CurrentFigure',h(i));
cur_ax = get(h(i),'CurrentAxes'); % gets current axis handle for given figure
hidn_axis=axes('Position',[0 0 1 1], 'Visible','off','Units','normalized','Tag','Stampit');
dh(i,1) = text(.905,0.01,plotID);
set(dh(i,1),'FontSize',[12],'HorizontalAlignment','Right');
set(h(i),'CurrentAxes',cur_ax); % Sets current axis back to its previous state
chld = get(h(i),'Children'); % These lines place the hidden "Stampit" axis
if length(chld)>1 % to the bottom of the figure's Children handle list
set(h(i),'Children',[chld(2:length(chld)); chld(1)]) % This alieveates problems with zoom and other functions
end
end
set(0,'CurrentFigure',cur_fig);
pause(.001)
warning on
return;
完整的Python代码:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import scipy
import scipy.linalg
from scipy import signal
from scipy.interpolate import griddata
from mpl_toolkits.mplot3d import Axes3D
import plotly
import plotly.graph_objs as go
import plotly.plotly as py
import os
from pylab import *
import math as m
#from matplotlib.backends.backend_pdf import PdfPages
#import time
#import calendar
import pylab as pl
matplotlib.rcParams['figure.figsize'] = (8.0, 10.5)
#timestamp = datetime.datetime.today().strftime('%Y%m%d%H%M')
plt.rc('xtick', labelsize=5)
plt.rc('ytick', labelsize=5)
plt.rc('grid', ls='dotted')
plt.rcParams['lines.dotted_pattern'] = [0.1,0.5]
def main(argv):
testdir = argv[0]
fname = os.path.join(testdir,'OUTDATA.DAT')
s = np.loadtxt(fname) #If in current directory
s2 = np.transpose([s[:,0],s[:,2]]) # these are
s3 = np.transpose([-s[:,1],s[:,3]]) # all going
posEncUm = np.divide(s2,25000) # to be
posLasUm = np.divide(s3,25000) # 169x2
err = posEncUm - posLasUm;
err[:,0] = scipy.signal.detrend(err[:,0])
err[:,1] = scipy.signal.detrend(err[:,1])
mn = np.min(posEncUm, axis = 0)
mx = np.max(posEncUm, axis = 0)
X,Y = np.meshgrid(posEncUm[:,0], posEncUm[:,1]) # makes uniform grid
errX = griddata(posEncUm[:,0],posEncUm[:,1],err[:,0],X,Y,interp='linear')
errY = griddata(posEncUm[:,0],posEncUm[:,1],err[:,1],X,Y,interp='linear')
#----------------------------------------------------------------------------------------------------------------------------------
#plot3 = pl.figure(figsize=(3,4))
fig = pl.figure(figsize=(3,4))
gs = pl.GridSpec(3, 2)
gs.update(left=0.08, right=0.925,
top=0.95, bottom=0.05,
hspace=0.6, wspace=0.8)
plt.subplot(321)
ax0 = pl.subplot(gs[0, 0])
plt.scatter(0.001*posEncUm[:,0],err[:,0], s=0.4, linewidths=0.7)
p = np.polyfit(0.001*posEncUm[:,0],err[:,0],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=5)
plt.xlabel('Encoder Position (X), mm', fontsize=5)
plt.ylabel('Laser Error (X), um', fontsize=5)
#plt.xlim([-200,200])
plt.ylim([-30,10])
plt.xticks(np.arange(-200,201,100))
plt.yticks(np.arange(-30,11, 10))
plt.grid()
plt.subplot(322)
ax1 = pl.subplot(gs[0, 1])
plt.scatter(0.001*posEncUm[:,1],err[:,0], s=0.4, linewidths=0.7)
p = np.polyfit(0.001*posEncUm[:,1],err[:,0],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=5)
plt.xlabel('Encoder Position (Y), mm', fontsize=5)
plt.ylabel('Laser Error (X), um', fontsize=5)
#plt.xlim([-200,200])
plt.ylim([-30,10])
plt.xticks(np.arange(-200,201,100))
plt.yticks(np.arange(-30,11, 10))
plt.grid()
plt.subplot(323)
ax2 = pl.subplot(gs[1, 0])
plt.scatter(0.001*posEncUm[:,0],err[:,1], s=0.4, linewidths=0.7)
p = np.polyfit(0.001*posEncUm[:,0],err[:,1],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=5)
plt.xlabel('Encoder Position (X), mm', fontsize=5)
plt.ylabel('Laser Error (Y), um', fontsize=5)
#plt.xlim([-200,200])
plt.ylim([-30,10])
plt.xticks(np.arange(-200,201,100))
plt.yticks(np.arange(-30,11, 10))
plt.grid()
plt.subplot(324)
ax3 = pl.subplot(gs[1, 1])
plt.scatter(0.001*posEncUm[:,1],err[:,1], s=0.4, linewidths=0.7)
p = np.polyfit(0.001*posEncUm[:,1],err[:,1],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=5)
plt.xlabel('Encoder Position (Y), mm', fontsize=5)
plt.ylabel('Laser Error (Y), um', fontsize=5)
#plt.xlim([-200,200])
plt.ylim([-30,10])
plt.xticks(np.arange(-200,201,100))
plt.yticks(np.arange(-30,11, 10))
plt.grid()
plt.subplot(325)
ax4 = pl.subplot(gs[2, :])
plt.quiver(0.001*X,0.001*Y,errX,errY)
plt.grid()
plt.xlabel('Encoder Pos (X), mm', fontsize=5)
plt.ylabel('Encoder Pos (Y), mm', fontsize=5)
plt.xticks(np.arange(-200,201,100))
plt.yticks(np.arange(-200,201,50))
plt.gca().set_aspect('equal', adjustable = 'box')
timestamp = os.path.basename(testdir)
fig.text(0.96,0.01, timestamp, horizontalalignment = 'right',size='small')
fig.savefig(os.path.join(testdir,"squareness_{0}.png".format(timestamp)))
fig.savefig(os.path.join(testdir,"squareness_{0}.pdf".format(timestamp)))
if __name__ == "__main__":
main(sys.argv[1:])