我在 python 中尝试一段代码给出错误的结果。我只是想确认 np.randn 函数与 Matlab randn plzz helppp 相同
S=[ 3.+3.j 1.-3.j -3.+1.j 1.+3.j 1.-1.j]
SNR=arange(6,23,1)
BER=zeros(len(SNR))
Es=10
for ii in arange(0,len(SNR)):
variance=Es*(10**(-SNR[ii]/10))
std_dev=cmath.sqrt(variance/2)
noise=(np.random.randn(len(S))+cmath.sqrt(-1)*np.random.randn(len(S))) *std_dev
S_noisy=S+noise
print(noise)
matlab代码
SNR=6:22;
display(SNR)
display(length(SNR))
BER=zeros(1,length(SNR));
display(BER)
display(length(BER))
Es=10;
for ii=1:length(SNR)
variance=Es*10^(-SNR(ii)/10);
std_dev=sqrt(variance/2);
noise=(randn(1,length(S))+sqrt(-1)*randn(1,length(S)))*std_dev;
S_noisy=S+noise;
end
display(noise)
结果应该是
[0.1150 + 0.1889i -0.0756 + 0.2055i 0.1862 + 0.0094i 0.1174 - 0.2288i
0.4456 - 0.0659i]