Using if endif statement checking odd or even number of data in wave.

Hello,

I am doing some FFT repetitively for some data file and I want the script to check if the number of data points is even (a must for FFT) or not. If it is odd, then delete one data points from all wave to make it even.

Variable NOD=DimSize(wave0,0)
Variable REMAIN=mod(NOD,2)

if (REMAIN 1==0)

DeletePoints NOD-1,1, wave0,wave1,wave2,wave3,wave4,wave5,wave6,wave7

endif


I used the above code to detect the modulus. If the number of data is odd, modulus equals to 1 then one data point will be deleted from all waves.

However, when I run this in front of all other codes, Igor prompts me "expected wave, variable name...." when it comes to the "if" code.

So, I want to ask if it is not allowed to run "if endif" directly in command line without putting them into a function?

Or, what is the simplest way to achieve that? (make all waves to even number of data points for FFT. Thanks.
OK I managed to do this by putting the function into the procedure window and save it as a global procedure.

eesyliu wrote:
Hello,

I am doing some FFT repetitively for some data file and I want the script to check if the number of data points is even (a must for FFT) or not. If it is odd, then delete one data points from all wave to make it even.

Variable NOD=DimSize(wave0,0)
Variable REMAIN=mod(NOD,2)

if (REMAIN 1==0)

DeletePoints NOD-1,1, wave0,wave1,wave2,wave3,wave4,wave5,wave6,wave7

endif


I used the above code to detect the modulus. If the number of data is odd, modulus equals to 1 then one data point will be deleted from all waves.

However, when I run this in front of all other codes, Igor prompts me "expected wave, variable name...." when it comes to the "if" code.

So, I want to ask if it is not allowed to run "if endif" directly in command line without putting them into a function?

Or, what is the simplest way to achieve that? (make all waves to even number of data points for FFT. Thanks.

eesyliu wrote:

Variable NOD=DimSize(wave0,0)
Variable REMAIN=mod(NOD,2)

if (REMAIN 1==0)

DeletePoints NOD-1,1, wave0,wave1,wave2,wave3,wave4,wave5,wave6,wave7

endif


I used the above code to detect the modulus. If the number of data is odd, modulus equals to 1 then one data point will be deleted from all waves.

However, when I run this in front of all other codes, Igor prompts me "expected wave, variable name...." when it comes to the "if" code.

So, I want to ask if it is not allowed to run "if endif" directly in command line without putting them into a function?

Or, what is the simplest way to achieve that? (make all waves to even number of data points for FFT. Thanks.


If you really wanted to do something like this from the command line, you could execute the following (from the command line)

DeletePoints NOD-1,REMAIN, wave0,wave1,wave2,wave3,wave4,wave5,wave6,wave7

One point will be deleted for mod = 1; none for mod = 0.

This assumes that your waves and variables exist in the current directory.
Have you considered, rather than deleting points, to mirror your data before you do the FFT operation? This always creates an even number of data points. I have used this trick successfully to minimize Gibb's oscillations (ringing) when I work in the FFT domain to smooth spectra. The caveat is to be aware of how this affects the inverse transformation. This function will mirror a scaled, 1-D wave about the last point.

Function MirrorWaveAtEnd(ww)
     wave ww

     variable npts, sx, dx
     npts = DimSize(ww,0)
     sx = DimOffset(ww,0)
     dx = DimDelta(ww,0)
     duplicate/O/FREE ww dww
     dww = ww[npts-p-1]
     Concatenate/NP/O {ww,dww}, ww
     SetScale/P x, sx, dx, ww    // you may want to use either 2*dx or dx/2 as the step size
     return 0
end


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
     duplicate/O/FREE ww dww
     dww = ww[npts-p-1]


It would be slightly more efficient to use:

WaveTransform flip ww
Well, this just shows how dated my knowledge is about the newer operations. Otherwise, it seems one either makes a free wave or kills a created wave.

Duplicate/FREE ww fww
Reverse fww
Concatenate/NP/O {ww,fww} ww


versus

WaveTransform flip ww
Concatenate/NP/O {ww,W_flipped} ww
killwaves W_flipped


Edit: Fixed syntax to Concatenate operation ... thanks AG!

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville