Complex Wave Division

I've been able to (I believe) do the analysis I wanted, but I feel I'm doing it in a clunky way. But it's the only way I've gotten it to work.

function wavedivider2()

    wave wave1, wave2
   
    make/o/c/n=(numpnts(wave1)) lp
    make/o/c/n=(numpnts(wave1)) sp
    make/o/c/n=(numpnts(wave1)) tr
    make/o/c/n=(numpnts(wave1)) cc
   
    lp = r2polar(wave1)
    sp = r2polar(wave2)
   
    tr =   cmplx(real(lp)/real(sp) * cos(imag(lp)-imag(sp)),real(lp)/real(sp) *  sin(imag(lp)-imag(sp)))
    cc = 1 + tr

end


wave1 and wave2 are complex waves already in the directory when I run this. I make 4 complex waves for future use. With lp and sp, I convert the rectangular coordinates to polar so that lp should be a complex wave with (magnitude, phase) as the (real, imag) parts.

I'm confused here why I can't set a wave equal to real(lp). Actually what I want to do is divide lp by sp. Ideally, I would just say tr = lp / sp. What is the best way to do this? I know it's likely a little more complicated since the phase is in the imaginary part of the result of r2polar, so it should be exponentiated to recover the complex number. I've defined the real and imaginary parts of tr explicitly, but this introduces the clunky necessity of including the magnitude of the ratio of the real parts into both the real and imaginary part of tr.

The last step is just to check that I can perform arithmetic on the tr wave.
I've been able to simply the operation in rectangular coordinates (wave1 and wave2 are in rectangular coordinates).

function wavedivider2()

    wave wave1, wave2
    make/o/c/n=(numpnts(wave1)) lp
    lp = wave1 / wave2
   
end


So this seems like the right way...Was there a better way to do it in polar coordinates?

I still couldn't do lp = real(wave1). Why is that?

I see that I created lp as a complex wave, so only giving it one wave (real(wave1)) isn't enough...It needs data to input for its real and imaginary parts.

Taking the /c flag out of the make command allows me to write lp = real(wave1).

If wave1 and wave2 are complex, then you need the WAVE statement to have a /C flag to tell Igor's compiler to generate complex arithmetic:

wave/C wave1, wave2

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
You could also save yourself the trouble by using MatrixOP which would handle waves correctly regardless of their real/complex persuasion.