Noisy plot after deviation (no sensor)
Show older comments
Hello,
i'm trying to get the acceleration from position and time data. (I dont get the data from an accelerometer, this would explain the noise! )The plot seems to be really noisy, is there a good explanation? Shouldn't I be able to see a perfect sine wave? I'm thankful for every idea, thanks!
Here my code and a picture of the result
h = 1:height(z_irl);
for i = 1:height(z_irl)
t(i) = 0.004 * h(i);
end
t = t';
dt = diff(t);
dz_irl = diff(z_irl);
%velocity
vel_irl = dz_irl./dt(1:end);
dvel_irl = diff(vel_irl);
%acceleration
acc_irl = dvel_irl./dt(2:end);
acc_irl(numel(t)) = 0;
figure(4)
plot(t,acc_irl,'-b')
xlabel('time')
ylabel('acceleration')
hold off

7 Comments
Scott MacKenzie
on 9 Jun 2021
Your code doesn't execute --> Unrecognized function or variable 't'.
Perhaps update your question, loading t with the required data.
Bruce Rogers
on 9 Jun 2021
Edited: Bruce Rogers
on 9 Jun 2021
Radhey Shyam Meena
on 9 Jun 2021
Edited: Radhey Shyam Meena
on 9 Jun 2021
Hi Bruce,
I understand that you are trying to plot acceleration over time, after computing the acceleration from the position values as specified in the z_irl_data.txt file.
Assuming, that you accept the values computed for acceleration since you did not raise a concern for their accuracy or authenticity, and your query is more about an explanation of the plot you are seeing.
Since the data is quite large (18k+), and it is squeezed along the time axis, you might not be able to visualize it correctly if you fit the entire data in one figure, and hence making it look like noisy.
One way is to look at only the values of first 50 points, to visualize the data correctly. That can be done as
plot(t(1:50),acc_irl(1:50),'-b')
And verify if this is the visualization you were looking for.
this is how it looks for me:

If you are concerned about the accuracy of the acceleration data OR you want to see a more smooth curve, probably by smoothening the values out on multiple data points (like moving average) OR Otherwise, you can update the question accordingly.
I hope this helps.
Regards.
Scott MacKenzie
on 9 Jun 2021
OK, I played with your code a bit. I don't see any issues. Perhaps the noise in the acceleration data is a natural follow-on to the noise in the velocity data. Below is what I put together. I'm not sure if this is of any help, but here you go, anyway. Good luck.
z_irl = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/647435/z_irl_data.txt');
t = 0.004 * (1:length(z_irl));
t = t';
dt = [0; diff(t)];
tiledlayout(3,1);
% position
nexttile;
plot(t,z_irl);
xlabel('time');
ylabel('position');
% velocity
dz_irl = [0; diff(z_irl)];
vel_irl = dz_irl./dt;
nexttile;
plot(t, vel_irl);
xlabel('time');
ylabel('velocity');
% acceleration
dvel_irl = [0; diff(vel_irl)];
acc_irl = dvel_irl./dt;
nexttile;
plot(t, acc_irl, '-b');
xlabel('time');
ylabel('acceleration');

Bruce Rogers
on 10 Jun 2021
Bruce Rogers
on 10 Jun 2021
Scott MacKenzie
on 10 Jun 2021
@Bruce Rogers I'm going to post a solution in a minute that shows the velocity and acceleration as a sine wave.
Accepted Answer
More Answers (1)
Scott MacKenzie
on 10 Jun 2021
@Bruce Rogers I added some filtering using MATLAB's smoothdata function. The result is a pretty good sine wave both for velocity and acceleration. smoothdata "returns a moving average of the elements of a vector using a fixed window length that is determined heuristically". The windows length is not specifiied, but you can play with this using parameters described in the documentation. The result is below. Note that the new waveforms experience both phase shift and attenuation as a result of the filtering. The acceleration waveform is very attenuated, indicating noise as large spikes in the raw data. Anyway, hope this helps. Good luck.
z_irl = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/647435/z_irl_data.txt');
t = 0.004 * (1:length(z_irl));
t = t';
dt = [0; diff(t)];
tiledlayout(3,1);
% position
nexttile;
plot(t,z_irl);
xlabel('time');
ylabel('position');
% velocity
dz_irl = [0; diff(z_irl)];
vel_irl = dz_irl./dt;
vel_irl = smoothdata(vel_irl); % filter using moving average
nexttile;
plot(t, vel_irl);
xlabel('time');
ylabel('velocity');
% acceleration
dvel_irl = [0; diff(vel_irl)];
acc_irl = dvel_irl./dt;
acc_irl = smoothdata(acc_irl); % filter using moving average
nexttile;
plot(t, acc_irl, '-b');
xlabel('time');
ylabel('acceleration');

1 Comment
Bruce Rogers
on 10 Jun 2021
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

