在Python 2.7,matplotlib

时间:2016-07-29 20:51:02

标签: python python-2.7 numpy matplotlib

这是我的代码:

import numpy, math, time
from matplotlib import pyplot

def parse_FFT_hex(string):

    invert_arrays = True
    num_bits = 12
    numpy.array = []
    overflow = False
    ch_0_re = []; ch_0_im = []; ch_1_re = []; ch_1_im = []
    for i in range(0,len(string), 32):
        # if any number is greater than 2045 return 'False'
        ch_0_re_num = int(string[i+30] + string[i+31] + string[i+28], 16)
        if ch_0_re_num > (2**(num_bits-1)): ch_0_re_num = ch_0_re_num - (2**num_bits)
        ch_0_im_num = int(string[i+29] + string[i+26] + string[i+27], 16)
        if ch_0_im_num > (2**(num_bits-1)): ch_0_im_num = ch_0_im_num - (2**num_bits)
        ch_1_re_num = int(string[i+22] + string[i+23] + string[i+20], 16)
        if ch_1_re_num > (2**(num_bits-1)): ch_1_re_num = ch_1_re_num - (2**num_bits)
        ch_1_im_num = int(string[i+21] + string[i+18] + string[i+19], 16)
        if ch_1_im_num > (2**(num_bits-1)): ch_1_im_num = ch_1_im_num - (2**num_bits)
        if abs(ch_0_re_num) > 2045 or abs(ch_0_im_num) > 2045 or abs(ch_1_re_num) > 2045 or abs(ch_1_im_num) > 2045: overflow = True
        ch_0_re.append(ch_0_re_num); ch_0_im.append(ch_0_im_num); ch_1_re.append(ch_1_re_num); ch_1_im.append(ch_1_im_num)

        ch_0_re_num = int(string[i+14] + string[i+15] + string[i+12], 16)
        if ch_0_re_num > (2**(num_bits-1)): ch_0_re_num = ch_0_re_num - (2**num_bits)
        ch_0_im_num = int(string[i+13] + string[i+10] + string[i+11], 16)
        if ch_0_im_num > (2**(num_bits-1)): ch_0_im_num = ch_0_im_num - (2**num_bits)
        ch_1_re_num = int(string[i+6] + string[i+7] + string[i+4], 16)
        if ch_1_re_num > (2**(num_bits-1)): ch_1_re_num = ch_1_re_num - (2**num_bits)
        ch_1_im_num = int(string[i+5] + string[i+2] + string[i+3], 16)
        if ch_1_im_num > (2**(num_bits-1)): ch_1_im_num = ch_1_im_num - (2**num_bits)
        if abs(ch_0_re_num) > 2045 or abs(ch_0_im_num) > 2045 or abs(ch_1_re_num) > 2045 or abs(ch_1_im_num) > 2045: overflow = True
        ch_0_re.append(ch_0_re_num); ch_0_im.append(ch_0_im_num); ch_1_re.append(ch_1_re_num); ch_1_im.append(ch_1_im_num)

    if invert_arrays:
        temp = ch_0_re
        ch_0_re = ch_0_im
        ch_0_im = temp
        temp = ch_1_re
        ch_1_re = ch_1_im
        ch_1_im = temp

    ch_0 = 0.0; ch_1 = 0.0
    for i in range(len(ch_0_re)):
        ch_0 += (ch_0_re[i]**2) + (ch_0_im[i]**2)
        ch_1 += (ch_1_re[i]**2) + (ch_1_im[i]**2)

    ch_0_pow = 10 * math.log10((ch_0/len(ch_0_re))/(2**22))
    ch_1_pow = 10 * math.log10((ch_1/len(ch_1_re))/(2**22))
    return [ch_0_pow, ch_1_pow, overflow]

powers = parse_FFT_hex(hex_string)
print powers
pyplot.figure()
pyplot.plot(powers[0], powers[1])

运行它给了我:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\salzda\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)
  File "C:\Users\salzda\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/salzda/Documents/Python Scripts/untitled1.py", line 82, in <module>
    pyplot.figure()
  File "C:\Users\salzda\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\pyplot.py", line 527, in figure
    **kwargs)
  File "C:\Users\salzda\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 45, in new_figure_manager
    thisFig = FigureClass(*args, **kwargs)
  File "C:\Users\salzda\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\figure.py", line 325, in __init__
    self.dpi = dpi
  File "C:\Users\salzda\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\figure.py", line 410, in _set_dpi
    self.dpi_scale_trans.clear().scale(dpi, dpi)
  File "C:\Users\salzda\AppData\Local\Continuum\Anaconda2\lib\site-packages\matplotlib\transforms.py", line 1965, in scale
    np.float_)
TypeError: 'list' object is not callable

即使列表打印得很好并且给了我想要的值,但由于某种原因它无法做任何事情。

1 个答案:

答案 0 :(得分:1)

你的问题:

numpy.array = []
不要这样做。您正在将numpy.array转变为列表。删除该行,您的代码可能会正常工作。