# How to split one wave into multiple waves

Hi,

I would like to split one wave into equally sized waves. I am new to coding and I was hoping to get some help.

Salome

function split(waveID)
wave waveID
variable nPoints = DimSize (waveID,0)
Variable i, x
for (i = 0; i < nPoints; i+=81)
for (x = 2; x < 14; x+=1)
duplicate/R=[i] waveID, wavex
endfor
endfor
end

Hi Salome,

You are very close.  Here is how I would approach it and make it a bit more general.  The first thing that caught my eye was the interval of 81 which is not a common number and I was wondering if it was accurate. Since Igor Pro starts at 0, going from 0 up to but not including 81 actually gives you 81 points and not 80 as is sometimes the real goal. To that end I have also brought the sample size into the parameters which makes the overall function a bit more reusable.  From there I determine the number of possible subsets and do not assume that there is an event multiple which cause an error if it is applied to a wave with a non-even number of subsets.

The duplicate function now goes through each chuck of the input wave and creates a subset. For the output wave name, notice I create a base name as a string and then convert the index value to a string to concatenate onto the base name and use the \$ operation to have Igor use the value of the string. You could further extend the function to take a base name as an input parameter.  In your code the use of "wavex" does not change as you increment x. Wavex is just reused and actually will case an error the second time the duplicate function is called because it was created the first pass and will be used again the next time.  This is one reason I have also included the /O overwrite flag.  Usually when writing code you need to test it a couple of times and if you don't use the overwrite you need to remember to delete the created waves before each run.

I hope this helps and if I was bit too basic please forgive me, better too basic than too obscure.

Andy

Function Split(WaveID,samplesize)
wave waveID
variable samplesize

Variable HowMany
HowMany = floor(Numpnts(WaveID)/samplesize)
String NewBaseName ="NewName_"
Variable index

For(index=0;index<Howmany;index+=1)
Duplicate/O/R=[index*samplesize,(index+1)*samplesize-1] waveID, \$(NewBaseName+num2str(index))
endfor
End

Another approach would be to first redimension the 1D wave into a 2D wave where each column of the 2D wave represents what you want to become 1 wave in the output. Then use the SplitWaves operation to split that 2D wave into multiple 1D waves.