我已成功将MATLAB源代码转换为Python - 但绘图输出不匹配。我已经在Python和Octave中双重验证了每个变量bot的值 - 两者都是相同的。
八度图输出:
Python Matplotlib输出:
Octave代码:
clear
N = 10^3; % number of symbols
am = 2*(rand(1,N)>0.5)-1 + j*(2*(rand(1,N)>0.5)-1); % generating random binary sequence
fs = 10; % sampling frequency in Hz
% defining the sinc filter
sincNum = sin(pi*[-fs:1/fs:fs]); % numerator of the sinc function
sincDen = (pi*[-fs:1/fs:fs]); % denominator of the sinc function
sincDenZero = find(abs(sincDen) < 10^-10);
sincOp = sincNum./sincDen;
sincOp(sincDenZero) = 1; % sin(pix/(pix) =1 for x =0
% raised cosine filter
alpha = 0.5;
cosNum = cos(alpha*pi*[-fs:1/fs:fs]);
cosDen = (1-(2*alpha*[-fs:1/fs:fs]).^2);
cosDenZero = find(abs(cosDen)<10^-10);
cosOp = cosNum./cosDen;
cosOp(cosDenZero) = pi/4;
gt_alpha5 = sincOp.*cosOp;
alpha = 1;
cosNum = cos(alpha*pi*[-fs:1/fs:fs]);
cosDen = (1-(2*alpha*[-fs:1/fs:fs]).^2);
cosDenZero = find(abs(cosDen)<10^-10);
cosOp = cosNum./cosDen;
cosOp(cosDenZero) = pi/4;
gt_alpha1 = sincOp.*cosOp;
% upsampling the transmit sequence
amUpSampled = [am;zeros(fs-1,length(am))];
amU = amUpSampled(:).';
% filtered sequence
st_alpha5 = conv(amU,gt_alpha5);
st_alpha1 = conv(amU,gt_alpha1);
% taking only the first 10000 samples
st_alpha5 = st_alpha5([1:10000]);
st_alpha1 = st_alpha1([1:10000]);
st_alpha5_reshape = reshape(st_alpha5,fs*2,N*fs/20).';
st_alpha1_reshape = reshape(st_alpha1,fs*2,N*fs/20).';
close all
figure;
st_alpha5_reshape
plot([0:1/fs:1.99],real(st_alpha5_reshape).','b');
title('eye diagram with alpha=0.5');
xlabel('time')
ylabel('amplitude')
axis([0 2 -1.5 1.5])
grid on
figure;
plot([0:1/fs:1.99],real(st_alpha1_reshape).','b');
title('eye diagram with alpha=1')
xlabel('time')
ylabel('amplitude')
axis([0 2 -1.5 1.5 ])
grid on
Python代码:
j = np.complex(0,1)
N = 10**3
#% number of symbols
am = 2.*(np.random.rand(1., N) > 0.5)-1.+np.dot(j, 2.*(np.random.rand(1., N) > 0.5)-1.)
#% generating random binary sequence
fs = 10.
#% sampling frequency in Hz
#% defining the sinc filter
sincNum = np.sin(np.dot(np.pi, np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs))))))
#% numerator of the sinc function
sincDen = np.dot(np.pi, np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs)))))
#% denominator of the sinc function
sincDenZero = np.where(abs(sincDen) < 10**-10);
sincOp=sincNum/sincDen
sincOp[int(sincDenZero[0])-1] = 1.
#% raised cosine filter
alpha = 0.5
cosNum = np.cos(np.dot(np.dot(alpha, np.pi), np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs))))))
cosDen = 1.-np.dot(2.*alpha, np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs)))))**2.
cosDenZero = np.where(abs(cosDen)<10**-10);
cosOp = cosNum/cosDen
cosOp[int(cosDenZero[0][0])-1] = np.pi/4.
cosOp[int(cosDenZero[0][1])-1] = np.pi/4.
gt_alpha5 = sincOp*cosOp
alpha = 1.
cosNum = np.cos(np.dot(np.dot(alpha, np.pi), np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs))))))
cosDen = 1.-np.dot(2.*alpha, np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs)))))**2.
cosDenZero = np.where(abs(cosDen)<10**-10);
cosOp = cosNum/cosDen
cosOp[int(cosDenZero[0][0])-1] = np.pi/4.
cosOp[int(cosDenZero[0][1])-1] = np.pi/4.
gt_alpha1 = sincOp*cosOp
#% upsampling the transmit sequence
#amUpSampled = np.array(np.vstack((np.hstack((am)), np.hstack((np.zeros((fs-1.), len(am)))))))
amUpSampled = np.append(am,np.zeros((fs-1,am.size)))
amU = amUpSampled.flatten(0)
#% filtered sequence
st_alpha5 = np.convolve(amU, gt_alpha5)
st_alpha1 = np.convolve(amU, gt_alpha1)
#% taking only the first 10000 samples
st_alpha5 = st_alpha5[0:10000:1]
st_alpha1 = st_alpha1[0:10000:1]
#st_alpha5_reshape = np.reshape(st_alpha5, (fs*2.), (np.dot(N, fs)/20.)).T
st_alpha5_reshape = np.reshape(st_alpha5, (-1,500)).T
#st_alpha1_reshape = np.reshape(st_alpha1, (fs*2.), (np.dot(N, fs)/20.)).T
st_alpha1_reshape = np.reshape(st_alpha1, (-1,500)).T
plt.close('all')
plt.figure(1)
plt.plot(np.array(np.hstack((np.arange(.1, (1.99)+(1./fs), 1./fs)))), np.real(st_alpha5_reshape).T, 'b')
plt.title('eye diagram with alpha=0.5')
plt.xlabel('time')
plt.ylabel('amplitude')
plt.axis(np.array(np.hstack((0., 2., -1.5, 1.5))))
plt.grid('on')
plt.figure(2)
plt.plot(np.array(np.hstack((np.arange(.1, (1.99)+(1./fs), 1./fs)))), np.real(st_alpha1_reshape).T, 'b')
plt.title('eye diagram with alpha=1')
plt.xlabel('time')
plt.ylabel('amplitude')
plt.axis(np.array(np.hstack((0., 2., -1.5, 1.5))))
plt.grid('on')
plt.show()
请告诉我问题在哪里,修复程序仅在Python代码中出现?
答案 0 :(得分:3)
一些事情。尽管您说“我已经在Python和Octave中对双变量bot的值进行了双重验证 - 但它们都是相同的。” - 事实并非如此。
首先,当您从MATLAB移植到numpy时需要从索引中减去1时,是次,但是您的代码没有任何这些。
所以你到处都有:
sincOp[int(sincDenZero[0])-1] = 1.
将其更改为
sincOp[int(sincDenZero[0])] = 1
简单来说,原因是因为np.where
的输出已被0索引,因此当你减去1时,你就过度补偿了。
接下来,你在整个地方使用np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs))))
,所以我们只需创建一个变量并构建一次:
fsrange = np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs))))
但这可能只是:
fsrange = np.arange(-fs, fs+(1./fs), 1./fs)
同样,这条巨大的界限:
cosNum = np.cos(np.dot(np.dot(alpha, np.pi), np.array(np.hstack((np.arange(-fs, (fs)+(1./fs), 1./fs))))))
可以是:
cosNum = np.cos(alpha * np.pi * fsrange)
这一行:
amUpSampled = np.append(am,np.zeros((fs-1,am.size)))
应该是(所以你不要修改am
,并正确指定args到zeros
):
amUpSampled = np.vstack([ am, np.zeros([(fs-1.), am.size]) ])
您在此处指定了错误的拼合顺序:
amU = amUpSampled.flatten(0)
它应该使用FORTRAN命令(MATLAB使用的)来展平:
amU = amUpSampled.flatten('F')
重塑形状时,需要指定FORTRAN顺序:
st_alpha5_reshape = np.reshape(st_alpha5, [(fs*2.), (N * fs / 20.)], 'F').T
st_alpha1_reshape = np.reshape(st_alpha1, [(fs*2.), (N * fs / 20.)], 'F').T
所以你纠正的python代码应如下所示:
import numpy as np
import matplotlib.pyplot as plt
j = np.complex(0,1)
N = 10**3
#% number of symbols
am = 2.*(np.random.rand(1., N) > 0.5)-1.+np.dot(j, 2.*(np.random.rand(1., N) > 0.5)-1.)
#% generating random binary sequence
fs = 10.
fsrange = np.arange(-fs, fs+(1./fs), 1./fs)
#% sampling frequency in Hz
#% defining the sinc filter
sincNum = np.sin(np.dot(np.pi, fsrange))
#% numerator of the sinc function
sincDen = np.dot(np.pi, fsrange)
#% denominator of the sinc function
sincDenZero = np.where(np.abs(sincDen) < 10**-10);
sincOp=sincNum/sincDen
sincOp[int(sincDenZero[0])] = 1.
#% raised cosine filter
alpha = 0.5
cosNum = np.cos(alpha * np.pi * fsrange)
cosDen = 1.-np.dot(2.*alpha, fsrange)**2.
cosDenZero = np.nonzero(abs(cosDen)<10**-10);
cosOp = cosNum/cosDen
cosOp[int(cosDenZero[0][0])] = np.pi/4.
cosOp[int(cosDenZero[0][1])] = np.pi/4.
gt_alpha5 = sincOp*cosOp
alpha = 1.
cosNum = np.cos(alpha * np.pi * fsrange)
cosDen = 1.-np.dot(2.*alpha, fsrange)**2.
cosDenZero = np.where(abs(cosDen)<10**-10);
cosOp = cosNum/cosDen
cosOp[int(cosDenZero[0][0])] = np.pi/4.
cosOp[int(cosDenZero[0][1])] = np.pi/4.
gt_alpha1 = sincOp*cosOp
#% upsampling the transmit sequence
amUpSampled = np.vstack([ am, np.zeros([(fs-1.), am.size]) ])
amU = amUpSampled.flatten('F')
#% filtered sequence
st_alpha5 = np.convolve(amU, gt_alpha5)
st_alpha1 = np.convolve(amU, gt_alpha1)
#% taking only the first 10000 samples
st_alpha5 = st_alpha5[0:10000]
st_alpha1 = st_alpha1[0:10000]
st_alpha5_reshape = np.reshape(st_alpha5, [(fs*2.), (N * fs / 20.)], 'F').T
st_alpha1_reshape = np.reshape(st_alpha1, [(fs*2.), (N * fs / 20.)], 'F').T
plt.close('all')
X = np.arange(0,1.99, 1.0/fs)
plt.figure(1)
plt.plot(X, np.real(st_alpha5_reshape).T, 'b')
plt.title('eye diagram with alpha=0.5')
plt.xlabel('time')
plt.ylabel('amplitude')
plt.axis(np.array(np.hstack((0., 2., -1.5, 1.5))))
plt.grid('on')
plt.figure(2)
plt.plot(X, np.real(st_alpha1_reshape).T, 'b')
plt.title('eye diagram with alpha=1')
plt.xlabel('time')
plt.ylabel('amplitude')
plt.axis(np.array(np.hstack((0., 2., -1.5, 1.5))))
plt.grid('on')
plt.show()
产生您期望的数字。
旁注:
如果你在MATLAB中有一个数组/矩阵(假设它叫做varname
),你可以用save varname
(在MATLAB中)将它保存到.mat文件中。
然后可以使用以下命令在python中加载该数组/矩阵:
import scipy.io
mat = scipy.io.loadmat("<path of .mat file>")
varname = mat[varname]
您可以在整个MATLAB工作区中执行此操作,只需save
- 在python mat
中仍然只是一个由变量名称键入的字典,因此您可以访问各个工作区变量就像你上面一样。
您可以使用它来逐步验证numpy产生的内容与您期望它产生的内容,并弄清楚您做错了什么。