espnet2.enh.layers.complex_utils.new_complex_like
Less than 1 minute
espnet2.enh.layers.complex_utils.new_complex_like
espnet2.enh.layers.complex_utils.new_complex_like(ref: Tensor | ComplexTensor, real_imag: Tuple[Tensor, Tensor])
Create a new complex tensor based on the reference tensor.
This function generates a new complex tensor that matches the type of the reference tensor ref using the provided real and imaginary parts. It supports both PyTorch’s native complex tensors and the ComplexTensor from the torch_complex library.
- Parameters:
- ref – A reference tensor which can be either a PyTorch Tensor or a ComplexTensor. This determines the type of the output tensor.
- real_imag – A tuple containing two tensors, the first for the real part and the second for the imaginary part.
- Returns: A complex tensor of the same type as ref, constructed from real_imag.
- Raises:
- ValueError – If the PyTorch version is less than 1.9 or if ref
- is not a supported tensor type. –
Examples
>>> real = torch.tensor([1.0, 2.0])
>>> imag = torch.tensor([3.0, 4.0])
>>> ref_tensor = torch.tensor([0.0, 0.0], dtype=torch.complex64)
>>> new_tensor = new_complex_like(ref_tensor, (real, imag))
>>> print(new_tensor)
tensor([1.+3.j, 2.+4.j], dtype=torch.complex64)
>>> ref_complex = ComplexTensor(real, imag)
>>> new_complex = new_complex_like(ref_complex, (real, imag))
>>> print(new_complex)
ComplexTensor(real=tensor([1., 2.]), imag=tensor([3., 4.]))