Naumi
Naumi

Reputation: 51

Is cross-correlation between two signals affected by scaling?

By my understanding, translations and scaling do not affect the correlation between two series. However, I'm using the correlate function by from the scipy python library and the MinMaxScaler from the sklearn library and I get totally different results for the cross-correlation of 2 series before and after scaling them to [-1, 1]. The plots of the two different cross-correlations can be seen on the attached image!

enter image description here

Is my assumption that scaling does not affect the correlation wrong?

Thanks!

Upvotes: 2

Views: 1452

Answers (1)

OverLordGoldDragon
OverLordGoldDragon

Reputation: 19776

MinMaxScaler maps data between (min, max), which can change the DC offset and (consequently) polarity of a signal (but not frequency); cross-correlation responds strongly to both these traits. To illustrate, a rising sine:

import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as sig
from sklearn.preprocessing import MinMaxScaler

#%%##########################################################
x = np.linspace(0, 5, 60)  # [0, 0.084, 0.169, ..., 4.91, 5]
x = x.reshape(-1, 1)       # (samples, features) = (60, 1)
y = x + np.sin(x)          # rising sine above x-axis
y_scaled = MinMaxScaler((-1, 1)).fit_transform(y)

#%%##########################################################
plt.plot(y)
plt.plot(y_scaled)
plt.axhline(0, color='k', linestyle='--')
plt.show()
#%%#####################################
plt.plot(sig.correlate(x, y))
plt.plot(sig.correlate(x, y_scaled))
plt.show()

I did say "can"; the condition for not changing cross-correlation shape is for neither polarity nor DC offset to change between transforms - i.e. the transform can be obtained purely by scaling. E.g. sin(x)/2 -> sin(x).

I recommend chapters 6 & 7 of this textbook, which present an excellent intuitive explanation of convolution, and then cross-correlation. There's also an interactive illustration.

Upvotes: 1

Related Questions