# Instantaneous velocity and displacement from acceleration signal using a proper filtering method

first I need to mention I'm new to signal processing. here is the situation: I have an acceleration time-series derived from an accelerometer

I wanted to imply a filtering method like high pass filter to denoise the signal. Using Fast Fourier Transform, I need first to decite on Cutoff frequency value. After looking at fft(acc) I thought the cutoff frequency should be 5hz I must note for x-axis (frequency) I just invert time (Fr=1/t) hope it's correct.

the estimated FFT's values are complex (not real numbers) which I force to work on the real part not imaginary. here is the code I used to filter acceleration signal noise by fft-highpass filter:

        % Matlab_code
% Filtering signals by fft-HighPass
%%
figure(1)
plot(t,acc,'b')
xlabel('Time(s)');ylabel('Acceleration (m/s^2)');
hold on
%%
Ts=mean(diff(t)); % Sampling rate
Fs=1/Ts;          % Sampling Frequency
Fc=5;             % Cutoff frequency = 5 hertz
fft_aac=real(fft(acc));
signal_temp=[acc,1./t];
signal=signal_temp;
for i=1:length(signal_temp)
if signal_temp(i,2)<5
signal(i,1)=0;
else
signal(i,1)=signal_temp(i,1);
end
end
filtered_acc=real(ifft(signal(:,1)));
plot(t,filtered_acc,'r')

%


Now the graph below is the result. the blue line is the noisy one and the red line is filtered signal. the filtered acceleration signals aren't even in the range of acceleration data but it's simply a noisy straight line.

here I listed my questions: 1- why is that happening? filtering didn't work well! 2- Do I need to use Hi pass or low pass filter by the way 3- What is the right way to choose cutoff frequency

please also help me with commenting on code and the way I approached filtering signal noise.

And after having a good filtering do I need to use a numerical integration (like trapezoidal integration method) to measure the instantaneous velocity and position? Thanks in advance

• The just using the real part is wrong. I suggest you use a Matlab timeseries class. The filtering methods work and don’t require much details. If you want to learn more, it makes a good reference implementation to compare the results of what you develop
– user28715
Mar 26, 2018 at 14:44
• Thanks for your comment dear @Stanley Pawlukiewicz. Could you tell me how timeseries class can help? If I use the whole part of fft value (real +imaginary) the code that says "make zero everything that is smaller than 5 hz" won't work. I'll try to modify the code by the way. I'd be appreciate it if you can explain more so I can prepare data for integration Mar 27, 2018 at 4:16
• >>help timeseries
– user28715
Mar 27, 2018 at 4:21
• I meant how that suppose to help filtering. timeseries make a time-series I've already made at first place. Mar 27, 2018 at 5:24

There a a number of conceptual problems with you code. The first has to do with filtering by zeroing out FFT bins. This is covered in:

Why is it a bad idea to filter by zeroing out FFT bins?

The second has to do with just taking the real part of the fft. I don't understand why you did this but this is the wrong way to do the wrong thing.

If your near term priority is to process your data. I suggest you discard most of your code and take an entirely different approach with using the built in Matlab timeseries class and use one of the built in filter methods on the object you create. The plot methods are also easy to use and you can set units on the object you create. This is the most direct way to a "correct" result with your data.

If you want to understand how to write your own correct code, this will take more time. There is more than one way to filter data and choosing which takes some time to learn. You can look at:

What resources are recommended for an introduction to DSP?

Essentially, I'm pointing you at a number of books that are several hundreds of pages of study, which is the long term answer to your problem. You will know what is correct.

If you do some Googling, you can probably find something in the middle that suits your needs but I can't recommend anything because I don't know what you know and don't know.

If you may permit the analogy, If one sees someone drowning, do you throw them a floatation device or try to teach them to swim.

Someone else here may provide a suitable solution.

It is to your credit that you have shown genuine effort.

• Thanks for your nice answer. I actually need a floating device with some swimming lessons to reach the best result island. so your point is to use filtering on a time-series of data I have. I used low-pas filtering, zero phase and some more and I've got better results but when I want to estimate velocity and position by integration of acceleration data I get bad result. some recommend use a Kalman filtering method which I haven't yet find a solution on it Mar 28, 2018 at 4:44
• Well a Kalman Filter is another book or 2. Recovering position from acceleration can be difficult even for a Kalman Filter. Error accumulates aa time increases and you need to know the initial position and velocity. Your data looks horrible. If you know those initial values there are some easier things to try. Alpha Beta Filter is a simple dumb KF. Savaski Golay Filter is another approach. Both have WikiP articles
– user28715
Mar 28, 2018 at 8:17