Julia Method Error converting Complex{Float64}

2019-08-10 14:28发布

I'm novice to Julia and I have the following code with this error:

MethodError(convert,(Complex{Float64},[-1.0 - 1.0im])).

I would like to know the source of the error and how to optimize this piece of code for speed.

This is my code:

function OfdmSym()
N = 64
n = 1000
symbol = convert(Array{Complex{Float64},2},ones(n,64))    # I need Array{Complex{Float64},2}
data   = convert(Array{Complex{Float64},2},ones(1,48))    # I need Array{Complex{Float64},2}

const unused = convert(Array{Complex{Float64},2},zeros(1,12))
const pilot  = convert(Array{Complex{Float64},2},ones(1,4))
const s      = convert(Array{Complex{Float64},2},[-1-im -1+im 1-im 1+im])# QPSK Complex Data

for i=1:n                  # generate 1000 symbols
    for j = 1:48           # generate 48 complex data symbols whose basis is s
          r = rand(1:4,1)  # 1, 2, 3, or 4
          data[j] = s[r]
    end
    symbol[i,:]=[data[1,1:10] pilot[1] data[1,11:20] pilot[2] data[1,21:30] pilot[3] data[1,31:40] pilot[4] data[1,41:48] unused]
end
end

As it's the first day programming in Julia, I tried very hard to reveal the source of the error without success. I also tried to optimize and initialize arrays as I could but when I time the code I realize that it is far from optimal. I appreciate your help.

1条回答
倾城 Initia
2楼-- · 2019-08-10 14:54

Try this much simpler code

function OfdmSym()
    N = 64
    n = 1000
    symbol = ones(Complex{Float64}, n, 64)
    data   = ones(Complex{Float64}, 1, 48)
    unused = zeros(Complex{Float64}, 1, 12)
    pilot  = ones(Complex{Float64}, 1, 4)
    s      = [-1-im  -1+im  1-im  1+im]

    for i=1:n                  # generate 1000 symbols
        for j = 1:48           # generate 48 complex data symbols whose basis is s
              r = rand(1:4)    # 1, 2, 3, or 4
              data[j] = s[r]
        end
        symbol[i,:]=[data[1,1:10] pilot[1] data[1,11:20] pilot[2] data[1,21:30] pilot[3] data[1,31:40] pilot[4] data[1,41:48] unused]
    end
end

OfdmSym()

I wouldn't worry too much about optimizing things until you have got it working correctly. The way you have it set up now seems like it'd be kinda inefficient due to all the slicing of arrays - it'd be better to try to build symbol directly.

查看更多
登录 后发表回答