【问题标题】:Scipy detrend not equivalent to MATLABScipy detrend 不等同于 MATLAB
【发布时间】:2018-06-13 22:40:30
【问题描述】:

我有两个具有相同目的的文件,一个在 Python 中,一个在 MATLAB 中。读入数据文件后,我希望他们计算并出错,使用 detrend 删除恒定偏移量,然后使用 griddata 插入曲面以适应错误。

在消除 Python 文件中的数据趋势时,我尝试将 scipy.signal.detrendlinearconstant 参数类型一起使用,因为 constant 最初不起作用。 (See 用于 scipy.signal.detrend 的文档)

但是,这些方法都没有获得与 MATLAB 文件相同的数组 err,并且我确保在此之前的所有其他内容都匹配。你能告诉我另一种像 MATLAB 那样去趋势的方法吗?

Python 代码(减去标头/导入):

timestamp = datetime.datetime.today().strftime('%Y%m%d%H%M')
print timestamp
plt.rc('xtick', labelsize=5)
plt.rc('ytick', labelsize=5)
plt.rc('grid', ls='dotted')
plt.rcParams['lines.dotted_pattern'] = [0.1,0.5]
np.set_printoptions(suppress=True)

def main(argv):
    testdir = argv[0] # if list indexing error --> you must input a file name after <python es15302_squareness.py> in the command line
    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;
 # -------------------------Everything good up to here----------------------    
    err[:,0] = scipy.signal.detrend(err[:,0], type=='constant')
    err[:,1] = scipy.signal.detrend(err[:,1], type=='constant')
    print err

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');

(我没有任何错误,只是 MATLAB 中的 err 在 detrends 后与 Python 中的 err 不匹配)

【问题讨论】:

  • 请不要在代码中包含行号(无论如何你是怎么得到它们的);这使他们无法为我们复制粘贴。

标签: python matlab scipy


【解决方案1】:

我不确定是否有不同的scipy/matplotlib 函数来解决此问题,但与此同时,通过计算 MATLAB 文件中每一列的平均值,平均值足够接近 0 (在 0.0001 以内)我想我会简单地取 Python 文件中列的平均值,然后从列中的每个索引中减去该平均值。

不过,在未来,我仍然很想知道一种不依赖于此的方法...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-13
    • 2013-09-10
    • 2022-06-13
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多