How to apply sgolay function(of matlab) in python? - python

I have a 1-D numpy array of positions(basically an eye-gaze data).
I want to apply sgolay function of matlab in python.
Though i have already seen scipy's savgol_filter and savgol_coeffs,i am not able to understand how to implement it as i am not well versed in it's mathematics.
I have also seen that there is a confusion about whether savgol_filter or savgol_coeffs of python is equivalent to matlab's sgolay function.
I want to differentiate the numpy array once to get velocity and then from velocity array differentiate it again to get acceleration.
The whole procedure is attached in this image

Related

Monotonic Interpolation in 2D

I would like to use scipy.interpolate.interp2d or scipy.interpolate.RectBivariateSpline to interpolate a 2D image, but would like for the interpolation to be monotonic.
Is there an equivalent function that assures monotonicity or a way to force interp2d or RectBivariateSpline to return monotonic interpolations?
I believe I am looking for something similar to PchipInterpolator, but for 2D (or n-dimensional).
The first question is: How do you define monotonicity in 2D (and in higher dimensions)? It seems to be not unique. I searched and found this paper: Piecewise polynomial monotonic interpolation of 2D gridded data (https://hal.inria.fr/hal-01059532/document). Maybe this helps.
The function RectBivariateSpline in scipy is wrapped around fitpack (as I remember, but maybe not). If that is the case, then it wouldn't be monotonic, it is b-spline-based.

Peak Detection of an FFT signal in jupyter notebook

I'm trying to find the peaks of and EMG signal which i converted into the frequency domain with the use of FFT. My problem is that when I try to use the command Find_peaks from the scipy.signal libraries it gives me a warning of ValueError: x must be a 1D array. could someone please point me in the right path because I am having a hard time of understanding how I could find the peaks of my signal in the frequency domain, should I convert it back to the time domain or should I find a way to compress the array. (P.S. I am still a newbie, all the help is much appreciated).This is what I was able to code
This is the error
The results of scipy's FFT is a multi-dimensional array that includes complex-number results of the transform, represented as a 2-element vector. The find_peaks call can only accept a 1D array. You may need to either convert these to a scalar (perhaps take the absolute value of the tuples), or you can use a peak detector that operates in more domains. Check out this related question for some options.
If you convert to 1D, peakutil is a package that may help you (though it doesn't appear to be dramatically more powerful than find_peaks for your needs).
For more details, you can also see some of the answers to the questions here, a related question.

Apodization Mask for Fast Fourier Transforms in Python

I need to do a Fourier transform of a map in Python. Fast Fourier Transforms expect periodic boundary conditions, but the input map is not periodic. So I need to apply an input filter/weight slowly tapering the map toward zero at the edges. Are there libraries for doing this in python?
My favorite function to apodize a map is the generalized Gaussian (also called 'Super-Gaussian' which is a Gaussian whose exponent is raised to a power P. By setting P to, say, 4 or 6 you get a flat-top pulse which falls off smoothly, which is good for FFT applications where sharp edges always create ripples in conjugate space.
The generalized Gaussian is available on Scipy. Here is a minimal code (Python 3) to apodize a 2D array with a generalized Gaussian. As noted in previous comments, there are dozens of functions which would work just as well.
import numpy as np
from scipy.signal import general_gaussian
# A 128x128 array
array = np.random.rand(128,128)
# Define a general Gaussian in 2D as outer product of the function with itself
window = np.outer(general_gaussian(128,6,50),general_gaussian(128,6,50))
# Multiply
ap_array = window*array
Such tapering is often referred to as a "window".
Scipy has many window functions.
You can use numpy.expand_dims to create the 2D window you want.
Regarding Stefan's comment, apparently the numpy team thinks that including more than arrays was a mistake. I would stick to using scipy for signal processing. Watch out, as they moved quite a bit of functions around in their 1.0 release so older documentation is, well, quite old.
As a final note: a "filter" is typically reserved for multiplications you apply in the Frequency domain, not spatial domain.

Create 3D- polynomial via numpy etc. from given coordinates

Given some coordinates in 3D (x-, y- and z-axes), what I would like to do is to get a polynomial (fifth order). I know how to do it in 2D (for example just in x- and y-direction) via numpy. So my question is: Is it possible to do it also with the third (z-) axes?
Sorry if I missed a question somewhere.
Thank you.
Numpy has functions for multi-variable polynomial evaluation in the polynomial package -- polyval2d, polyval3d -- the problem is getting the coefficients. For fitting, you need the polyvander2d, polyvander3d functions that create the design matrices for the least squares fit. The multi-variable polynomial coefficients thus determined can then be reshaped and used in the corresponding evaluation functions. See the documentation for those functions for more details.

3d Trapz (or Simps) like Matlab

I have a 3D array that I need to integrate numerically using Python. My array is a function of wavelength, depth and time. It is data that I have modelled numerically using another software package and don't have an analytical form of the function, just the 3d array output from the other package. I need to find the triple integral of this array. In Matlab I use trapz(my_array, 3) where 3 is the ndims to integrate over. The Scipy trapz only seems to work on a single integral.
I think I may have 2 options but I am I need some advice.
opt 1. use 3d interpolation in scipy that returns a function handle, do these exist? the 1d version returns a function, and then use scipy.integrate.tplquad to do the integration over the interpolated function where I use the max and in values in my array as the integration limits.
opt 2. use three nested trapz calls like this suggestion for 2d I found on another site. --> sp.trapz(sp.trapz(f, y[np.newaxis,:], axis=1), x, axis=0))
Can't quite get my head around to make either work. Any help/advice would be appreciated. I need to make sure that my integration error is as low as possible.

Categories

Resources