Skip to content
DeepForgeby svx
All problems
HardTime Seriests-224

Spectral Coherence Lite

Problem

Compute the magnitude-squared coherence between two series at frequency bin k: abs(conj(X[k]) * Y[k]) / sqrt(|X[k]|^2 * |Y[k]|^2), where X and Y are discrete Fourier transforms.

Return 0.0 when lengths differ, k is out of range, or either spectrum has (near) zero power.

Hint

Coherence near 1 means the two series share power at that frequency.

Test cases

case 1
in: [[1,2,3,4],[2,4,6,8],1]
expected: 1
case 2
in: [[1,0,-1,0],[1,2,3,4],1]
expected: 1
case 3
in: [[1,1,1,1],[0,1,0,1],1]
expected: 0
case 4
in: [[1,-1,1,-1],[1,1,1,1],2]
expected: 0
Starter codePython
import math
def spectral_coherence_lite(x, y, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor