Aplly average to a set of data
2 次查看(过去 30 天)
显示 更早的评论
Hello
I will really appreciate your help regarding follow code.
As it can see, x axis is measured in second, each second has 10 numbers or data samples (do a zoom in, to see a specific second).
I am trying to apply movmean function in order to obtain one (1) number as result of the average of each ten numbers per second (see attached file Figure 2 for reference).
Regards.
clear;
Uzp = unzip('Data 2.zip');
data = readmatrix('Data 2');
t = seconds(data(:,1));
ixv = ceil(data(:,1) + 1E-12);
secv = accumarray(ixv, (1:size(data,1)).', [], @(x){data(x,[2 3])});
A2 = reshape(cell2mat(secv).', 2,10,[]);
tv = unique(ixv);
figure
plot(tv, squeeze(A2(1,:,tv)), '.')
ylabel('Phase (°)')
grid
xlabel('Time')
xlim([tv(1) tv(end)])
xlim([0 86400])
0 个评论
采纳的回答
Star Strider
2023-6-19
编辑:Star Strider
2023-6-19
This does the same thing,, except that instead of returning a cell array of the individual points, it takes the mean of each interval and returns it —
clear;
Uzp = unzip('Data 2.zip');
data = readmatrix('Data 2');
t = seconds(data(:,1));
ixv = ceil(data(:,1) + 1E-12);
secv = accumarray(ixv, (1:size(data,1)).', [], @(x){mean(data(x,[2 3]))});
secm = cell2mat(secv); % Create Matrix From Cell Array
% A2 = reshape(cell2mat(secv).', 2,10,[]); % Not Needed Here
tv = unique(ixv);
figure
plot(tv, secm, '-')
ylabel('Phase (°)')
grid
xlabel('Time')
xlim([tv(1) tv(end)])
xlim([0 86400])
figure
plot(tv, secm, '-')
ylabel('Phase (°)')
grid
xlabel('Time')
xlim([tv(1) tv(end)])
xlim([0 1E+2])
figure
stairs(tv, secm)
ylabel('Phase (°)')
grid
xlabel('Time')
xlim([tv(1) tv(end)])
xlim([0 1E+2])
The difference here is that movmean creates a moving average of adjacent elements. This code creates the mean of consecutive 10-element segments of the signal, so not a moving average and instead so sort of average of adjacent 10-element blocks of the data.
NOTE — The ‘secm’ matrix has a row length that is 1/10 the length of ‘data’. So it does what you want it to do.
EDIT — Re-ran code.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!