Thursday 12 September 2013

Dealing with zero's in a moving average

Dealing with zero's in a moving average

EDIT
My original post was not the best explination, so I have rewritten it.
So what I'm trying to do is calculate the moving average on an array of
data. Each average is taken over, for example, the 24 values. However, I
have two conditions that need to be changed in my current function.
Take an average over 24 values that ARE NOT zero as zero is considered as
bad data, so I want 24 'good' values for the average.
If there are five consecutive zeros, I do not want to take the average and
simply say the average is zero.
I need to Here is my current function for this, but it needs updating to
encompass these changes.
def averaged_rel_track(navg, rel_values, nb):
'''function to average the relative track values for each blade. This is
dependant on the number values specified by the user to average over in a
rolling average'''
avg_rel_track=[]
for blade in range(0,int(nb)):
av_values=[]
rel_blade=rel_values[:,int(blade)]
for rev in range(0,len(rel_blade)):
section=rel_blade[rev-int(navg)+1:rev+1]
print section
if np.any(section==0):
av_value=0
else:
av_value=np.sum(section)/int(navg)
print av_value
av_values.append(av_value)
avg_rel_track.append(av_values)
avg_rel_track=np.array(avg_rel_track)
return avg_rel_track.transpose()
At the moment there is alot of checking involved.
Is there a function where you can select X number of values that are not
zero/none? Currently what I'm attempting to do works like this:
Select a section of data than is N values long
x= number of zeros/nan's in the data
Extend section by x values
But this doesn't work as then I need to check that the new section does
not contain zeros and it would detect the original zeros. I could check
the extension for zeros, repeating the process, but this seems like a very
long winded way to do this.
I know of scipy.stats.nanmean that will ignore the none values when
averaging the data.
If anyone could help that would be great, but the main question I would
like advice on is:
Is there a function that will select N values that are not zero or one?

No comments:

Post a Comment