Sliding Window Over Wave

Hey Forum,

I have started to write a piece of code to help me with an analysis of some time series data, and I am stumbling with something which is likely to be pretty simple. The code is below, relevant info in comment sections:


// This aims at defining a window (or bin) that will slide along a waveform,
// perform FFT analysis and curve fitting and to retrieve fit values at each window position
// before moving on to the next position as defined by a given step-size and looping until
// all data-points are exhausted.

Function TestForSlide()
	
	Wave Wave_WF
	
	//Find window size in terms of points for an input seconds
	Variable N = numpnts(Wave_WF)
	Variable L = 60 //window size in seconds has to be prompted to user
	variable size, j, k

	For (size=0; size<N+1; size+=1)
		j = pnt2x(Wave_WF, size)		
		If(j<L)
			continue
		else
			print "window size of ", j, "seconds, corresponds to ", size, "points"
			break
		endIf
	endfor
	
	 
	// slide the window over data in defined stepsizes and do something (currently, do average) at each step
	// at each step also gather info and store it in a wave.
	variable wleft, wright
	variable step = 1 //stepsize in datapoints
	Wave Averages
	
	For (wleft=0; wleft<=N; wleft+=step)
		wright = wleft+size
		WaveStats /Q /R=[wleft, wright] Wave_WF
		print "Window from ", wleft, "to ", wright, "has an average of ", V_avg
		Averages[wleft]=V_avg
		If (wright<N)
			continue
		else
			break
		endIF
	endFor	
end



I get an error of trying to operate on a null or missing wave.
I guess this has to do with my Averages wave. What am I doing wrong?

Cheers,
R.
I didn't try ro run your function, but both your for loops go one point further than the number of points in the wave. Remember, if a wave has 10 points the index of the last point is 9, not 10. The WaveStats command also goes beyond the length of Wave_WF by an additional 'size' number of points. 'wright' cannot be allowed to be larger than N-1. You test the value of 'wright' after you have already called WaveStats

Couldn't you replace the first for loop with
x2pnt
? It seems like you are looking for the point corresponding to an x-value of 'L'

You could also include your additional continue/break condition into the loop using
for (wleft=0; (wleft<=N) && (wright<N); wleft+=step)

or something similar, but since wright is always larger than wleft, you could skip the wleft condition altogether
for (wleft=0; (wright<N); wleft+=step)

or even better base the for loop on wright instead of wleft
for (wright=size; (wright<N); wright+=step)
Hi,

I'm guessing that when you run the function you don't have the averages wave? If so then you need to make it with the
Make
command. This will also declare the wave. If I'm right, you are just declaring a wave which doesn't exist so Igor doesn't know what to do with the assignments in your loop.

So instead of
Wave Averages

Go for

//...
Make/O/N=(N) Averages

for(wleft = 0; wleft < N; wleft += 1)
//...

Thank you both for the suggestions!!

I made alterations in the
 FOR
loops and went for
 MAKE
the wave instead of simply declaring it and it now works.

So, I have also implemented the FFTs and gaussian fits to this, and am testing it now. I will probably put here more questions on this later!

Cheers,

R.