Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

python - Remove jumps like peaks and steps in timeseries

I have quite a few sensors in the field that measure water pressure. In the past the height of these sensors have been changed quite a few times creating jumps in the timeseries. Since these timeseries are continuous and I have a manual measurement I should technically be able to remove the jumps (by hand this is easy, but there are too many measurements so I need to do it in python).

I've tried removing the jumps using a median filter but this doesn't really work.

My code:

    # filter out noise in signal (peaks)
    minimumPeak = 0.03 # filter peaks larger than 0.03m
    filtered_value = np.array(im.median_filter(data['value'], 5))
    noise = np.array((filtered_value-data['value']).abs() > minimumPeak)
    data.loc[noise, 'value'] = filtered_value[noise]

data is pandas dataframe containing two columns: 'datetime' and 'value'.

I've also tried to do this manually and got it working in a simple case, but not well in any other. Any idea how I would filter out the jumps?

An example is shown in the picture below (yellow indicating the jumps, red the measurement by hand (it is very well possible that this measurement is not in the beginning as it is in this example))

Time series with jumps

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have sharp peaks and steps in your data. I guess you want to

  • remove the peaks and replace by some averaged values
  • remove the steps by cumulative changing the offset of the remaining data values

That's in line with what you said in your last comment. Please note, that this will alter (shift) big parts of your data!

It's important to recognize that the width of both, peaks and steps, is one pixel in your data. Also you can handle both effects pretty much independently.

I suggest to first remove the peaks, then remove the steps.

  1. Remove peaks by calculating the absolute difference to the previous and to the next data value, then take the minimum of both, i.e. if your data series is y(i) compute p(i)=min(abs(y(i)-y(i-1)), abs(y(i+1)-y(i))). All values above a threshold are peaks. Take them and replace the data values with the mean of the previous and the next pixel like.

  2. Now remove the steps, again by looking for absolute differences of consecutive values (as suggested in the comment by AreTor), s(i)=abs(y(i)-y(i-1)) and look for values above a certain threshold. The positions are the step positions. Create an zero-valued offset array of the same size, then insert the differences of the data points (without the absolute value), then form the cumulative sum and subtract the result from the original data to remove the steps.

Please note that this removes peaks and steps which go up as well as down. If you want to remove only one kind, just don't take the absolute value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...