Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio |PyJEM| Search

 

Function: Standard Deviation
Function

Function to compute the standard deviation (sigma n-1) of an image, or a set of values passed in as pixel values in an image. The number of data points (n), the mean and the sum are also returned.

Version
version:20160604, v2.0
Author
D. R. G. Mitchell
Acknowledgements
Comments

Data is passed into the function as an array (image). This is the front-most image. To analyse numerical data - create a one dimensional image, paste the values into the image (array) and pass it to the function.

System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
If you used the previous version of this standard deviation function in any of your scripts - please update the code with this version, as it corrects a bug.
Supported
Yes
Included Files
Main script file.
Source Code

// Function to compute the standard deviation (sigma n-1) of an image, or

// a set of values passed in as pixel values in an image. The

// number of data points (n) the mean and the sum are also returned.

// This emulates the functionality in the Analysis/Statistics menu

 

// D. R. G. Mitchell, adminnospam@dmscripting.com (remove the nospam to make this email address work)

// version:20160604, v2.0, June 2016, www.dmscripting.com

 

 

void StandardDeviation(image arrayimg, number &stddev, number &n, number &mean, number &sum)

{

mean=mean(arrayimg)

number xsize, ysize

getsize(arrayimg,xsize, ysize)

n=xsize*ysize

 

image nminusmeansqrd=(arrayimg-mean)**2

number sumsqrdiff=sum(nminusmeansqrd)

stddev=sqrt((sumsqrdiff/(n-1)))

sum=sum(arrayimg)

}

 

 

// main program starts here

 

number nodocs=countdocumentwindowsoftype(5)

if(nodocs<1)

{

showalert("Ensure an image is shown front-most.",2)

exit(0)

}

 

image front:=getfrontimage()

 

 

// Declare some variables

 

number stddev, meanval, nopixels, sum

 

 

// Call the standard deviation function

 

StandardDeviation(front, stddev, nopixels, meanval, sum)

 

 

// Output the results

 

result("\nNo of values = "+nopixels+"\nSum = "+sum+"\nMean = "+meanval+"\nStandard Deviation = "+stddev+"\n")

 

showalert("No of values = "+nopixels+"\nSum = "+sum+"\nMean = "+meanval+"\nStandard Deviation = "+stddev,2)