# 2.1.22.1.2 ocmath_median_filter

## Description

The function will first sort all the data values from the surrounding neighbourhood into numerical order and then replace the data point being considered with the value at a certain percentile.

## Syntax

int ocmath_median_filter( const int nSize, const double * pIn, double * pOut, const int nPts, const double dPercent = 50, int nPadding = EDGEPAD_ZERO, bool bAveraging = false )

## Parameters

nSize
[input] Size of input and output data array.
pIn
[input] Pointer to input data array of type double to be filtered.
pOut
[output] Pointer to output data array of type double to receive results.
nPts
[input] Number of surrounding points to left and right. Thus the number of points considered in each segment is (2*nPts + 1).
dPercent
[input] The percent for computing median.
EDGEPAD_REFLECT pad reflect, end points are repeated such that on the left, [-1] = , [-2] = , [-3] =  and etc
EDGEPAD_REPEAT pad with  values the left and with [nSize-1] on the right
EDGEPAD_PERIODIC pad periodic, [-1] = [nSize -1], [-2] = [nSize - 2]
bAveraging
[input] If true, the function will average the most nearest neighbors if there is no match to the given percent.

## Return

Returns STATS_NO_ERROR on successful exit and a non-zero STATS error code on failure.

## Examples

EX1

////////////////////////////////////////////////////////////////////////////////////
// This example performs median filtering on a column of worksheet. At each point in the column,
// n neighboring points on each side are considered, and the median of all those points
// is assigned as the filtered result for that point.
// Parameters:
//            iNumPts:    Number of points on either side of each point, to consider for median
//
// Return:
//            0:            success
//            non-zero:    failure
int ocmath_median_filter_ex1(int iNumPts)
{
//Assume a worksheet is active
Worksheet wks = Project.ActiveLayer();
wks.SetSize(-1,3);
DataRange dr;
dr.Add("X", wks, 0, 0, -1, 0);
dr.Add("X", wks, 0, 1, -1, 1);
dr.Add("X", wks, 0, 2, -1, 2);
vector vxData, vyData;

dr.GetData(&vyData, 1);
uint nSize = vyData.GetSize();
if( (iNumPts * 2) >= nSize/2)
{
printf("Group size is too large!\n");
return -2;
}
// Set size of result to be same as source
vector vDest;
vDest.SetSize(nSize);
double dPerc = 50;
int iRet = ocmath_median_filter(nSize, vyData, vDest, iNumPts, dPerc);
if( iRet != OE_NOERROR)
return iRet;

dr.SetData(vDest, false, 2);

return 0;
}