简体   繁体   中英

Scipy detrend not equivalent to MATLAB

I have two files with the same purpose, one in Python and one in MATLAB. After reading in a data file, I want them to calculate and error, use detrend to remove a constant offset, then interpolate a surface to fit the errors using griddata.

In detrending the data in my Python file, I attempted to use scipy.signal.detrend with both linear and constant argument types since constant initially didn't work. ( See for documentation of scipy.signal.detrend )

However, neither of these methods get the same array err as the MATLAB file, and I ensured that everything else up to that point had matched. Can you tell me a different way to detrend as MATLAB does?

Python code (minus the header/imports):

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 code:

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

(I don't have any errors, it's just that err in MATLAB doesn't match err in Python after the detrends)

I am not sure if there is a different scipy / matplotlib function to fix this issue, but in the meantime, by calculating the average value of each column from the MATLAB file, the mean was close enough to 0 (within 0.0001) that I think I will simply take the average value of the columns in my Python file, then subtract that mean value from every index in the column.

In the future, though, I would still love to know a method that does not rely on this...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM