Peak Detection of an FFT signal in jupyter notebook - python

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.

Related

How to apply sgolay function(of matlab) in 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

What specific algorithm is used in NumPy for the mean of an array, and what is its Big O?

For specifics of the question, I can't say since it's work related. I'm seeing some code at work that's using NumPy to compute a mean, along with some other tasks, and I want to rule out NumPy being the source of the O(n^2) behavior that I'm empirically observing in profiling this function.
I'm also curious about how NumPy computes the mean. Shouldn't there be real numerical stability issues in taking a mean of >1mil floats?

Performing UMAP dimension reduction on inconsistently shaped data - python

first question, I will do my best to be as clear as possible.
If I can provide UMAP with a distance function that also outputs a gradient or some other relevant information, can I apply UMAP to non-traditional looking data? (I.e., a data set with points of inconsistent dimension, data points that are non-uniformly sized matrices, etc.) The closest I have gotten to finding something that looks vaguely close to my question is in the documentation here (https://umap-learn.readthedocs.io/en/latest/embedding_space.html), but this seems to be sort of the opposite process, and as far as I can tell still supposes you are starting with tuple-based data of uniform dimension.
I'm aware that one way around this is just to calculate a full pairwise distance matrix ahead of time and give that to UMAP, but from what I understand of the way UMAP is coded, it only performs a subset of all possible distance calculations, and is thus much faster for the same amount of data than if I were to take the full pre-calculation route.
I am working in python3, but if there is an implementation of UMAP dimension reduction in some other environment that permits this, I would be willing to make a detour in my workflow to obtain this greater flexibility with incoming data types.
Thank you.
Algorithmically this is quite possible, but in practice most implementations do not support anything other than fixed dimension vectors. If computing the all pairs distances is not tractable another option is to try to find a way to featurize or vectorize the data in a way that will allow for easy distance computations. This is, of course, not always possible. The final option is to implement things yourself, but this requires handling the nearest neighbour search, which is likely a non-trivial coding project in and of itself.

fft2 different result in numpy and matlab

I was trying to port one code from python to matlab, but I encounter one inconsistence between numpy fft2 and matlab fft2:
peak =
4.377491037053e-223 3.029446976068e-216 ...
1.271610790463e-209 3.237410810582e-203 ...
(Large data can't be list directly, it can be accessed here:https://drive.google.com/file/d/0Bz1-hopez9CGTFdzU0t3RDAyaHc/edit?usp=sharing)
Matlab:
fft2(peak) --(sample result)
12.5663706143590 -12.4458341615690
-12.4458341615690 12.3264538927637
Python:
np.fft.fft2(peak) --(sample result)
12.56637061 +0.00000000e+00j -12.44583416 +3.42948517e-15j
-12.44583416 +3.35525358e-15j 12.32645389 -6.78073635e-15j
Please help me to explain why, and give suggestion on how to fix it.
The Fourier transform of a real, even function is real and even (ref). Therefore, it appears that your FFT should be real? Numpy is probably just struggling with the numerics while MATLAB may outright check for symmetry and force the solution to be real.
MATLAB uses FFTW3 while my research indicates Numpy uses a library called FFTPack. FFTW is one of the standards for FFT performance and uses a number of tricks to work quickly and perform calculations to the best precision possible. You can incredibly tiny numbers and this offers a number of numerical challenges that any library will be hard pressed to resolve.
You might consider executing the Python code against an FFTW3 wrapper like pyFFTW3 and see if you get similar results.
It appears that your input data is gaussian real and even, in which case we do expect the FFT2 of the signal to be real and even. If all your inputs are this way you could just take the real part. Or round to a certain precision. I would trust MATLAB's FFTW code over the Python code.
Or you could just ignore it. The differences are quite small and a value of 3e-15i is effectively zero for most applications. If you have automated the comparison, consider calling them equivalent if the mean square error of all the entries is less than some threshold (say 1e-8 or 1e-15 or 1e-20).

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