• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

feihoo87 / waveforms / 16159025029

09 Jul 2025 02:41AM UTC coverage: 53.366% (-0.03%) from 53.393%
16159025029

push

github

feihoo87
Update version to 2.1.1 following recent enhancements and maintain backward compatibility.

1 of 1 new or added line in 1 file covered. (100.0%)

207 existing lines in 2 files now uncovered.

1316 of 2466 relevant lines covered (53.37%)

6.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/waveforms/utils.py
1
from itertools import repeat
×
2
from types import MappingProxyType
×
3
from typing import Optional, Sequence, cast
×
4

5
import numpy as np
×
6
import scipy.sparse as sp
×
7

8

9
def freeze(x):
×
10
    """Freeze a mutable object.
11
    """
12
    if isinstance(x, (int, float, complex, str, bytes, type(None))):
×
13
        pass
×
14
    elif isinstance(x, (list, tuple)):
×
15
        return tuple([freeze(y) for y in x])
×
16
    elif isinstance(x, dict):
×
17
        return MappingProxyType({k: freeze(v) for k, v in x.items()})
×
18
    elif isinstance(x, set):
×
19
        return frozenset([freeze(y) for y in x])
×
20
    elif isinstance(x, (np.ndarray, np.matrix)):
×
21
        x.flags.writeable = False
×
22
    elif isinstance(x, sp.spmatrix):
×
23
        cast(np.ndarray, getattr(x, 'data')).flags.writeable = False
×
24
        if getattr(x, 'format') in {'csr', 'csc', 'bsr'}:
×
25
            cast(np.ndarray, getattr(x, 'indices')).flags.writeable = False
×
26
            cast(np.ndarray, getattr(x, 'indptr')).flags.writeable = False
×
27
        elif getattr(x, 'format') == 'coo':
×
28
            cast(np.ndarray, getattr(x, 'row')).flags.writeable = False
×
29
            cast(np.ndarray, getattr(x, 'col')).flags.writeable = False
×
30
    elif isinstance(x, bytearray):
×
31
        x = bytes(x)
×
32
    return x
×
33

34

35
def getFTMatrix(fList: Sequence[float],
×
36
                numOfPoints: int,
37
                phaseList: Optional[Sequence[float]] = None,
38
                weight: Optional[np.ndarray] = None,
39
                sampleRate: float = 1e9) -> np.ndarray:
40
    """
41
    get a matrix for Fourier transform
42

43
    Args:
44
        fList (Sequence[float]): list of frequencies
45
        numOfPoints (int): size of signal frame
46
        phaseList (Optional[Sequence[float]], optional): list of phase. Defaults to None.
47
        weight (Optional[np.ndarray], optional): weight or list of weight. Defaults to None.
48
        sampleRate (float, optional): sample rate of signal. Defaults to 1e9.
49

50
    Returns:
51
        numpy.ndarray: exp matrix
52
    
53
    >>> shots, numOfPoints, sampleRate = 100, 1000, 1e9
54
    >>> f1, f2 = -12.7e6, 32.8e6
55
    >>> signal = np.random.randn(shots, numOfPoints)
56
    >>> e = getFTMatrix([f1, f2], numOfPoints, sampleRate=sampleRate)
57
    >>> ret = signal @ e
58
    >>> ret.shape
59
    (100, 2)
60
    >>> t = np.arange(numOfPoints) / sampleRate
61
    >>> signal = 0.8 * np.sin(2 * np.pi * f1 * t) + 0.2 * np.cos(2 * np.pi * f2 * t)
62
    >>> signal @ e
63
    array([-0.00766509-0.79518987j,  0.19531432+0.00207068j])
64
    >>> spec = 2 * np.fft.fft(signal) / numOfPoints
65
    >>> freq = np.fft.fftfreq(numOfPoints)
66
    >>> e = getFTMatrix(freq, numOfPoints, sampleRate=1)
67
    >>> np.allclose(spec, signal @ e)
68
    True
69
    """
70
    e = []
×
71
    t = np.linspace(0, numOfPoints / sampleRate, numOfPoints, endpoint=False)
×
72
    if weight is None or len(weight) == 0:
×
73
        weight = np.full(numOfPoints, 2 / numOfPoints)
×
74
    if phaseList is None or len(phaseList) == 0:
×
75
        phase_list = np.zeros_like(fList)
×
76
    else:
77
        phase_list = phaseList
×
UNCOV
78
    if weight.ndim == 1:
×
79
        weight_list = repeat(weight)
×
80
    else:
81
        weight_list = weight
×
82
    for f, phase, weight in zip(fList, phase_list, weight_list):
×
UNCOV
83
        e.append(weight * np.exp(-1j * (2 * np.pi * f * t + phase)))
×
UNCOV
84
    return np.asarray(e).T
×
85

86

UNCOV
87
def shift(signal: np.ndarray, delay: float, dt: float) -> np.ndarray:
×
88
    """
89
    delay a signal
90

91
    Args:
92
        signal (np.ndarray): input signal
93
        delay (float): delayed time
94
        dt (float): time step of signal samples
95

96
    Returns:
97
        np.ndarray: delayed signal
98
    """
UNCOV
99
    points = int(delay // dt)
×
100
    delta = delay / dt - points
×
101

102
    if delta > 0:
×
UNCOV
103
        ker = np.array([0, 1 - delta, delta])
×
104
        signal = np.convolve(signal, ker, mode='same')
×
105

UNCOV
106
    if points == 0:
×
107
        return signal
×
108

109
    ret = np.zeros_like(signal)
×
UNCOV
110
    if points < 0:
×
111
        ret[:points] = signal[-points:]
×
112
    else:
UNCOV
113
        ret[points:] = signal[:-points]
×
UNCOV
114
    return ret
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc