Compute RMS value over 1-second intervals

11 views (last 30 days)
Karim
Karim on 11 May 2023
Commented: Mathieu NOE on 28 Jun 2023
Trying to get RMS 1s value from a excell file which contains 2 collums of data . collum 1 time and collum 2 is accelaration. I appled Wk filter and code runs fine but now i wanted to Compute RMS value over 1-second intervals but its howing error.
% Compute RMS value over 1-second intervals
windowSize = round(fs); % Number of samples in one second
a_rms = movrms(ac, windowSize); (ERROR in this line)

Answers (2)

Mathieu NOE
Mathieu NOE on 11 May 2023
Edited: Mathieu NOE on 12 May 2023
hello
I am not aware of a matlab function called movrms (but there is a movmean and movsum)
here a code that does the same thing with the possibilty also to add overlap
Edit : improved code
% dummy data
n = 1000;
Fs = 100;
dt = 1/Fs;
t=(0:n-1)*dt;
y = max(0.707,abs(cos(t))+0.1*rand(size(t)));
buffer = Fs; % 1 second buffer
overlap = round(0.5*Fs); % overlap (here 50 % of buffer size)
[t_rms,y_rms] = my_rms(t,y,buffer,overlap);
figure(1),
plot(t,y,t_rms,y_rms,'r-*');
legend('raw data','1 s rms');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t_rms,x_rms] = my_rms(t,x,buffer,overlap)
%%%% main loop %%%%
m = length(x);
dt = mean(diff(t));
shift = buffer-overlap; % nb of samples between 2 contiguous buffers
for ci=1:fix((m-buffer)/shift +1)
start_index = 1+(ci-1)*shift;
stop_index = min(start_index+ buffer-1,m);
t_rms(ci) = dt*(start_index+stop_index)/2; % time (centered in buffer)
x_rms(ci) = sqrt(mean(x(start_index:stop_index).^2));
end
end

Star Strider
Star Strider on 12 May 2023
Try this —
movrms = @(A,k) sqrt(movmean(A.^2,k));
Fs = 1000;
T = 10;
t = linspace(0, T*Fs, T*Fs+1).';
s = randn(size(t));
srms = movrms(s,round(Fs));
figure
plot(t,s, 'DisplayName','Signal')
hold on
plot(t, srms, '-r', 'DisplayName','RMS')
hold off
grid
legend('Location','best')
s = sin(2*pi*t/10);
srms = movrms(s,round(Fs));
Check = srms([1 end-1000])
Check = 2×1
0.7071 0.7071
figure
plot(t,s, 'DisplayName','Signal')
hold on
plot(t, srms, '-r', 'DisplayName','RMS')
hold off
grid
legend('Location','best')
xlim([0 200])
See the documentation on movmean for details. My ‘movrms’ function can be changed to accommodate other arguments to movmean by changing the ‘movrms’ argument list accordingly.
This gives the correct result, the RMS value of a sine being .
.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!