// 计算相关性 float crossCorrelation(uint16_t x, uint16_t y, int delay) { float meanX = 0; float meanY = 0; float varX = 0; float varY = 0; float covXY = 0; // 计算均值和协方差 for (int i = 0; i < BUFFER_SIZE-delay; i++) { meanX += x[i]; meanY += y[i+delay]; covXY += x[i] * y[i+delay]; } meanX /= BUFFER_SIZE-delay; meanY /= BUFFER_SIZE-delay; covXY /= BUFFER_SIZE-delay; // 计算方差 for (int i = 0; i < BUFFER_SIZE-delay; i++) { varX += (x[i] - meanX) * (x[i] - meanX); varY += (y[i+delay] - meanY) * (y[i+delay] - meanY); } // 计算相关性 float corr = covXY / sqrt(varX * varY); return corr; }
时间: 2023-05-27 14:01:27 浏览: 194
// The function calculates the cross correlation between two signals x and y with a given delay
float crossCorrelation(uint16_t x[], uint16_t y[], int delay) {
float meanX = 0; // Mean of x
float meanY = 0; // Mean of y
float varX = 0; // Variance of x
float varY = 0; // Variance of y
float covXY = 0; // Covariance of x and y
// Calculate mean and covariance
for (int i = 0; i < BUFFER_SIZE - delay; i++) {
meanX += x[i];
meanY += y[i + delay];
covXY += x[i] * y[i + delay];
}
meanX /= BUFFER_SIZE - delay;
meanY /= BUFFER_SIZE - delay;
covXY /= BUFFER_SIZE - delay;
// Calculate variance
for (int i = 0; i < BUFFER_SIZE - delay; i++) {
varX += pow((x[i] - meanX), 2);
varY += pow((y[i + delay] - meanY), 2);
}
// Calculate correlation
float corr = covXY / sqrt(varX * varY);
return corr;
}
// Note: the input signals x and y should be arrays of uint16_t type, and they should have the same length (BUFFER_SIZE).
阅读全文