quantize: Force values into a set of bins

Description Arguments Value Usage Details Author(s) See Also Examples

Description

This function quantizes data into a set of bins based on a metric function. Each value in the input is evaluated with each quantization level (the bin), and the level with the smallest distance is assigned to the input value.

Arguments

x

A sequence

bins

The bins to quantize into

metric

The method to attract values to the bins

Value

A vector containing quantized data

Usage

quantize(x, bins=c(-1,0,1), metric=function(a,b) abs(a-b))

Details

When converting analog signals to digital signals, quantization is a natural phenomenon. This concept can be extended to contexts outside of DSP. More generally it can be thought of as a way to classify a sequence of numbers according to some arbitrary distance function.

The default distance function is the Euclidean distance in 1 dimension. For the default set of bins, values from (-infty, -.5] will map to -1. The values from (-.5, .5] map to 0, and the segment (.5, infty) map to 1. Regardless of the ordering of the bins, this behavior is guaranteed. Hence for a collection of boundary points k and bins b, where |b| = |k| + 1, the mapping will always have the form (-infty, k_1] => b_1, (k_1, k_2] => b_2, ... (k_n, infty) => b_n.

Author(s)

Brian Lee Yung Rowe

See Also

confine

Examples

1
2
3
4
x <- seq(-2, 2, by=.1)  
quantize(x)

quantize(x, bins=-1.5:1.5)

Example output

 [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  0  0  0  0  0  0  0  0  0
[26]  0  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
 [1] -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -1.5 -0.5 -0.5 -0.5 -0.5
[16] -0.5 -0.5 -0.5 -0.5 -0.5 -0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5  0.5
[31]  0.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5  1.5

lambda.tools documentation built on May 2, 2019, 4:28 a.m.