[Numpy-discussion] Correlation filter

Keith Goodman kwgoodman at gmail.com
Fri Nov 20 14:28:49 EST 2009


On Fri, Nov 20, 2009 at 11:17 AM,  <josef.pktd at gmail.com> wrote:
> On Fri, Nov 20, 2009 at 1:51 PM, Keith Goodman <kwgoodman at gmail.com> wrote:
>> def corr3(x, y):
>>    x = x - x.mean()
>>    x /= x.std()
>>    nx = x.size
>>    one = np.ones(nx)
>>    xy = lfilter(x, 1, y)
>>    sy = lfilter(one, 1, y)
>>    sy2 = lfilter(one, 1, y*y)
>>    d = xy / np.sqrt(nx * sy2 - sy * sy)
>>    return d
>
> Is this correct? xy uses the original y and not a demeaned y.

I wouldn't be surprised if I made mistakes. But I don't think I need
to demean y. One way to write the numerator of the correlation
coefficent is

N*sum(xy) - sum(x)*sum(y)

but sum(x) is zero, so it becomes

N*sum(xy)

So I think I only need to demean x. But I'd better test the code. Plus
I have no idea how lfilter will handle my missing values (NaN).



More information about the NumPy-Discussion mailing list