By elarr

2018-06-13 14:17:56 8 Comments

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 ='%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]

def main(argv):
    testdir = argv[0] # if list indexing error --> you must input a file name after <python> 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;

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)


@elarr 2018-06-13 14:40:30

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...

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] What is the Python equivalent of static variables inside a function?

  • 2008-11-10 23:33:36
  • andrewdotnich
  • 246550 View
  • 463 Score
  • 23 Answer
  • Tags:   python

9 Answered Questions

15 Answered Questions

[SOLVED] Installing SciPy with pip

3 Answered Questions

[SOLVED] scipy equivalent for MATLAB spy

8 Answered Questions

[SOLVED] What is the Python 3 equivalent of "python -m SimpleHTTPServer"

  • 2011-10-30 07:22:49
  • ryanbraganza
  • 243587 View
  • 739 Score
  • 8 Answer
  • Tags:   python python-3.x

7 Answered Questions

[SOLVED] Does Python SciPy need BLAS?

  • 2011-09-21 08:11:54
  • jdborg
  • 115954 View
  • 170 Score
  • 7 Answer
  • Tags:   python scipy

8 Answered Questions

[SOLVED] Relationship between scipy and numpy

  • 2011-06-01 12:11:19
  • NPE
  • 36067 View
  • 210 Score
  • 8 Answer
  • Tags:   python numpy scipy

1 Answered Questions

Scipy detrend puzzle

  • 2015-01-19 04:21:17
  • Arbitel
  • 121 View
  • 1 Score
  • 1 Answer
  • Tags:   python numpy scipy

2 Answered Questions

[SOLVED] Differences between Matlab and Numpy and Python's `round` function

2 Answered Questions

Sponsored Content