Aplly average to a set of data
1 view (last 30 days)
Show older comments
Angel Lozada
on 19 Jun 2023
Commented: Star Strider
on 20 Jun 2023
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 Comments
Accepted Answer
Star Strider
on 19 Jun 2023
Edited: Star Strider
on 19 Jun 2023
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 Comments
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!