Movavg function doesn't compute values for the entire timeseries

6 views (last 30 days)
I use the movavg function twice to do a double exponential smoothing on timeseries data, and for some reasong it's only computing it for the first few points in each column. below is the code I use and I attached the timetable.
load august_filtered_data.mat
figure
plot(signal_timetable.Time,signal_timetable.Signal)
smoothed_data = twoexp_movavg(signal_timetable,60);
figure
plot(smoothed_data.Time,smoothed_data.Signal)
function [TT] = twoexp_movavg(TT,numpts)
ma = movavg(TT{:,:},'exponential',numpts);
dma = 2*ma - movavg(ma,'exponential',numpts);
TT{:,:} = dma;
end
  1 Comment
Poison Idea fan
Poison Idea fan on 9 Sep 2022
Moved: Star Strider on 9 Sep 2022
I'm sorry I should've been more diligent. By mistake, i uploaded the output of the function I included. I also changed the window size to 1 to see if I would get the same number of points output as a window size of 60 points. Below is a more accurate example of how I call the function. This is timetable is about an hours worth of data out of the weeks of data in the timeseries.
figure
load august_filtered_data.mat
figure
plot(signal_timetable.Time,signal_timetable.Signal)
smoothed_data = twoexp_movavg(signal_timetable,60);
figure
plot(smoothed_data.Time,smoothed_data.Signal)

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 9 Sep 2022
Use the fillmissing function —
LD = load(websave('august_filtered_data','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1120940/august_filtered_data.mat'))
LD = struct with fields:
signal_timetable: [5000×4 timetable]
signal_timetable = LD.signal_timetable
signal_timetable = 5000×4 timetable
Time Signal power filter state Tau ___________________ ____________________________________ ____________________________________ ____________ ________________________ 2022-08-12 12:05:45 11.589 4.8807 3.3609 4.0757 563.4 596.26 463.61 697.23 1 7.1129e-05 2.7593e-05 2022-08-12 12:05:46 12.201 4.7314 2.9541 4.1898 564.9 595.89 463.76 699.01 1 7.1218e-05 2.7614e-05 2022-08-12 12:05:47 12.387 NaN NaN 4.093 563.55 596.64 467.3 698.6 1 7.1079e-05 2.756e-05 2022-08-12 12:05:48 11.609 5.6979 2.2949 3.8749 562.94 598.08 464.41 698.33 1 7.1172e-05 2.76e-05 2022-08-12 12:05:49 11.483 4.9726 NaN 3.8147 563.29 596.58 466.04 698.45 1 7.1256e-05 2.7607e-05 2022-08-12 12:05:50 11.232 4.9261 NaN 3.2259 563.14 600.08 467.07 699.1 1 7.1342e-05 2.7585e-05 2022-08-12 12:05:51 11.323 5.6548 2.6544 3.4456 562.36 604.4 465.48 698.18 1 7.1049e-05 2.7515e-05 2022-08-12 12:05:52 11.399 5.4601 3.0548 3.8976 561.74 607.38 465.12 698.1 1 7.1229e-05 2.7558e-05 2022-08-12 12:05:53 11.097 5.2122 NaN 2.9902 562.23 605.41 466.19 699.12 1 7.1339e-05 2.7567e-05 2022-08-12 12:05:54 11.916 5.4602 3.4976 3.7028 562.71 602.71 465.85 698.96 1 7.1223e-05 2.7573e-05 2022-08-12 12:05:55 11.993 NaN 2.8391 3.2377 562.36 602.79 464.4 698.01 1 7.1142e-05 2.7562e-05 2022-08-12 12:05:56 12.065 3.916 3.5892 NaN 563.01 609.2 462.94 698.79 1 7.1209e-05 2.7597e-05 2022-08-12 12:05:57 11.284 4.8572 3.3282 NaN 562.83 606.7 463.16 697.8 1 7.1171e-05 2.7542e-05 2022-08-12 12:05:58 12.256 5.4621 3.124 3.7976 564.55 608.55 462.84 699.32 1 7.1159e-05 2.7577e-05 2022-08-12 12:05:59 12.37 4.5209 2.6034 3.1908 564.21 608.76 460.09 699.66 1 7.127e-05 2.7541e-05 2022-08-12 12:06:00 11.756 4.4921 3.5136 NaN 563.71 608.37 458.6 698.06 1 7.1124e-05 2.7549e-05
NrNaNs = nnz(isnan(signal_timetable.Signal))
NrNaNs = 5385
NrNaNs4 = nnz(isnan(signal_timetable.Signal(:,4)))
NrNaNs4 = 1458
NotNan4 = numel(signal_timetable.Signal(:,4)) - NrNaNs4
NotNan4 = 3542
signal_timetable = fillmissing(signal_timetable, 'linear')
signal_timetable = 5000×4 timetable
Time Signal power filter state Tau ___________________ ____________________________________ ____________________________________ ____________ ________________________ 2022-08-12 12:05:45 11.589 4.8807 3.3609 4.0757 563.4 596.26 463.61 697.23 1 7.1129e-05 2.7593e-05 2022-08-12 12:05:46 12.201 4.7314 2.9541 4.1898 564.9 595.89 463.76 699.01 1 7.1218e-05 2.7614e-05 2022-08-12 12:05:47 12.387 5.2146 2.6245 4.093 563.55 596.64 467.3 698.6 1 7.1079e-05 2.756e-05 2022-08-12 12:05:48 11.609 5.6979 2.2949 3.8749 562.94 598.08 464.41 698.33 1 7.1172e-05 2.76e-05 2022-08-12 12:05:49 11.483 4.9726 2.4147 3.8147 563.29 596.58 466.04 698.45 1 7.1256e-05 2.7607e-05 2022-08-12 12:05:50 11.232 4.9261 2.5346 3.2259 563.14 600.08 467.07 699.1 1 7.1342e-05 2.7585e-05 2022-08-12 12:05:51 11.323 5.6548 2.6544 3.4456 562.36 604.4 465.48 698.18 1 7.1049e-05 2.7515e-05 2022-08-12 12:05:52 11.399 5.4601 3.0548 3.8976 561.74 607.38 465.12 698.1 1 7.1229e-05 2.7558e-05 2022-08-12 12:05:53 11.097 5.2122 3.2762 2.9902 562.23 605.41 466.19 699.12 1 7.1339e-05 2.7567e-05 2022-08-12 12:05:54 11.916 5.4602 3.4976 3.7028 562.71 602.71 465.85 698.96 1 7.1223e-05 2.7573e-05 2022-08-12 12:05:55 11.993 4.6881 2.8391 3.2377 562.36 602.79 464.4 698.01 1 7.1142e-05 2.7562e-05 2022-08-12 12:05:56 12.065 3.916 3.5892 3.4243 563.01 609.2 462.94 698.79 1 7.1209e-05 2.7597e-05 2022-08-12 12:05:57 11.284 4.8572 3.3282 3.611 562.83 606.7 463.16 697.8 1 7.1171e-05 2.7542e-05 2022-08-12 12:05:58 12.256 5.4621 3.124 3.7976 564.55 608.55 462.84 699.32 1 7.1159e-05 2.7577e-05 2022-08-12 12:05:59 12.37 4.5209 2.6034 3.1908 564.21 608.76 460.09 699.66 1 7.127e-05 2.7541e-05 2022-08-12 12:06:00 11.756 4.4921 3.5136 3.1423 563.71 608.37 458.6 698.06 1 7.1124e-05 2.7549e-05
figure
plot(signal_timetable.Time, signal_timetable.Signal, '-')
grid
xlabel('Time')
ylabel('Amplitude')
title('Original')
legend(compose('Signal %d',1:size(signal_timetable.Signal,2)), 'Location','best')
signal_timetablef = twoexp_movavg(signal_timetable,60);
figure
plot(signal_timetablef.Time, signal_timetablef.Signal,'-')
grid
xlabel('Time')
ylabel('Amplitude')
title('Filtered')
legend(compose('Signal %d',1:size(signal_timetable.Signal,2)), 'Location','best')
function [TT] = twoexp_movavg(TT,numpts)
ma = movavg(TT{:,:},'exponential',numpts);
dma = 2*ma - movavg(ma,'exponential',numpts);
TT{:,:} = dma;
end
Experiment with different function parameters or different fillmissing 'method' options to get the desired result.

More Answers (1)

Poison Idea fan
Poison Idea fan on 9 Sep 2022
The issue is coming from NaN values in the timetable. I treat the data before I smooth it with a filter for erroneous points and replace them with NaN so I still have 1 second time resolution. If I remove them before calling the smoothing function the issue goes away. I don't think movavg has an option to omit NaN values like the mean function does.
  1 Comment
Poison Idea fan
Poison Idea fan on 9 Sep 2022
I accepted before the edit. I went to change it to accept yours but alas, it has been deleted. All my fault though.

Sign in to comment.

Categories

Find more on Time Series in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!