You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
386 lines
12 KiB
386 lines
12 KiB
2 years ago
|
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved
|
||
3 years ago
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
import math
|
||
|
from typing import List
|
||
|
from typing import Tuple
|
||
|
from typing import Union
|
||
|
|
||
|
import paddle
|
||
|
from paddle import Tensor
|
||
|
|
||
|
|
||
2 years ago
|
class WindowFunctionRegister(object):
|
||
|
def __init__(self):
|
||
|
self._functions_dict = dict()
|
||
3 years ago
|
|
||
2 years ago
|
def register(self):
|
||
|
def add_subfunction(func):
|
||
|
name = func.__name__
|
||
|
self._functions_dict[name] = func
|
||
|
return func
|
||
|
|
||
|
return add_subfunction
|
||
|
|
||
|
def get(self, name):
|
||
|
return self._functions_dict[name]
|
||
|
|
||
|
|
||
|
window_function_register = WindowFunctionRegister()
|
||
|
|
||
|
|
||
|
@window_function_register.register()
|
||
2 years ago
|
def _cat(x: List[Tensor], data_type: str) -> Tensor:
|
||
|
l = [paddle.to_tensor(_, data_type) for _ in x]
|
||
3 years ago
|
return paddle.concat(l)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
3 years ago
|
def _acosh(x: Union[Tensor, float]) -> Tensor:
|
||
|
if isinstance(x, float):
|
||
|
return math.log(x + math.sqrt(x**2 - 1))
|
||
|
return paddle.log(x + paddle.sqrt(paddle.square(x) - 1))
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
3 years ago
|
def _extend(M: int, sym: bool) -> bool:
|
||
2 years ago
|
"""Extend window by 1 sample if needed for DFT-even symmetry."""
|
||
3 years ago
|
if not sym:
|
||
|
return M + 1, True
|
||
|
else:
|
||
|
return M, False
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
3 years ago
|
def _len_guards(M: int) -> bool:
|
||
2 years ago
|
"""Handle small or incorrect window lengths."""
|
||
3 years ago
|
if int(M) != M or M < 0:
|
||
|
raise ValueError('Window length M must be a non-negative integer')
|
||
|
|
||
|
return M <= 1
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
3 years ago
|
def _truncate(w: Tensor, needed: bool) -> Tensor:
|
||
2 years ago
|
"""Truncate window by 1 sample if needed for DFT-even symmetry."""
|
||
3 years ago
|
if needed:
|
||
|
return w[:-1]
|
||
|
else:
|
||
|
return w
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _general_gaussian(
|
||
|
M: int, p, sig, sym: bool = True, dtype: str = 'float64'
|
||
|
) -> Tensor:
|
||
3 years ago
|
"""Compute a window with a generalized Gaussian shape.
|
||
|
This function is consistent with scipy.signal.windows.general_gaussian().
|
||
|
"""
|
||
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
|
|
||
|
n = paddle.arange(0, M, dtype=dtype) - (M - 1.0) / 2.0
|
||
2 years ago
|
w = paddle.exp(-0.5 * paddle.abs(n / sig) ** (2 * p))
|
||
3 years ago
|
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _general_cosine(
|
||
|
M: int, a: float, sym: bool = True, dtype: str = 'float64'
|
||
|
) -> Tensor:
|
||
3 years ago
|
"""Compute a generic weighted sum of cosine terms window.
|
||
|
This function is consistent with scipy.signal.windows.general_cosine().
|
||
|
"""
|
||
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
|
fac = paddle.linspace(-math.pi, math.pi, M, dtype=dtype)
|
||
2 years ago
|
w = paddle.zeros((M,), dtype=dtype)
|
||
3 years ago
|
for k in range(len(a)):
|
||
|
w += a[k] * paddle.cos(k * fac)
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _general_hamming(
|
||
|
M: int, alpha: float, sym: bool = True, dtype: str = 'float64'
|
||
|
) -> Tensor:
|
||
3 years ago
|
"""Compute a generalized Hamming window.
|
||
|
This function is consistent with scipy.signal.windows.general_hamming()
|
||
|
"""
|
||
2 years ago
|
return _general_cosine(M, [alpha, 1.0 - alpha], sym, dtype=dtype)
|
||
3 years ago
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _taylor(
|
||
|
M: int, nbar=4, sll=30, norm=True, sym: bool = True, dtype: str = 'float64'
|
||
|
) -> Tensor:
|
||
3 years ago
|
"""Compute a Taylor window.
|
||
|
The Taylor window taper function approximates the Dolph-Chebyshev window's
|
||
|
constant sidelobe level for a parameterized number of near-in sidelobes.
|
||
|
"""
|
||
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
|
# Original text uses a negative sidelobe level parameter and then negates
|
||
|
# it in the calculation of B. To keep consistent with other methods we
|
||
|
# assume the sidelobe level parameter to be positive.
|
||
2 years ago
|
B = 10 ** (sll / 20)
|
||
3 years ago
|
A = _acosh(B) / math.pi
|
||
2 years ago
|
s2 = nbar**2 / (A**2 + (nbar - 0.5) ** 2)
|
||
3 years ago
|
ma = paddle.arange(1, nbar, dtype=dtype)
|
||
|
|
||
2 years ago
|
Fm = paddle.empty((nbar - 1,), dtype=dtype)
|
||
3 years ago
|
signs = paddle.empty_like(ma)
|
||
|
signs[::2] = 1
|
||
|
signs[1::2] = -1
|
||
|
m2 = ma * ma
|
||
|
for mi in range(len(ma)):
|
||
2 years ago
|
numer = signs[mi] * paddle.prod(
|
||
|
1 - m2[mi] / s2 / (A**2 + (ma - 0.5) ** 2)
|
||
|
)
|
||
3 years ago
|
if mi == 0:
|
||
2 years ago
|
denom = 2 * paddle.prod(1 - m2[mi] / m2[mi + 1 :])
|
||
3 years ago
|
elif mi == len(ma) - 1:
|
||
|
denom = 2 * paddle.prod(1 - m2[mi] / m2[:mi])
|
||
|
else:
|
||
2 years ago
|
denom = (
|
||
|
2
|
||
|
* paddle.prod(1 - m2[mi] / m2[:mi])
|
||
|
* paddle.prod(1 - m2[mi] / m2[mi + 1 :])
|
||
|
)
|
||
3 years ago
|
|
||
|
Fm[mi] = numer / denom
|
||
|
|
||
|
def W(n):
|
||
|
return 1 + 2 * paddle.matmul(
|
||
|
Fm.unsqueeze(0),
|
||
2 years ago
|
paddle.cos(2 * math.pi * ma.unsqueeze(1) * (n - M / 2.0 + 0.5) / M),
|
||
|
)
|
||
3 years ago
|
|
||
|
w = W(paddle.arange(0, M, dtype=dtype))
|
||
|
|
||
|
# normalize (Note that this is not described in the original text [1])
|
||
|
if norm:
|
||
|
scale = 1.0 / W((M - 1) / 2)
|
||
|
w *= scale
|
||
|
w = w.squeeze()
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _hamming(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
|
||
3 years ago
|
"""Compute a Hamming window.
|
||
|
The Hamming window is a taper formed by using a raised cosine with
|
||
|
non-zero endpoints, optimized to minimize the nearest side lobe.
|
||
|
"""
|
||
2 years ago
|
return _general_hamming(M, 0.54, sym, dtype=dtype)
|
||
3 years ago
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _hann(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
|
||
3 years ago
|
"""Compute a Hann window.
|
||
|
The Hann window is a taper formed by using a raised cosine or sine-squared
|
||
|
with ends that touch zero.
|
||
|
"""
|
||
2 years ago
|
return _general_hamming(M, 0.5, sym, dtype=dtype)
|
||
3 years ago
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _tukey(
|
||
|
M: int, alpha=0.5, sym: bool = True, dtype: str = 'float64'
|
||
|
) -> Tensor:
|
||
3 years ago
|
"""Compute a Tukey window.
|
||
|
The Tukey window is also known as a tapered cosine window.
|
||
|
"""
|
||
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
|
||
|
if alpha <= 0:
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
elif alpha >= 1.0:
|
||
|
return hann(M, sym=sym)
|
||
|
|
||
|
M, needs_trunc = _extend(M, sym)
|
||
|
|
||
|
n = paddle.arange(0, M, dtype=dtype)
|
||
|
width = int(alpha * (M - 1) / 2.0)
|
||
2 years ago
|
n1 = n[0 : width + 1]
|
||
|
n2 = n[width + 1 : M - width - 1]
|
||
|
n3 = n[M - width - 1 :]
|
||
3 years ago
|
|
||
|
w1 = 0.5 * (1 + paddle.cos(math.pi * (-1 + 2.0 * n1 / alpha / (M - 1))))
|
||
|
w2 = paddle.ones(n2.shape, dtype=dtype)
|
||
2 years ago
|
w3 = 0.5 * (
|
||
|
1
|
||
|
+ paddle.cos(math.pi * (-2.0 / alpha + 1 + 2.0 * n3 / alpha / (M - 1)))
|
||
|
)
|
||
3 years ago
|
w = paddle.concat([w1, w2, w3])
|
||
|
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _gaussian(
|
||
|
M: int, std: float, sym: bool = True, dtype: str = 'float64'
|
||
|
) -> Tensor:
|
||
3 years ago
|
"""Compute a Gaussian window.
|
||
|
The Gaussian widows has a Gaussian shape defined by the standard deviation(std).
|
||
|
"""
|
||
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
|
|
||
|
n = paddle.arange(0, M, dtype=dtype) - (M - 1.0) / 2.0
|
||
|
sig2 = 2 * std * std
|
||
2 years ago
|
w = paddle.exp(-(n**2) / sig2)
|
||
3 years ago
|
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _exponential(
|
||
|
M: int, center=None, tau=1.0, sym: bool = True, dtype: str = 'float64'
|
||
|
) -> Tensor:
|
||
|
"""Compute an exponential (or Poisson) window."""
|
||
3 years ago
|
if sym and center is not None:
|
||
|
raise ValueError("If sym==True, center must be None.")
|
||
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
|
|
||
|
if center is None:
|
||
|
center = (M - 1) / 2
|
||
|
|
||
|
n = paddle.arange(0, M, dtype=dtype)
|
||
|
w = paddle.exp(-paddle.abs(n - center) / tau)
|
||
|
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _triang(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
|
||
|
"""Compute a triangular window."""
|
||
3 years ago
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
|
|
||
|
n = paddle.arange(1, (M + 1) // 2 + 1, dtype=dtype)
|
||
|
if M % 2 == 0:
|
||
|
w = (2 * n - 1.0) / M
|
||
|
w = paddle.concat([w, w[::-1]])
|
||
|
else:
|
||
|
w = 2 * n / (M + 1.0)
|
||
|
w = paddle.concat([w, w[-2::-1]])
|
||
|
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _bohman(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
|
||
3 years ago
|
"""Compute a Bohman window.
|
||
|
The Bohman window is the autocorrelation of a cosine window.
|
||
|
"""
|
||
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
|
|
||
|
fac = paddle.abs(paddle.linspace(-1, 1, M, dtype=dtype)[1:-1])
|
||
|
w = (1 - fac) * paddle.cos(math.pi * fac) + 1.0 / math.pi * paddle.sin(
|
||
2 years ago
|
math.pi * fac
|
||
|
)
|
||
3 years ago
|
w = _cat([0, w, 0], dtype)
|
||
|
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _blackman(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
|
||
3 years ago
|
"""Compute a Blackman window.
|
||
|
The Blackman window is a taper formed by using the first three terms of
|
||
|
a summation of cosines. It was designed to have close to the minimal
|
||
|
leakage possible. It is close to optimal, only slightly worse than a
|
||
|
Kaiser window.
|
||
|
"""
|
||
2 years ago
|
return _general_cosine(M, [0.42, 0.50, 0.08], sym, dtype=dtype)
|
||
3 years ago
|
|
||
|
|
||
2 years ago
|
@window_function_register.register()
|
||
|
def _cosine(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
|
||
|
"""Compute a window with a simple cosine shape."""
|
||
3 years ago
|
if _len_guards(M):
|
||
2 years ago
|
return paddle.ones((M,), dtype=dtype)
|
||
3 years ago
|
M, needs_trunc = _extend(M, sym)
|
||
2 years ago
|
w = paddle.sin(math.pi / M * (paddle.arange(0, M, dtype=dtype) + 0.5))
|
||
3 years ago
|
|
||
|
return _truncate(w, needs_trunc)
|
||
|
|
||
|
|
||
2 years ago
|
def get_window(
|
||
|
window: Union[str, Tuple[str, float]],
|
||
|
win_length: int,
|
||
|
fftbins: bool = True,
|
||
|
dtype: str = 'float64',
|
||
|
) -> Tensor:
|
||
3 years ago
|
"""Return a window of a given length and type.
|
||
2 years ago
|
|
||
|
Args:
|
||
2 years ago
|
window (Union[str, Tuple[str, float]]): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'gaussian', 'general_gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'.
|
||
2 years ago
|
win_length (int): Number of samples.
|
||
|
fftbins (bool, optional): If True, create a "periodic" window. Otherwise, create a "symmetric" window, for use in filter design. Defaults to True.
|
||
|
dtype (str, optional): The data type of the return window. Defaults to 'float64'.
|
||
|
|
||
3 years ago
|
Returns:
|
||
2 years ago
|
Tensor: The window represented as a tensor.
|
||
2 years ago
|
|
||
|
Examples:
|
||
|
.. code-block:: python
|
||
|
|
||
|
import paddle
|
||
|
|
||
|
n_fft = 512
|
||
|
cosine_window = paddle.audio.functional.get_window('cosine', n_fft)
|
||
|
|
||
|
std = 7
|
||
|
gaussian_window = paddle.audio.functional.get_window(('gaussian',std), n_fft)
|
||
3 years ago
|
"""
|
||
|
sym = not fftbins
|
||
|
|
||
|
args = ()
|
||
|
if isinstance(window, tuple):
|
||
|
winstr = window[0]
|
||
|
if len(window) > 1:
|
||
|
args = window[1:]
|
||
|
elif isinstance(window, str):
|
||
|
if window in ['gaussian', 'exponential']:
|
||
2 years ago
|
raise ValueError(
|
||
|
"The '" + window + "' window needs one or "
|
||
|
"more parameters -- pass a tuple."
|
||
|
)
|
||
3 years ago
|
else:
|
||
|
winstr = window
|
||
|
else:
|
||
2 years ago
|
raise ValueError(
|
||
|
"%s as window type is not supported." % str(type(window))
|
||
|
)
|
||
3 years ago
|
|
||
|
try:
|
||
2 years ago
|
winfunc = window_function_register.get('_' + winstr)
|
||
3 years ago
|
except KeyError as e:
|
||
|
raise ValueError("Unknown window type.") from e
|
||
|
|
||
2 years ago
|
params = (win_length,) + args
|
||
3 years ago
|
kwargs = {'sym': sym}
|
||
|
return winfunc(*params, dtype=dtype, **kwargs)
|