Is there any better way to find the average of a dynamic signal for a specific time like 10s or something.?
3 次查看(过去 30 天)
显示 更早的评论
I have a dynamic signal which needs to be taken average every X seconds. Please post down your ideas how to do it.
p.s : without using mean
0 个评论
回答(2 个)
Mathieu NOE
2021-11-22
hello
this is a demo code for splitting a signal in smaller chunks (buffer) with or without overlap (as you prefer)
for each buffer you can compute whatever metrics you need (min / max / rms / sigma....)
clc
clearvars
% dummy data
data = rand(320,3); % data must be column oriented (number of rows = number of samples)
buffer = 25; % nb of samples for averaging
%% zero overlap averaging (unweighted)
[m,n] = size(data);
for ci=1:fix(m/buffer)
start_index = 1+(ci-1)*buffer;
stop_index = min(start_index+ buffer-1,m);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
avg_data(ci,:) =mean(data(start_index:stop_index,:)); %
end
figure(1),
plot(time_index,avg_data,'+-');
% return
%% averaging with overlap
clearvars
% dummy data
data = rand(320,3);
buffer = 25; % nb of samples in one buffer (buffer size)
overlap = 10; % overlap expressed in samples
%%%% main loop %%%%
[m,n] = size(data);
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);
time_index(ci) = round((start_index+stop_index)/2); % time index expressed as sample unit (dt = 1 in this simulation)
avg_data(ci,:) = mean(data(start_index:stop_index,:)); %
end
figure(2),
plot(time_index,avg_data,'+-');
0 个评论
Peter Perkins
2021-11-23
Why would you not use mean?
In any case, make a timetable, and use something like
retime(tt,'regular','mean','TimeStep',seconds(10))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Programmatic Model Editing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!