data_handling#
Example Usage#
Full Contents:#
- data_handling.inpaint_nans(A, method)#
INPAINT_NANS: in-paints over nans in an array usage: B=INPAINT_NANS(A) % default method usage: B=INPAINT_NANS(A,method) % specify method used
Solves approximation to one of several pdes to interpolate and extrapolate holes in an array
- arguments (input):
A - nxm array with some NaNs to be filled in
- method - (OPTIONAL) scalar numeric flag - specifies
which approach (or physical metaphor to use for the interpolation.) All methods are capable of extrapolation, some are better than others. There are also speed differences, as well as accuracy differences for smooth surfaces.
methods {0,1,2} use a simple plate metaphor. method 3 uses a better plate equation,
but may be much slower and uses more memory.
method 4 uses a spring metaphor. method 5 is an 8 neighbor average, with no
rationale behind it compared to the other methods. I do not recommend its use.
- method == 0 –> (DEFAULT) see method 1, but
this method does not build as large of a linear system in the case of only a few NaNs in a large array. Extrapolation behavior is linear.
- method == 1 –> simple approach, applies del^2
over the entire array, then drops those parts of the array which do not have any contact with NaNs. Uses a least squares approach, but it does not modify known values. In the case of small arrays, this method is quite fast as it does very little extra work. Extrapolation behavior is linear.
- method == 2 –> uses del^2, but solving a direct
linear system of equations for nan elements. This method will be the fastest possible for large systems since it uses the sparsest possible system of equations. Not a least squares approach, so it may be least robust to noise on the boundaries of any holes. This method will also be least able to interpolate accurately for smooth surfaces. Extrapolation behavior is linear.
Note: method 2 has problems in 1-d, so this method is disabled for vector inputs.
- method == 3 –+ See method 0, but uses del^4 for
the interpolating operator. This may result in more accurate interpolations, at some cost in speed.
- method == 4 –+ Uses a spring metaphor. Assumes
springs (with a nominal length of zero) connect each node with every neighbor (horizontally, vertically and diagonally) Since each node tries to be like its neighbors, extrapolation is as a constant function where this is consistent with the neighboring nodes.
- method == 5 –+ See method 2, but use an average
of the 8 nearest neighbors to any element. This method is NOT recommended for use.
- arguments (output):
B - nxm array with NaNs replaced
Example
[x,y] = meshgrid(0:.01:1); z0 = exp(x+y); znan = z0; znan(20:50,40:70) = NaN; znan(30:90,5:10) = NaN; znan(70:75,40:90) = NaN;
z = inpaint_nans(znan);
See also: griddata, interp1
Author: John D’Errico e-mail address: woodchips@rochester.rr.com Release: 2 Release date: 4/15/06
- data_handling.medfilt1m(x, r, z)#
One-dimensional adaptive median filtering with missing values.
Applies a width s=2*r+1 one-dimensional median filter to vector x, which may contain missing values (elements equal to z). If x contains no missing values, y(j) is set to the median of x(j-r:j+r). If x contains missing values, y(j) is set to the median of x(j-R:j+R), where R is the smallest radius such that sum(valid(x(j-R:j+R)))>=s, i.e. the number of valid values in the window is at least s (a value x is valid x~=z). Note that the radius R is adaptive and can vary as a function of j.
This function uses a modified version of medfilt1.m from Matlab’s ‘Signal Processing Toolbox’. Note that if x contains no missing values, medfilt1m(x) and medfilt1(x) are identical execpt at boundary regions.
- USAGE
y = medfilt1m( x, r, [z] )
- INPUTS
x - [nx1] length n vector with possible missing entries r - filter radius z - [NaN] element that represents missing entries
- OUTPUTS
y - [nx1] filtered vector x
- EXAMPLE
x=repmat((1:4)’,1,5)’; x=x(:)’; x0=x; n=length(x); x(rand(n,1)>.8)=NaN; y = medfilt1m(x,2); [x0; x; y; x0-y]
See also MODEFILT1, MEDFILT1
Piotr’s Computer Vision Matlab Toolbox Version 2.35 Copyright 2014 Piotr Dollar. [pdollar-at-gmail.com] Licensed under the Simplified BSD License [see external/bsd.txt]
- data_handling.shiftImagesX(shiftx, tdat, fixed, fillval)#
SHIFTIMAGESX(shiftx, tdat, fixed)
- Parameters
shiftx (
int (or float, dealt as int)) – the shift applied to tdattdat (
N x M float or int image) – the image to shift relative to fixedfixed (
N x M float or int image) – the image to keep fixed but extend in x if necessaryfillval (
float or int) – value to fill out of bounds pixels default value is to fill with the shift value (to penalize these pixels in an optimization)
- Returns
shiftdat (N+few x M+few image)
shiftfixed (N+few x M+few image)
NPMitchell 2019
- data_handling.slidingmax(dat, win)#
sliding one dimensional maximum
- Parameters
dat (
N x 1 float or int array) – the data to be “smoothed” via sliding maximumwin (
int) – window size (full width)
- Returns
maxdata (N x 1 float or int array) – the 1d data “smoothed” via sliding max
NPMitchell 2020
- data_handling.springEnergy1D(xx, BL, fixed_ind, fixed_x)#
xx has only mobile dof Insert fixed positions into xx