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

feihoo87 / waveforms / 12880607338

15 Jan 2025 08:24PM UTC coverage: 54.553% (-1.4%) from 55.908%
12880607338

push

github

feihoo87
v2.0.1

1 of 30 new or added lines in 2 files covered. (3.33%)

653 of 1197 relevant lines covered (54.55%)

4.91 hits per line

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

0.0
/waveforms/utils.py
NEW
1
from itertools import repeat
×
NEW
2
from typing import Optional, Sequence
×
3

NEW
4
import numpy as np
×
5

6

NEW
7
def getFTMatrix(fList: Sequence[float],
×
8
                numOfPoints: int,
9
                phaseList: Optional[Sequence[float]] = None,
10
                weight: Optional[np.ndarray] = None,
11
                sampleRate: float = 1e9) -> np.ndarray:
12
    """
13
    get a matrix for Fourier transform
14

15
    Args:
16
        fList (Sequence[float]): list of frequencies
17
        numOfPoints (int): size of signal frame
18
        phaseList (Optional[Sequence[float]], optional): list of phase. Defaults to None.
19
        weight (Optional[np.ndarray], optional): weight or list of weight. Defaults to None.
20
        sampleRate (float, optional): sample rate of signal. Defaults to 1e9.
21

22
    Returns:
23
        numpy.ndarray: exp matrix
24
    
25
    >>> shots, numOfPoints, sampleRate = 100, 1000, 1e9
26
    >>> f1, f2 = -12.7e6, 32.8e6
27
    >>> signal = np.random.randn(shots, numOfPoints)
28
    >>> e = getFTMatrix([f1, f2], numOfPoints, sampleRate=sampleRate)
29
    >>> ret = signal @ e
30
    >>> ret.shape
31
    (100, 2)
32
    >>> t = np.arange(numOfPoints) / sampleRate
33
    >>> signal = 0.8 * np.sin(2 * np.pi * f1 * t) + 0.2 * np.cos(2 * np.pi * f2 * t)
34
    >>> signal @ e
35
    array([-0.00766509-0.79518987j,  0.19531432+0.00207068j])
36
    >>> spec = 2 * np.fft.fft(signal) / numOfPoints
37
    >>> freq = np.fft.fftfreq(numOfPoints)
38
    >>> e = getFTMatrix(freq, numOfPoints, sampleRate=1)
39
    >>> np.allclose(spec, signal @ e)
40
    True
41
    """
NEW
42
    e = []
×
NEW
43
    t = np.linspace(0, numOfPoints / sampleRate, numOfPoints, endpoint=False)
×
NEW
44
    if weight is None or len(weight) == 0:
×
NEW
45
        weight = np.full(numOfPoints, 2 / numOfPoints)
×
NEW
46
    if phaseList is None or len(phaseList) == 0:
×
NEW
47
        phaseList = np.zeros_like(fList)
×
NEW
48
    if weight.ndim == 1:
×
NEW
49
        weightList = repeat(weight)
×
50
    else:
NEW
51
        weightList = weight
×
NEW
52
    for f, phase, weight in zip(fList, phaseList, weightList):
×
NEW
53
        e.append(weight * np.exp(-1j * (2 * np.pi * f * t + phase)))
×
NEW
54
    return np.asarray(e).T
×
55

56

NEW
57
def shift(signal: np.ndarray, delay: float, dt: float) -> np.ndarray:
×
58
    """
59
    delay a signal
60

61
    Args:
62
        signal (np.ndarray): input signal
63
        delay (float): delayed time
64
        dt (float): time step of signal samples
65

66
    Returns:
67
        np.ndarray: delayed signal
68
    """
NEW
69
    points = int(delay // dt)
×
NEW
70
    delta = delay / dt - points
×
71

NEW
72
    if delta > 0:
×
NEW
73
        ker = np.array([0, 1 - delta, delta])
×
NEW
74
        signal = np.convolve(signal, ker, mode='same')
×
75

NEW
76
    if points == 0:
×
NEW
77
        return signal
×
78

NEW
79
    ret = np.zeros_like(signal)
×
NEW
80
    if points < 0:
×
NEW
81
        ret[:points] = signal[-points:]
×
82
    else:
NEW
83
        ret[points:] = signal[:-points]
×
NEW
84
    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

© 2025 Coveralls, Inc