Duplicate wave with arithmetic options

Greetings Forum,

I'm kind of new to programming with igor. I want to duplicate "wave k" to create "wave j" such that each point in "wave j" is subtracted from previous point in "wave k", something like this (image): 

It doesn't have to be a duplicate, I think one can define function to make this, any ideas how?

Thanks in advance.

example of what i expect wave k to be like
make/o/n=20 Original=p^2duplicate/o Original,Subtracted
•Subtracted[] = Original[p]-Original[p-1]

This can be easily done by the wave assignment in Igor Pro. You can see a demonstration by executing the above.

Here, p is the bin number of the waves, aligning the bin 1 of LHS to bin 1 of RHS, so and so forth. An assignment of Subtracted[] = Original[p] will make a bin-to-bin copy from Original to Subtracted. Now with the above, I am assigning them while doing the subtraction.

Duplicate/O k, j
j[0] = 0
j[1,] = k[p] - k[p-1]

This, of course, uses literal wave names in the commands. If you are using this inside a function, it is likely that the names should be variable. That adds some complexity.

For information about what all those square brackets and p's are doing: DisplayHelpTopic "Waveform Arithmetic and Assignment"

I would discourage wave names that are one character as they aren't very descriptive. And i, j, k are frequently used in programming as the index variables in loops. 

In reply to by johnweeks

johnweeks wrote:

@Sandbo- be careful with point 0!

 

Thanks for pointing that out lol
At first I also wondered what will happen when it goes below zero, but somehow it worked. Maybe under some situation it will complain.

In Igor 8 I think the "index out of bounds" error is always turned on. In some previous version, it was only on if you added

#pragma rtErrors=3

at the top of your procedure file. In old versions, the point numbers were simply clipped to the range [0, npoints), leading to some strange and hard to find bugs.

Before reading the comments I was trying to figure out by myself, and came up with something like this:

Make /o /N=15 Kay =p +0
Duplicate /o Kay, Jay
InsertPoints 1,1, Jay
Make /o /N=15 KaySub
Kaysub = Kay - Jay

but the issue was the first element in J has to be 0 .....

and then with the help of my friend, wrote a do while loop as such:

Function KayJay()
variable Jay1,KayP1,KayM1
variable i=0
wave kay1
make/o/n=(numpnts(kay1)) Jay2
Wave Jay
Do
    If (i==0)
        Jay2[i]=0
    
    else
        Jay2[i]=Kay1[i]-Kay1[i-1]
    endif
Jay1=Jay2[i]
Kayp1=Kay1[i]
Kaym1=Kay1[i-1]
i+=1
While (i < numpnts(kay1))
 
End

 

Thanks a lot for your comments,

Just as food for thought: You can get rid of the if statement with initializing Jay2 with zero and write things more compact with a for loop.

Function KayJay()
    variable Jay1,KayP1,KayM1
    variable i=0
    Wave Kay1
    make/o/n=(numpnts(kay1)) Jay2=0
    Wave Jay
    For (i=1; i < numpnts(kay1); i+=1)
        Jay2[i]=Kay1[i]-Kay1[i-1]
        Jay1=Jay2[i]
        Kayp1=Kay1[i]
        Kaym1=Kay1[i-1]
    Endfor
End

Btw, your variables Jay1, KayP1, KayM1 and the wave 'Jay' is not used further in your function. Do they have any purpose?

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More