Help to make a function that operate on several waves

Good morning,

 

I am still very new with Igor and I

 am trying to generate a function that would allow me to operate on several waves in a table  (wave0 to wave100 or more) without having to type each one of these operation.

 

The operation I am trying to do is newwave = (wave0 - mean(wave0 x1, x20))/ mean(wave0 x1,x20)

 

I would like the function to generate a tab that would have all the new waves.

 

Would appreciate any help and explanation you can provide.

 

Thank you

Hi Phil,

A couple things for your request. The first is to create a function to do the normalization and that is straight forward, but one thing to keep in mind is data organization which includes both the wave name of the new wave and also if there is a different data folder where you would like/need to segregate the new waves.  For example if you have a different data folder for the normalized wave, then you can reuse the input wave name.  This may be useful it keeping track of the resulting waves.  In this example the new wave stays in the same directory as your current working folder which maybe different than the source input wave.

function Normalize(input)
    wave input
   
    Duplicate input, $(nameOfWave(input)+"norm") // newname is based on input wave
    Wave newWave = $(nameOfWave(input)+"norm") //create wave reference so it can be modified
    Variable initialWave =Mean(input,0,pnt2x(input,20))//calculate it only once
    newwave =(input -initialWave)/initialWave
End

Now to run this function over a group of waves in a table takes a couple of steps and does depend on how things are named and placed in the first function. First figure out how waves are in the table and I am assuming that they are are all 1D waves. Then go through the table info column by column and extract the wave name and run it through the first function.  I also need to keep track of the waves added using the same naming convention.  This could be made more robust by having the first function return a string with the new wave name, but I wanted to keep things simple here.  Lastly I create a blank table and then add the newly normalized waves to that table.

Function Run_Over_Table()
   
    string tableWaves, specificColumn
    String NewTableList=""
    //first get info about the table as a whole
    TableWaves = tableinfo("",-2) // assumes top table
    Variable NumOfWaves, index
    NumOfWaves =numberByKey("COLUMNS", Tablewaves)-1//point column is included in count
    //Now we can iterate over the waves by using the column id number to get the wave
    For(index=0;index<NumOfWaves;Index+=1)
        specificColumn = tableinfo("",index)//Substitute column number to get a specific one
        Wave input = $stringByKey("Wave", specificColumn)// get the full wave location and create wave reference
        Normalize(input) // run the previous normalization content
        NewTableList +=(stringByKey("Wave", specificColumn)+"_norm;")//Build list of new waves
    endfor
    edit
    For(index=0;index<NumOfWaves;Index+=1)
        Wave AddToTable = $stringFromList(index,NewTableList)
        appendToTable AddToTable
    endfor
   
End

Hope this is a start.

Andy

Welcome to the forum. If you are now to Igor I very much recommend to read and do the Guided Tour in the Igor Manual. I know, most of the time one doesn't even take the manual out of its wrapping when getting into new software. However in the case of Igor the manual is not only excellent but will make a great difference to get good results fast. Otherwise Andy gave you a good start to tackle you problem. I can imagine that lots of this stuff may look like code salad to you. If you want to do things a little bit easier, maybe you can break up your problem into smaller pieces. For example, you may want to stop working with tables and organize your data in a way which makes it easier to write simple functions.