How can I convert spline in Matlab to python? - python

I used interp1 in matlab with the method 'spline', is there a function in any python packages can stead of it?
I have tried interp1d in scipy with the kind='cubic', but it has different results with interp1 in matlab with the method 'spline', although not very big.
And I have found people saying that InterplotationUnivariateSpline with k=3 is same as interp1d with kind='cubic', is it true? And what about CubicSpline?In docs they say that the resulting spline will be the same as returned by splrep (with s=0) and InterpolatedUnivariateSpline, but they also say that it doesn't use B-spline basis like splrep (with s=0) and InterpolatedUnivariateSpline.
Matlab code:
ppx = interp1(samplez,samplex,'spline','pp');
interpx = ppval(ppx,interpz);
python code:
from scipy import interpolate
interplinex = interpolate.interp1d(samplez, samplex, kind='cubic')
interpx = interplinex(interpz)
The result named interpx in Matlab is different with the result named interpx in python

Related

Matlab Dirichlet kernel equivalent in Python?

Is there an equivalent in Python for Matlab diric? The Dirichlet kernel DL(ω) is: DL(ω) = sin(ωL/2) / sin (ω/2)
I found scipy.stats.Dirichlet, but that's not related. That's no big deal to write the expanded form, but there is a limit to manage: DL(0) = L.
Try the SciPy "special" Dirichlet function

Sympy plot _backend.ax appears as a list object, won't let me set xlabel

I'm trying to use sympy to plot an implicitly defined function in python, and have found the built-in plotting functionality to be sorely lacking. The source code recommends using _backend to directly modify the matplotlib axis and figure objects. Here's an abbreviated version of my code:
import matplotlib
from sympy import symbols, exp, plot_implicit, Eq
V,I = symbols('V I')
#define implicit equation to plot
eq1 = Eq(exp(I+V)-I,0)
#plot equation
p1 = plot_implicit(eq1, V,I)
#use _backend.ax to set xlabel and title of plot
axis = p1._backend.ax
axis.set_xlabel('Voltage (V)')
axis.set_title('ooga')
p1._backend.fig.savefig('test.png')
p1.show()
But when I run it, I get the following output:
plot generated without specified axis label and title
AttributeError: 'list' object has no attribute 'set_xlabel'
Why isn't this working? I don't understand why my axis object is being saved as a list. (Also, I find it rather odd that I get any plot output at all, as this would seem to imply the plot is being generated before I call p1.show())
Edit:
I've resolved the issue by following JohanC's recommendation to use move_sympyplot_to_axes from this post. Unfortunately I was unable to get ._backend working as intended, but this solution works well enough for my purposes.

How do I use the detrending function for matplotlib.pyplot.acorr?

I am a Python beginner. I am trying to detrend a time-series before running an autocorrelation analysis by using acorr in matplotlib. But there is something about the syntax that I fail understand.
Matplotlib's website (https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.acorr.html) describes how to use detrending with the acorr function: "x is detrended by the detrend callable. This must be a function x = detrend(x) accepting and returning an numpy.array." I must be reading this wrong, because the code I use does not work.
Failed attempts:
plt.acorr(values, detrend=True)
plt.acorr(values, detrend="linear")
plt.acorr(values=detrend(values))
As you can see, some rudimentary fact about syntax or matplotlib escapes me. Please help.
In matplotlib.mlab you find functions which you can use for detrending. An example:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mlab
wn = np.random.normal(size=10**3)
plt.figure()
plt.acorr(np.abs(wn), maxlags=200, detrend=mlab.detrend_none) #default detrend
plt.figure()
plt.acorr(np.abs(wn), maxlags=200, detrend=mlab.detrend) #subtract sample mean

wavedec does not returning any coefficients in python using pywt library

i used wavelet decomposition command in python using pywt library but it does not return any coefficients. my code is given below .
import numpy as np
import pywt as pywt
(e,f)=pywt.wavedec(y,'db12' ,level=2)
print("e:"+str(e))
print("f:"+str(f))
I also tried with pywt.dwt(y,' db12', level=2) it is also not returning any coefficients
it returns a null output, where y is a matrix contains my input
I tried reproducing your results with a random (discrete) signal like so:
import numpy as np
import pyw
x = np.random.randint(0,100,500)
y = pywt.wavedec(x, 'db12', level=2)
(e,f) = pywt.dwt(x, 'db12')
I noticed two things: For a 1D signal, wavedec returns more than two coefficient arrays, as also mentioned in the docs. Similarly, the dwt function does not know the keyword level=, but works just fine with the command specified above.
Hope that helps

fitdistr in rpy2

I've a 1D list of data, that I want to fit into a distribution using either least squares or maximum likelihood, as presented here, but I want to do it from python instead of the R interactive shell.
I got rpy2 installed, and would like to use the fitdistr function from within the interactive ipython shell, as I have imported the data in a list.
Where is this function, and how do I use it?
The function is in the R package MASS
from rpy2.robjects.packages import importr
MASS = importr('MASS')
# the function is now at MASS.fitdistr

Categories