#!/usr/bin/python
# -*- coding : utf-8 -*-
from math import *
import numpy as np
from scipy.sparse import spdiags,csr_matrix
from pylab import *
import matplotlib.pyplot as plt
u_basic=input("Signal elements (row complex vector, each element last tb sec) = ? ") ;
m_basic = u_basic.shape[1]; #gives the no. of columns
fcode = input("Allow frequency coding (Yes=1, no=0) = ? ");
if (fcode == 1):
f_basic = input("Frequency coding in units of 1/tb (row vector of same length) = ? ");
F=input("Maximal Doppler shift for ambiguity plot [in units of 1/Mtb] (e.g., 1)= ? ");
K=input("Number of Doppler grid points for calculation (e.g., 100) = ? ");
F=float(F);
df=F/K/m_basic;
T=input("Maximal Delay for ambiguity plot [in units of Mtb] (e.g., 1)= ? ");
N=input("Number of delay grid points on each side (e.g. 100) = ? ");
sr=input("Over sampling ratio (>=1) (e.g. 10)= ? ");
r=math.ceil(sr*(N+1)/T/m_basic);
if (r==1):
dt=1;
m=m_basic;
uamp=np.abs(u_basic);
phas=uamp*0;
phas=angle(u_basic);
if (fcode==1):
phas=phas+2*numpy.pi*cumsum(f_basic);
uexp=exp(j*phas);
u=uamp*uexp;
else:
dt=1/r;
ud=diag(u_basic);
ao=ones([r,m_basic]);
m=m_basic*r;
u_basic=reshape(ao*ud,(1,m),order='F') #reshape(ao*ud,1,m);
uamp=np.abs(u_basic);
phas=angle(u_basic);
u=u_basic;
if (fcode == 1):
ff=diag(f_basic);
phas=2*numpy.pi*dt*cumsum(reshape(ao*ff,1,m))+phas;
uexp=exp(j*phas);
u=uamp*uexp;
'''t=arange(0,r*m_basic-1)/r;
a1=([0])
a2=([r*m_basic-1])
t1=concatenate((a1,t));tscale1=concatenate((a2,r*m_basic));
tscale1=[0 arange(0:r*m_basic-1) r*m_basic-1]/r;'''
t1=range(0,int(r*m_basic)-1);
t=[i/r for i in t1]
#t.append((r*m_basic)-1);
t1=t;
t1.insert (0,0);
tscale1=np.array(t1)
tscale1=np.reshape(tscale1,(1,tscale1.shape[0]))
dphas1=np.append(nan,diff(phas))
#dphas1=[nan,diff (phas)]
#dphas1=[nan,diff (phas,n=1,axis=0)]
myint=r/2/np.pi;
dphas=[j*myint for j in dphas1]
#dphas=[nan,diff (phas,n=1,axis=0)]*r/2/pi;
x=np.transpose(tscale1)
y=np.transpose(np.abs(uamp))
#a=np.append(0,uamp)
#b=np.append(a,0)
subplot(311)
plt.plot(x,y,linewidth=1.5)
#plot(tscale1 [0,abs(uamp),0],'linewidth',1.5)
ylabel(' Amplitude ')
axis([-inf,inf,0,1.2*amax(np.abs(uamp))])
#plt.show()
#phas1=np.append(0,phas)
subplot(312)
plot(tscale1, phas,linewidth=1.5)
axis([-inf,inf,-inf,inf])
ylabel(' Phase [rad] ')
myint1=ceil(amax(t))
dphas2=[k*myint1 for k in dphas]
subplot(313)
plot(t,dphas2,linewidth=1.5)
axis([-inf,inf,-inf,inf])
xlabel(' \itt / t_b ')
ylabel(' \itf * Mt_b ')
#plt.show()
dtau=ceil(T*m)*dt/N;
tau=around(arange(0,N,1)*dtau/dt)*dt;
f=arange(0,K,1)*df;
f=np.reshape(f,(1,f.shape[0]))
f=np.append(-fliplr(f),f);
mat1=spdiags(u,0,m+ceil(T*m),m);
u_padded=[zeros(1,ceil(T*m)),u,zeros(1,ceil(T*m))];
cidx=arange(1,m+ceil(T*m)+1);
ridx=around(tau/dt);
#ridx=around(tau/dt).transpose();
index = cidx[int(np.ones((N+1,1))),:]+ridx[:,int(np.ones(1,m+np.ceil(T*m)))]
#index = cidx(np.ones(N+1,1),:) + ridx(:,np.ones(1,m+ceil(T*m)));
mat2 = csr_matrix(u_padded(index));
uu_pos=mat2*mat1;
我在以下行中收到以下错误: - > 124 index = cidx [int(np.ones((N + 1,1))),:] + ridx [:,int(np.ones(1,m + np.ceil(T * m)))] TypeError:只能将length-1数组转换为Python标量 输入为:np.mat([1,2,3]),0,1,1,1,1,1,10,10
答案 0 :(得分:1)
int(np.ones((N+1,1)))
- int()
是一个python函数,可将其输入转换为1个整数。但你给它一个numpy数组,一个2d。
您可能不需要int
。 ones
需要dtype=int
参数。
np.arange(10)[np.ones((3,3),dtype=int)]
我同意这个评论。大多数代码与此错误无关。
cidx[int(np.ones((N+1,1))),:]+ridx[:,int(np.ones(1,m+np.ceil(T*m)))]
您可能甚至不需要该行中的ones
。这看起来像一个MATLAB表达式,可以扩展cidx
和ridx
,以便它们可以相加。使用ones
代替zeros
也表明了这一点。 numpy
进行广播,所以我怀疑表达式可以简化为:
cidx[np.newaxis,:] + ridx[:, np.newaxis]
(这假定cidx
和ridx
是1d数组 - 我没有挖掘早期的代码来验证这一点。)