I am writing a procedure that will replace my NaN values for an average value of my data. So far, the program I have written simply copies the previous wave that I had into my new string. This is what my procedure looks like, hopefully someone can tell me if they see anything wrong:
Function replace_nan(data_wave)Wave data_wave
VariableiString destName = NameofWave(data_wave)[0,11]+"_modified"Make/O/N=(numpnts(data_Wave))/D $destNameWave/D dest = $destName
dest = NaNWavestats/q data_wave
For(i=0; i<(numpnts(data_wave)); i+=1)If(data_wave[i]!=NaN)
dest[i]=data_wave[i]Elseif(data_wave[i]== NaN)
dest[i]=V_avg
EndifEndforappendtotable dest
End
First of all, instead of data_wave[i]!=NaN and data_wave[i]== NaN, use the numtype function. That would give you this:
Function replace_nan(data_wave)Wave data_wave
VariableiString destName = NameofWave(data_wave)[0,11]+"_modified"Make/O/N=(numpnts(data_Wave))/D $destNameWave/D dest = $destName
dest = NaNWavestats/q data_wave
For(i=0; i<(numpnts(data_wave)); i+=1)If(numtype(data_wave[i])!= 2)
dest[i]=data_wave[i]Else
dest[i]=V_avg
EndifEndforappendtotable dest
End
Second, you don't need an explicit for loop here. Instead, you could use waveform assignment. Waveform assignment is much faster than explicit for loops. That leaves you with this:
Function replace_nan(data_wave)Wave data_wave
String destName = NameofWave(data_wave)[0,11]+"_modified"Duplicate/O data_wave, $destNameWave/D dest = $destNameWavestats/q data_wave
dest = (numtype(data_wave[p])!= 2) ? data_wave[p] : V_avg
appendtotable dest
End
Note that I also replaced the Make command with a simpler Duplicate command.
Third, in many cases, there's a way to use MatrixOp to give you improved performance and often simpler syntax. That's true in your case, giving you this:
Function replace_nan(data_wave)Wave data_wave
String destName = NameofWave(data_wave)[0,11]+"_modified"Wavestats/q data_wave
MatrixOp/O $destName = replaceNaNs(data_wave, V_avg)Wave/D dest = $destNameappendtotable dest
End
and exploit the fact that comparing NaN to everything is false. But I must strongly support Adams suggestions, especially the matrixOP solution. Which is faster, better readable and shorter.
data_wave[i]!=NaNanddata_wave[i]== NaN, use thenumtypefunction. That would give you this:Second, you don't need an explicit for loop here. Instead, you could use waveform assignment. Waveform assignment is much faster than explicit for loops. That leaves you with this:
Note that I also replaced the Make command with a simpler Duplicate command.
Third, in many cases, there's a way to use MatrixOp to give you improved performance and often simpler syntax. That's true in your case, giving you this:
March 3, 2016 at 08:17 am - Permalink
DisplayHelptopic "NumType".Instead of a loop you can use:
Check out
DisplayHelptopic "? :"to see what is happening.EDIT: Too late ;-)
March 3, 2016 at 08:22 am - Permalink
and exploit the fact that comparing NaN to everything is false. But I must strongly support Adams suggestions, especially the
matrixOPsolution. Which is faster, better readable and shorter.March 3, 2016 at 09:08 am - Permalink
March 3, 2016 at 10:34 am - Permalink