File: fallback.py

package info (click to toggle)
numpy-rms 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 132 kB
  • sloc: python: 138; ansic: 95; makefile: 4
file content (24 lines) | stat: -rw-r--r-- 728 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
from numpy.typing import NDArray


def calculate_rms(a: NDArray):
    """Given a numpy array, return its RMS power level."""
    return np.sqrt(np.mean(np.square(a), axis=-1))


def rms_numpy(a: NDArray, window_size: int) -> NDArray:
    if 0 in a.shape:
        raise ValueError("Cannot input empty array")

    output_shape = a.shape[:-1] + (a.shape[-1] // window_size,)
    output_array = np.zeros(shape=output_shape, dtype=a.dtype)

    end_index = output_shape[-1] * window_size

    output_i = 0
    for offset in range(0, end_index, window_size):
        rms = calculate_rms(a[..., offset : offset + window_size])
        output_array[..., output_i] = rms
        output_i += 1
    return output_array