File size: 1,832 Bytes
17b7ba4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
from typing import List, Optional, Sequence

def dominant_frequency(x: np.ndarray, fs: float = 1.0) -> float:
    x = np.asarray(x, dtype=float)
    if len(x) < 2:
        return 0.0
    x = x - np.mean(x)
    spectrum = np.abs(np.fft.rfft(x))
    freqs = np.fft.rfftfreq(len(x), d=1.0 / fs if fs > 0 else 1.0)
    if spectrum.size <= 1:
        return 0.0
    idx = int(np.argmax(spectrum[1:]) + 1) if spectrum.size > 1 else 0
    return float(freqs[idx]) if idx < len(freqs) else 0.0

def ensure_period_list(

    periods: Optional[Sequence[float]],

    fallback: Optional[float],

    series_length: int,

) -> List[int]:
    cleaned: List[int] = []
    if periods:
        for val in periods:
            try:
                p_int = int(round(float(val)))
            except (TypeError, ValueError):
                continue
            if p_int >= 2:
                cleaned.append(p_int)
    if not cleaned and fallback:
        try:
            p = int(round(float(fallback)))
            if p >= 2:
                cleaned.append(p)
        except (TypeError, ValueError):
            pass
    if not cleaned:
        approx = max(2, series_length // 10)
        cleaned.append(approx)
    # deduplicate while preserving order
    seen = set()
    unique: List[int] = []
    for p in cleaned:
        if p not in seen:
            seen.add(p)
            unique.append(p)
    return unique

def aggregate_modes(modes: np.ndarray, indices: Optional[List[int]]) -> np.ndarray:
    if indices is None or len(indices) == 0:
        return np.zeros(modes.shape[1], dtype=float)
    valid = [idx for idx in indices if 0 <= idx < modes.shape[0]]
    if not valid:
        return np.zeros(modes.shape[1], dtype=float)
    return np.sum(modes[valid, :], axis=0)