How to NaN data in a time series if it changes quickly?

Hi, I have a time series, where the data occasionally drops in value quickly due to artifacts. I would like a fucntion that moves along the data and if it detects a drop of 5 over two consecutive data points, it turns both of those data points into NaNs.
I'm quite limited in matlab experiance, so was wondering if anyone has any ideas how to approach this? Thanks Ben

3 Comments

Are you storing the time series as vectors?
Why over 2? Why keep it if the drop is only 1 element long? Also, do you have the Image Processing Toolbox, because this is easy if you do.
Hi Image Analyst. I do have the Image Processing toolbox. How would I use it to achieve this?
Your code also seems to be onthe right track, and I am currently trying to adapt it to my data. Thank you.

Sign in to comment.

 Accepted Answer

Cedric
Cedric on 18 Feb 2013
Edited: Cedric on 19 Feb 2013
Or this (assuming that your data is stored in variable data):
lid = diff(data) <= -5 ;
data([lid, 0] | [0, lid]) = NaN ;

1 Comment

This is great. I had to adjust it slightly to run on drops, an their associated rises. Thanks heaps Ben

Sign in to comment.

More Answers (1)

Try this:
% Create sample data.
data = 1 : 30;
data(10) = 2; % drop only one element long.
data(20:21) = [14, 9] % 2 element long drop.
data(25:27) = [14, 9, 3] % 3 element long drop.
subplot(1,2,1);
plot(data, 'bd-');
grid on;
% Find differences
diffs = [0 diff(data)]
% Find drops of 5 or more.
bigDrops = num2str(diffs <= -5)
bigDrops(bigDrops==' ')=[] % Get rid of spaces.
% Get rid of single element drops.
bigDrops=strrep(bigDrops, '010', '000')
% Replace data with nans in those elements.
data(bigDrops == '1') = nan;
% Plot results.
subplot(1,2,2);
plot(data, 'bd-');
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

Community Treasure Hunt

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

Start Hunting!