1

I am trying to create a function to calculate the moving average on a set of data. This is the function:

def averaged_rel_track(navg, rel_values, nb, zeroindex):
    #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
    for blade in range(0,int(nb)):
        av_values=[]
        rel_blade=rel_values[:,blade]
        for rev in range(0,len(rel_blade)):          
            section=rel_blade[rev-int(navg)+1:rev]
            av_value=np.mean(section)
            av_values.append(av_value)

        print av_values

however I would like to add a check to it and I'm strugging witht the best way to implement this... If the number of averages (navg) in the moving average is say 24 the average is taken of the 23 values before the specified element and the element. However, if one of those 24 values is a zero I want the average for that specified element to be zero. I tried:

def averaged_rel_track(navg, rel_values, nb, zeroindex):
    #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
    for blade in range(0,int(nb)):
        av_values=[]
        rel_blade=rel_values[:,blade]
        for rev in range(0,len(rel_blade)):          
            section=rel_blade[rev-int(navg)+1:rev]
            av_value=np.mean(section)
            zero_test= np.where(np.any(section==0))
            print zero_test
            if len(zero_test)==0:
                av_value=None
            av_values.append(av_value)
        print av_values

However the length of zero_test is always 1?

Can anyone think of a way to correct this method or a different method altogether?

Cheers

Ashleigh Clayton
  • 1,443
  • 8
  • 23
  • 32
  • I know this is a bit confusing, but more than happy to give more explination – Ashleigh Clayton Sep 06 '13 at 12:52
  • See examples of efficient moving average calculations here: http://stackoverflow.com/questions/11352047/finding-moving-average-from-data-points-in-python – YXD Sep 06 '13 at 13:01
  • I don't quite understand what you are trying to do though. But you should try replacing all of the lines involving `zero_test` with `if np.any(section == 0):` – YXD Sep 06 '13 at 13:03

1 Answers1

0

Following @Mr E's comment I solved this by replacing zero_test with simply np.any(section==0). This is a much simpler way of doing it.

Ashleigh Clayton
  • 1,443
  • 8
  • 23
  • 32