0

I am trying to generate a velocity signal from acceleration data in an array acc, I have also time another array with the same length in numpy.

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate

time = np.array([ , , ... , , ])
acc = np.array([ , , ... , , ])
# using this
vel=integrate.cumtrapz(acc,time,initial=0)

second plot should be similar to the first one. another view

With some basic calculus it is obvious that the solution should be a similar plot, but for some reason I get these

Is there any suggestions?, maybe it is because the initial conditions but I'm not sure, and also I do not have access to them, since it is experimental data.

I also take the same data and put it on LabVIEW and the integration does this image of velocity.

enter image description here

but clearly it is not the same with the plt.plot(time,vel).

2
  • "...for some reason I get these" What is the question? What, specifically, is wrong with these plots? Commented Oct 23, 2014 at 14:56
  • I am uploading a file. Commented Oct 23, 2014 at 17:55

3 Answers 3

1

From the plot of acc, you can make a rough estimate of the amplitude to be 1.4 and the frequency to be 136 Hz, or 854 radians/sec. So, a rough model of acc is 1.4*cos(854*t) + b. I'm ignoring the phase of the oscillation, and b is a constant that is too small to estimate from the plot. In python, you can estimate b with acc.mean().

Then, by integrating, the velocity should be roughly 1.4/854*sin(854*t) + b*t = 0.00164*cos(854*t) + b*t. So you should expect the local peak-to-peak variation of vel to be about 0.0033, which agrees with your plot of vel.

b is the slope of the linear trend in vel, and from the plot of vel, b can be estimated to be roughly 0.0085. So I would expect acc.mean() to be about 0.0085. Does that agree with your data?

Sign up to request clarification or add additional context in comments.

4 Comments

yes, in this case looks like a sin curve, but the solution is more complicated. I think that the problem it is on the integration function and its values under the axis, if you now what I mean.
I think the best way to do this is with a functions that gives an array with every trap integration. Because it is cumulative sum and there are negative numbers under the curve the plot goes wrong.
Sorry, but I still don't understand what is wrong. Please edit the question to explain more clearly what the problem is. If possible, give an example of the output that you expected, and explain how it differs from what you got. As it is now, I don't see anything wrong.
I think I got something, when I do vel=integrate.cumtrapz(acc-np.mean(acc),time,initial=0) I get a better signal but then I have to apply some filters and with luck it'll work.
0

Since there are not many details: 1) Is plt pylab? 2) If so, use plt.plot(time, vel, '.') to see the trend of dots, maybe you could see some problems. 3) Check out this link

4) Please post more details so that people can understand your problem better.

Hopefully these words might be at least a little helpful :-)

3 Comments

what is the data? why is acc not used
@ChesterL sorry, I checked it now.
according to the document, pyplot.plot() takes lists as parameters, I am not sure if numpy array would work well with plot(). Have you try plt.plot(acc) to see how the data goes ignoring time? Are you sure the import data files are similar?
0

The problem is what you mentioned, the initial condition. The integration begins at an arbitrary phase depending on the sampling of the experimental data. This represents an arbitrary constant added to the input to the integral, and consequently a linear ramping component of the output. (Ideally you should begin sampling when the data is zero, for instance before the acceleration starts, but this is frequently not possible.)

In the case when output is known to be periodic, it is OK to simply detrend the output by subtracting the appropriate linear fit. Note that simply fitting a line to the data then subtracting that line is not adequate, however, even if you take care to fit to an integer number of cycles. This is obvious by considering the case where the sampled data contains 1 or 2 cycles, there are only specific starting phases where the data is symmetric and the correct slope is computed.

Instead, the fit must be done in three stages: 1) fit a line through e.g. all the peaks, 2) subtract this line from the data, and 3) compute the average of the resulting output and subtract that. Step 2 results in a periodic output that eliminates the component rising linearly with time, but leaves an arbitrary offset. The last step is necessary to remove the offset (assuming you expect the mean displacement to be zero).

Step (1) can be accomplished by other means - e.g. by fitting to an odd number of half-cycles, or making sure the fit is from e.g. the first peak to the last peak. As mentioned, fitting from the first peak to the last valley gives an incorrect slope, however.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.