
Complex waves in function

Erik_Kran
Hi,
A strange issue with complex waves.
The following codes :
Function test() wave r12, m wave ref1=r12 wave ref2=m ref1=ref2 End
does not work. => "While executing a wave assignment, the following error occurred: Complex wave used in real expression."
but the following :
Macro titi() r12=m end
is fine.
These are two simple examples, my original program includes several calculations with complex waves.
Thanks a lot for your help.
r12 and m are two complex waves
February 7, 2025 at 09:33 am - Permalink
In your function use wave /c r12 when you reference them. The flag /c identifies them as complex.
Andy
February 7, 2025 at 11:50 am - Permalink
Thanks. Unfortunately, does not work.
Eric
February 7, 2025 at 12:00 pm - Permalink
All the WAVE statements need to be WAVE/C if the waves referred are complex. If something doesn't work, please post the code that doesn't work, and details about the waves.
The difference between the macro and the function is that since a macro is interpreted at run-time, it will look up the actual waves involved and use the correct branch of code to deal with the actual wave type. A function must compile either complex or real code at a time when the wave may not even exist yet. So you have to tell the compiler what kind of code to compile. There is a run-time check to make sure the actual wave matches what you told the compiler. That avoids crashes.
February 7, 2025 at 12:06 pm - Permalink
Below are the two codes : one with the function, the other one as a macro. The macro works. Note that in the function code, the declared waves are real.
February 7, 2025 at 12:12 pm - Permalink
Finally I fixed it, but not sure I understand why it works now.
the last version :
February 7, 2025 at 12:51 pm - Permalink
Your fix of adding /C to the Make commands is the correct one: The compiler now knows that the created waves will be complex at runtime, and compiles code that expects and operates on complex waves.
February 7, 2025 at 03:24 pm - Permalink
OK. So it means that the the command:
should write:
duplicate /O /C m delta
?
February 8, 2025 at 01:48 am - Permalink
Only if you want to use the automatic wave assignment created by the duplicate operation. The duplicate operation alone will work just fine without this flag. You seem to be confused by these flags, but it is very simple actually. You have to let Igor (the compiler) know what type of data you are working with (if you want to work with the data via wave assignments etc.), so that type conflicts are avoided. It would surely be useless to feed a numerical wave some text like 'Hello', no? So if Igor does not know you are using complex waves, it will treat the thing as the default, which are just numbers and will lead to the error. You can read more about this here:
DisplayHelpTopic "WAVE Reference Types"
I recommend to avoid macros at all costs.
February 9, 2025 at 05:35 pm - Permalink
Hi Erik,
I'm new at WaveMetrics and also found parts of this confusing. I went through the first code you submitted and made the minimal number of changes required to get it working. The changes are all commented. Hopefully this will help you understand the nuances of the Duplicate command a bit better. At least, it helped me :)
Thanks,
Kris
February 10, 2025 at 02:19 pm - Permalink
Hi Kris
Thanks a lot for your comments. Really helpful :
Here is the explanation and explains why it worked when I removed the duplicate and replaced them with Make /C.
Cheers,
Eric
February 10, 2025 at 10:31 pm - Permalink
That's exactly right, Eric
February 11, 2025 at 09:56 am - Permalink