how to handle changing wave reference in typical commands

Hello,

Based on my inputs of start and end date of performing data analysis my code extracts the data for individual batches, naming them as batch0, batch1 etc... The number of batches that are extracted depends entirely on the time range I chose to analyze. Therefore, number of individual batches vary every time. Now, I want to do some mathematical operation on the data of each batch but couldn't find a way to automate that using, say for loop. Is there a way to do that?

The way I am extracting data for individual batch is using duplicate function for the parent wave between start and end points and saving them in different waves referenced like this - $nameofthewave(wve) + num2str(i). I am not sure if I could use the same referencing method to say add all positive outcomes in individual batches for all batches.

Any help would be much appreciated!!

Thank you.

Here is an example that may illustrate what you want. It returns a string list of the waves created.

// no error checking on exceeding wave length
Function/S extract_wlist(wave srcwave, variable spoint, variable pwidth, variable nwaves)

    string rootname = NameOfWave(srcwave)
    string rstr = "", tstr
    variable ps, pe
   
    variable ic
   
    for (ic=0;ic<nwaves;ic+=1)
        sprintf tstr, "%s_%02d", rootname, ic
        ps = spoint + ic*(pwidth - 1); pe = ps + pwidth - 1
        duplicate/R=[ps,pe]/O srcwave, $tstr
        rstr += tstr + ";"
    endfor
    return rstr
end

// on command line

> make/N=101/D swave = sin(p)
> print extract_wlist(swave, 0, 5, 6)
-->   swave_00;swave_01;swave_02;swave_03;swave_04;swave_05;

 

In reply to by jjweimer

jjweimer wrote:

Here is an example that may illustrate what you want. It returns a string list of the waves created.

// no error checking on exceeding wave length
Function/S extract_wlist(wave srcwave, variable spoint, variable pwidth, variable nwaves)

    string rootname = NameOfWave(srcwave)
    string rstr = "", tstr
    variable ps, pe
   
    variable ic
   
    for (ic=0;ic<nwaves;ic+=1)
        sprintf tstr, "%s_%02d", rootname, ic
        ps = spoint + ic*(pwidth - 1); pe = ps + pwidth - 1
        duplicate/R=[ps,pe]/O srcwave, $tstr
        rstr += tstr + ";"
    endfor
    return rstr
end

// on command line

> make/N=101/D swave = sin(p)
> print extract_wlist(swave, 0, 5, 6)
-->   swave_00;swave_01;swave_02;swave_03;swave_04swave_05;

 

So, for example, if I would like to add swave_00[5] + swave_03[7] + swave_04[6] + ... what kind of command I use to make it that happen.

I always have trouble to perform arithmetic operations with the waves that are created through some kind of automated process (in this case using a loop). 

> ... what kind of command ...?

variable sumT
sumT = swave_00[5] + swave_01[6] + ...

I would need to see the specifics in your processing to help with anything beyond this.

In reply to by jjweimer

jjweimer wrote:

> ... what kind of command ...?

variable sumT
sumT = swave_00[5] + swave_01[6] + ...

I would need to see the specifics in your processing to help with anything beyond this.

What I meant is that the number of waves created through the automated process change based on the time range I choose for analyzing the data. Therefore, if the time range is big enough I can have 100s of those waves created or if the time range is small enough I might have only 3 waves created. Now I want to do some arithmetic or manipulate the data in those individual waves. 

Updating then on my previous post, you might use this as an example ...

// no error checking on exceeding wave length
Function/S extract_wlist(wave srcwave, variable spoint, variable pwidth, variable nwaves)

    string rootname = NameOfWave(srcwave)
    string rstr = "", tstr
    variable ps, pe
   
    variable ic
   
    for (ic=0;ic<nwaves;ic+=1)
        sprintf tstr, "%s_%02d", rootname, ic
        ps = spoint + ic*(pwidth - 1); pe = ps + pwidth - 1
        duplicate/R=[ps,pe]/O srcwave, $tstr
        rstr += tstr + ";"
    endfor
    return rstr
end

Function sum_atIndex(string sList, variable pp)

    string theOne
    variable ic, Nwaves, sumT=0
    Nwaves = ItemsInList(sList)
    for (ic=0;ic<Nwaves; ic+=1)
        theOne = StringFromList(ic,sList)
        wave ww = $theOne
        sumT += ww[pp]
    endfor
    return sumT
end

Function do_test(variable pp)

    string sList
    make/N=101/D/O swave = sin(p)
    sList = extract_wlist(swave, 0, 5, 6)
    print "The sum at index ", pp, "is ", sum_atIndex(sList, pp)
    return 0
end

// on command line
> do_test(3)
  The sum at index   3  is   -0.247939

 

I think your question is how to create an arbitrary number of wave references.

Maybe the function that creates the waves can return a wave of type WAVE that contains wave references. As each wave is created you add a reference to the wave of references.

function/WAVE MakeWaveRefs(variable imax)
    Make/O/N=0/WAVE refs // using overwrite flags for demo
    variable i
    for(i=0;i<imax;i++)
        Make/O $"foo" + num2str(i) /wave = w
        refs[numpnts(refs)] = {w}
    endfor
    return refs
end

function test()
    wave/wave w = MakeWaveRefs(3)
    for (wave w : refs)
        Print NameOfWave(w)
    endfor
end

 

I tried to work on it but challenged to find a way to do things. Here is the code that I used to create arbitrary number of waves based on the start and endpoints. 

 

function ExtractBlocks(batch_time, delta_VOC)
	wave batch_time, delta_VOC
	//make/d/o/n=10 batch_data=0
    Extract/INDX batch_time, startPoints, (p==0 || batch_time[p-1]==0) && batch_time[p]!=0
    Extract/INDX batch_time, endPoints, (p==(numpnts(batch_time)-1) || batch_time[p+1]==0) && batch_time[p]!=0
    variable i,j
    for(i=0;i<numpnts(startPoints);i++)
        Duplicate/O/R=[startPoints[i],endPoints[i]] batch_time $NameOfWave(batch_time)+num2str(i)
        Duplicate/O/R=[startPoints[i],endPoints[i]] delta_VOC $NameOfWave(delta_VOC)+num2str(i)

 
    endfor
end

 

By running the code above, I created waves that are named as delta_VOC0, delta_VOC1, delta_VOC2, delta_VOC3..... Now, I would like to check each value in delta_VOC0, delta_VOC1, delta_VOC2, delta_VOC3..... if it is positive or negative. And add positive values and negative values separately for individual wave. I thought to use a for loop to do that, but then depending upon the time range I choose, I might have 100s of delta_VOCs (delta_VOC1, delta_VOC2, delta_VOC3, delta_VOC4, delta_VOC5, delta_VOC6.....), So, I can't do it in a simple way of referencing each delta_VOC wave. I believe I need another for loop where I can change the wave reference (delta_VOC0 then delta_VOC1 then delta_VOC2 then ......) until I cover all delta_VOC waves.

And when I use this automatic referencing method, how to obtain each value or element of the wave, like in normal circumstances, for example, we may use delta_VOC0[7] to read the value of 8th point, but when I have automatic referencing, then what syntax should I use to do that?

  

In reply to by vpratap397

vpratap397 wrote:

Now, I would like to check each value in delta_VOC0, delta_VOC1, delta_VOC2, delta_VOC3..... if it is positive or negative. And add positive values and negative values separately for individual wave.

It's not clear to me what you need here. Could you maybe give a more explicit example of how you would process a pair of time and delta_VOC waves? Do these waves all need to be saved, or are they an intermediate step in some calculation?

vpratap397 wrote:

And when I use this automatic referencing method, how to obtain each value or element of the wave, like in normal circumstances, for example, we may use delta_VOC0[7] to read the value of 8th point, but when I have automatic referencing, then what syntax should I use to do that?

Any wave reference that you create is no different to the 'normal' case.

function test()
    make/O foo7 = p * 10 // creates wave reference foo7
    wave w1 = foo7 // creates wave reference w1
    variable num = 7
    string str = "foo"
    wave w2 = $str + num2str(num) // creates wave reference w2
    // all three wave references can be used in this function
    print foo7[8], w1[8], w2[8]
end

 

It may be worthwhile reading the help about wave references. Copy these commands, paste them into Igor's command line and press Enter:

DisplayHelpTopic "Wave References"

DisplayHelpTopic "Accessing Waves In Functions"