Skip to content

freq

Frequency-domain analysis of EMG signals.

get_spectrum

get_spectrum(data: ndarray, fs: Optional[int] = 2048) -> Tuple[np.ndarray, np.ndarray]

Compute the single-sided amplitude spectrum of the input data.

Parameters:

Name Type Description Default
data ndarray

Input data with shape (samples, channels).

required
fs int

Sampling frequency in Hz. Default is 2048.

2048

Returns:

Name Type Description
spectrum ndarray

Amplitude spectrum of the input data with shape (samples//2, channels), in the same units as the data, where the first dimension corresponds to the positive frequencies.

xf ndarray

Frequency axis in Hz, with shape (samples//2,).

Source code in emg_tools/freq.py
 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
def get_spectrum(
    data: np.ndarray,
    fs: Optional[int] = 2048,
) -> Tuple[np.ndarray, np.ndarray]:

    """Compute the single-sided amplitude spectrum of the input data.

    Args:
        data (np.ndarray): Input data with shape (samples, channels).
        fs (int, optional): Sampling frequency in Hz. Default is 2048.

    Returns:
        spectrum (np.ndarray): Amplitude spectrum of the input data with shape
            (samples//2, channels), in the same units as the data, where the
            first dimension corresponds to the positive frequencies.
        xf (np.ndarray): Frequency axis in Hz, with shape (samples//2,).
    """

    # Initialise variables
    samples, chs = data.shape

    # Compute amplitude spectrum
    yf = fft.fft(data, axis=0)
    xf = fft.fftfreq(samples, 1/fs)[:samples//2]
    spectrum = 2/samples * np.abs(yf[0:samples//2])
    spectrum[0] /= 2 # DC component is not mirrored

    return spectrum, xf

get_psd

get_psd(data: ndarray, fs: Optional[int] = 2048, *, welch: Optional[bool] = False, nperseg: Optional[int] = None) -> Tuple[np.ndarray, np.ndarray]

Compute the one-sided Power Spectral Density (PSD) of the input data.

By default the periodogram is used. Welch's method can be requested instead.

Parameters:

Name Type Description Default
data ndarray

Input data with shape (samples, channels).

required
fs int

Sampling frequency in Hz. Default is 2048.

2048
welch bool

If True, the PSD is estimated with Welch's method. Default is False (periodogram).

False
nperseg int

Length of each segment in samples, only used if welch is True. Default is None, which uses scipy's default (256).

None

Returns:

Name Type Description
psd ndarray

One-sided PSD of the input data with shape (freqs, channels), in units of data^2/Hz.

xf ndarray

Frequency axis in Hz, with shape (freqs,).

Source code in emg_tools/freq.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def get_psd(
    data: np.ndarray,
    fs: Optional[int] = 2048,
    *,
    welch: Optional[bool] = False,
    nperseg: Optional[int] = None,
) -> Tuple[np.ndarray, np.ndarray]:

    """Compute the one-sided Power Spectral Density (PSD) of the input data.

    By default the periodogram is used. Welch's method can be requested instead.

    Args:
        data (np.ndarray): Input data with shape (samples, channels).
        fs (int, optional): Sampling frequency in Hz. Default is 2048.
        welch (bool, optional): If True, the PSD is estimated with Welch's
            method. Default is False (periodogram).
        nperseg (int, optional): Length of each segment in samples, only used
            if welch is True. Default is None, which uses scipy's default (256).

    Returns:
        psd (np.ndarray): One-sided PSD of the input data with shape
            (freqs, channels), in units of data^2/Hz.
        xf (np.ndarray): Frequency axis in Hz, with shape (freqs,).
    """

    # Compute PSD
    if welch:
        xf, psd = signal.welch(data, fs=fs, nperseg=nperseg, axis=0)
    else:
        xf, psd = signal.periodogram(data, fs=fs, axis=0)

    return psd, xf