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])

采纳的回答

Star Strider
Star Strider 2023-6-19
编辑:Star Strider 2023-6-19
This seems to be a slightly different version of your earlier Question: Plot data from txt file.
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.
.
  2 个评论
Angel Lozada
Angel Lozada 2023-6-20
Star Strider.
I really appreciate your collaboration.
Regards.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by