Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About getting the received signal val_Y in test_score.py #6

Closed
STARainZ opened this issue Jan 21, 2024 · 5 comments
Closed

About getting the received signal val_Y in test_score.py #6

STARainZ opened this issue Jan 21, 2024 · 5 comments
Assignees

Comments

@STARainZ
Copy link

Hello,

Thanks for the excellent work! I have a question about the way you generate val_Y in the following code:

val_Y = torch.matmul(val_P, val_H)
val_Y = val_Y + \
np.sqrt(local_noise) * torch.randn_like(val_Y)

I'm wondering whether it should be np.sqrt(local_noise/2) in L122 instead since local_noise is the variance of complex-valued noise. Should the standard deviation of the real and imaginary parts be $\sigma_{\text{pilot}}/\sqrt{2}$, as shown in test_ml.py:

val_Y = np.matmul(val_P, val_H)
val_Y = val_Y + \
np.sqrt(local_noise) / np.sqrt(2.) * \
(np.random.normal(size=val_Y.shape) + \
1j * np.random.normal(size=val_Y.shape))

Thank you.

@mariusarvinte mariusarvinte self-assigned this Jan 23, 2024
@mariusarvinte
Copy link
Collaborator

mariusarvinte commented Jan 25, 2024

Thank you for noticing this very subtle aspect - while it seems incorrect at a first look, there is an undocumented behavior of the PyTorch torch.randn and torch.randn_like functions that makes this correct.

Whenever these functions are used with complex data types they will sample from the unit power complex Gaussian distribution with real and imaginary powers 1/2 and standard deviations 1/sqrt(2) each.

This can be verified by running the following basic snippet:

import torch
import numpy as np

desired_power = 4.2

y_cplx_native = np.sqrt(desired_power) * torch.randn(1000, 1000, dtype=torch.complex64)
print('Complex signal power using native complex: ', torch.var(y_cplx_native))
print('Real part signal power using native complex: ', torch.var(torch.real(y_cplx_native)))
print('Imag part signal power using native complex: ', torch.var(torch.imag(y_cplx_native)))

y_cplx_from_reals = np.sqrt(desired_power) * 1/np.sqrt(2) * (torch.randn(1000, 1000, dtype=torch.float32) + 1j * torch.randn(1000, 1000, dtype=torch.float32))
print('Complex signal power using manual complex: ', torch.var(y_cplx_from_reals ))
print('Real part signal power using manual complex: ', torch.var(torch.real(y_cplx_from_reals)))
print('Imag part signal power using manual complex: ', torch.var(torch.imag(y_cplx_from_reals)))

Should give correct results in both cases, such as:

Complex signal power using native complex:  tensor(4.2006)
Real part signal power using native complex:  tensor(2.1028)
Imag part signal power using native complex:  tensor(2.0978)

Complex signal power using manual complex:  tensor(4.2078)
Real part signal power using manual complex:  tensor(2.1055)
Imag part signal power using manual complex:  tensor(2.1023)

This can also be verified for torch.randn_like - whenever a torch.complex variable is used as input, it will correctly sample from the unit power complex Gaussian. We used the native complex sampling in test_score.py and the "manual" construction in test_ml.py, so the definitions should be consistent.

It took us a while to realize this ourselves through trial and error. Let us know if you have any more questions.

@STARainZ
Copy link
Author

Thanks for the reply! I have checked and found that torch.randn_like samples from a complex Gaussian distribution with variance 1 when the input is complex-valued. Therefore, the code is correct.

@mariusarvinte mariusarvinte pinned this issue Jan 25, 2024
@jtamir
Copy link

jtamir commented Jan 25, 2024 via email

@mariusarvinte
Copy link
Collaborator

Thanks, added.

Also raised the issue on pytorch: pytorch/pytorch#118269.
A like there wouldn't hurt for visibility.

@123nyr
Copy link

123nyr commented Dec 2, 2024

您好,非常感谢你的工作!我想知道您的论文中的图里面的Approximate MMSE在SNR下的NMSE的结果要怎么复现,训练和测试都在CDL-C里面。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants