I want to calculate the moving rms of accelerometer data.

Hi
I am working on a project where I get accelerometer data with a vector containing the acceleration (1019970 samples) and another vector containg the time corresponding to the acceleration vector. The sampling time is 1.6667e-04. I want to calculate the moving rms of this signal according to the signal. The formulas I have are for the integral notation and for the discrete time notation as seen below.
How would I implement this in matlab to obtain the moving rms for the acceleration vector (without using simulink).

 Accepted Answer

Try something like this —
Fs = 5000;
T = 100;
t = linspace(0, T*Fs, T*Fs-1)/Fs;
s = sin(2*pi*t*25);
xrms = @(s, n) sqrt(movmean(s.^2, n)); % Calculate RMS Function, 'n': Window Of Consecutive Data
figure
plot(t, s, 'DisplayName','Signal')
hold on
plot(t, xrms(s,100), 'DisplayName','RMS(Signal)')
hold off
grid
legend('Location', 'best')
xlim([0 1])
The ‘xrms’ function produces the expected result of for the RMS value of a sine signal (after a short initial transient) over 100 data points (in this example).
.

7 Comments

I set ‘n’ to be 100:
plot(t, xrms(s,100), 'DisplayName','RMS(Signal)')
Set it to be whatever works with your signal.
Note that this is in samples, so if you want in seconds for example, you would have to calculate that as Fs*sec where ‘Fs’ is the sampling frequency in samples/second and ‘sec’ is the number of seconds.
See the documentation for movmean for other options for ‘n’, as described in Input Arguments. You can pass them directly to ‘xrms’ and they should work with it, or set them in movmean if you do not want to change them. If you want them as arguments to ‘xrms’ as well, passing other arguments would require adding them as arguments both to ‘xrms’ and to movmean.
.
Yes I understand, but I see you use the accelarion as a function, which is not the case in my situation. I have two verctors of 1019970x1, which is the time and acceleration respectively. How do I calculate the moving rms only using the vectors and the formulas stated above.
The xrms function will calculate the moving RMS, since it takes the root of the moving mean of the squares. What exactly is unclear to you?
I see you use the accelarion as a function, which is not the case in my situation.
I do not understandd what you intend by that. It is straightforward to use my function to calculate a different vector using the acceleration signal.
Assuming that you have a time vector with uniform sampling intervals and an acceleration vector and you want to calculate the RMS value of the acceleration signal on a moving window of 5 seconds —
Fs = 250; % Sampling Frequency (Hz)
T = 1000; % Signal Length (seconds)
t = linspace(0, T*Fs, T*Fs-1)/Fs; % Signal Time Vector
acc = sin(2*pi*t*5) + randn(size(t))/5; % Acceleration Signal
xrms = @(s, n) sqrt(movmean(s.^2, n)); % Calculate RMS Function, 'n': Window O
RMSacc = xrms(acc, Fs*5); % RMS Vector — Use 'xrms' To Calculate It From 'acc'
figure
plot(t, acc, 'DisplayName','Acceration Signal')
hold on
plot(t, RMSacc, 'DisplayName','RMS Of ‘acc’ Signal (5 Second Window)')
hold off
grid
xlabel('Time (s)')
ylabel('Amplitude (g)')
legend('Location','best')
xlim([200 210]) % 'Zoom' Axis To See Detail (Optional)
This illustrates how to use the function and how to display its results. The function makes the calculation easier.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!