1

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 Answer 1

3

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.

4
  • Very clean and intuitive. I now got the source of the error above. It was rand(1:4,1) which returns an array of Int32 rather than an element of type Int32 as with rand(1:4). Your code works fine but I need advice on optimization. Can you help me in this? Many thanks.
    – AboAmmar
    Sep 28, 2014 at 15:42
  • You should accept this answer as it fixes the problem in this question, and ask a new question about the performance of the code - you'll get better answers that way. Sep 28, 2014 at 15:43
  • I accepted the answer, thanks. But any further help in optimization?
    – AboAmmar
    Sep 28, 2014 at 15:45
  • I've answered that in your new question. Sep 28, 2014 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.