%% Multichannel Signal with Spikes and Missing Samples
% Generate a two-channel signal consisting of sinusoids of different frequencies. Place spikes in random places. Use NaNs to add missing samples at random. Reset the random number generator for reproducible results. Plot the signal.

rng('default')

n = 59;
x = sin(pi./[15 10]'*(1:n)+pi/3)';

spk = randi(2*n,9,1);
x(spk) = x(spk)*2;
x(randi(2*n,6,1)) = NaN;

plot(x)

%
% Filter the signal using medfilt1 with the default settings. Plot the filtered signal. By default, the filter assigns NaN to the median of any segment with missing samples.

y = medfilt1(x);
plot(y)

%
% Transpose the original signal. Filter it again, specifying that the function work along the rows. Exclude the missing samples when computing the medians. If you leave the second argument empty, then medfilt1 uses the default filter order of 3.

y = medfilt1(x',[],[],2,'omitnan');
plot(y')

%
% The function cannot assign a value to the segment that contains only NaNs. Increase the segment length to fix this issue. The change also removes the outlier more thoroughly.

y = medfilt1(x,4,'omitnan');
plot(y)

%
% The default zero padding results in the function's underestimating the signal values at the edges. Lessen this effect by using decreasing windows to compute the medians at the ends.

y = medfilt1(x,4,'omitnan','truncate');
plot(y)

% Copyright 2015 The MathWorks, Inc.