这是用脉冲编码调制方法将给定的正弦信号转换成数字编码的程序。你知道吗
import matplotlib.pyplot as plt
import numpy
Amplitude=input("Enter the Amplitude: ")
Time=input("Enter the Time: ")
Frequency=1.0/Time
x=numpy.linspace(-Amplitude,Amplitude,num=Time*10)
series=Amplitude*numpy.sin(2*numpy.pi/Frequency*x)
plt.plot(series)
plt.show()
Quantization=input("Enter the Quantization bit: ")
power=pow(2,Quantization-1)
Size=2*Amplitude/power
z=[]
for t in range(0,Time*10):
if(series[t]>=0):
p=0
n=Size
for k in range(0,power-1):
if((series[t]>=p)and(series[t]<=n)):
z.append(k+power)
break
p=n
n=Size+n
else:
p=-1
n=-Size
for k in range(0,power-1):
if((series[t]<=p)and(series[t]>=n)):
z.append(power-k-1)
break
p=n
n=n-Size
plt.plot(z)
plt.show()
print z
signal=[]
rem=0
for j in range(0,Time*10):
i=1
summ =0
while 1:
if(z[j]>0):
rem=int(z[j])%2
summ=summ+(i*rem)
z[j]=z[j]/2
i=i*10
else:
signal.append(summ)
break
print signal
下面是问题
[root@vijay ~]# python PCM.py
Enter the Amplitude: 20
Enter the Time: 10
Enter the Quantization bit: 3
[4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4]
Traceback (most recent call last):
File "PCM.py", line 42, in <module>
if(z[j]>0):
IndexError: list index out of range
在注释了最后一个for循环之后,显示的信号输出中还有另一个问题。好像你在运行下面的程序
import matplotlib.pyplot as plt
import numpy
Amplitude=input("Enter the Amplitude: ")
Time=input("Enter the Time: ")
Frequency=1.0/Time
x=numpy.linspace(-Amplitude,Amplitude,num=Time*10)
series=Amplitude*numpy.sin(2*numpy.pi/Frequency*x)
plt.plot(series)
plt.show()
Quantization=input("Enter the Quantization bit: ")
power=pow(2,Quantization-1)
Size=2*Amplitude/power
z=[]
for t in range(0,Time*10):
if(series[t]>=0):
p=0
n=Size
for k in range(0,power-1):
if((series[t]>=p)and(series[t]<=n)):
z.append(k+power)
break
p=n
n=Size+n
else:
p=-1
n=-Size
for k in range(0,power-1):
if((series[t]<=p)and(series[t]>=n)):
z.append(power-k-1)
break
p=n
n=n-Size
plt.plot(z)
plt.show()
print z
我们有两个情节。1个绘图图是正确的,但第二个在时间=60(x轴=60)时有故障,根据概念和编程,那里的期望值应该是3.0,但不是。你知道吗
请帮助,我是python新手,无法调试问题。谢谢
目前没有回答
相关问题 更多 >
编程相关推荐