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

feihoo87 / waveforms / 15353267452

15 May 2025 03:34AM UTC coverage: 46.214% (-1.4%) from 47.595%
15353267452

push

github

feihoo87
add freeze

0 of 24 new or added lines in 1 file covered. (0.0%)

18 existing lines in 2 files now uncovered.

653 of 1413 relevant lines covered (46.21%)

5.55 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
×
NEW
2
from types import MappingProxyType
×
UNCOV
3
from typing import Optional, Sequence
×
4

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

8

NEW
9
def freeze(x):
×
10
    """Freeze a mutable object.
11
    """
NEW
12
    if isinstance(x, (int, float, complex, str, bytes, type(None))):
×
NEW
13
        pass
×
NEW
14
    elif isinstance(x, (list, tuple)):
×
NEW
15
        return tuple([freeze(y) for y in x])
×
NEW
16
    elif isinstance(x, dict):
×
NEW
17
        return MappingProxyType({k: freeze(v) for k, v in x.items()})
×
NEW
18
    elif isinstance(x, set):
×
NEW
19
        return frozenset([freeze(y) for y in x])
×
NEW
20
    elif isinstance(x, (np.ndarray, np.matrix)):
×
NEW
21
        x.flags.writeable = False
×
NEW
22
    elif isinstance(x, sp.spmatrix):
×
NEW
23
        x.data.flags.writeable = False
×
NEW
24
        if x.format in {'csr', 'csc', 'bsr'}:
×
NEW
25
            x.indices.flags.writeable = False
×
NEW
26
            x.indptr.flags.writeable = False
×
NEW
27
        elif x.format == 'coo':
×
NEW
28
            x.row.flags.writeable = False
×
NEW
29
            x.col.flags.writeable = False
×
NEW
30
    elif isinstance(x, bytearray):
×
NEW
31
        x = bytes(x)
×
NEW
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
        phaseList = np.zeros_like(fList)
×
76
    if weight.ndim == 1:
×
77
        weightList = repeat(weight)
×
78
    else:
79
        weightList = weight
×
80
    for f, phase, weight in zip(fList, phaseList, weightList):
×
81
        e.append(weight * np.exp(-1j * (2 * np.pi * f * t + phase)))
×
82
    return np.asarray(e).T
×
83

84

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

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

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

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

104
    if points == 0:
×
105
        return signal
×
106

107
    ret = np.zeros_like(signal)
×
108
    if points < 0:
×
109
        ret[:points] = signal[-points:]
×
110
    else:
111
        ret[points:] = signal[:-points]
×
112
    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