Performing thresholding operations on an array: threshold

### 15.2.12 Performing thresholding operations on an array: threshold

The threshold command changes data in an array by raising (or lower) the values to meet a given threshold.

- threshold takes two mandatory arguments and two optional arguments:
  - v, a vector of real or complex numbers.
  - a bound specification, which can be one of: This can be either:
    - b=b0 for real numbers b and b0.
    - b, a real number (equivalent to b=b).
    - [l=l0,u=u0], for real numbers l,l0,u,u0.
    - [l,u], a list of two real numbers (equivalent to [l=l,u=u]).
  - Optionally ’≺’, a quoted comparison operator, one of ’<’, ’<=’, ’>’, ’>=’ (by default ’<’).
  - Optionally, abs=bool, where bool is either true or false (by default abs=false). If abs=true, then the components of v must be real.
- threshold(v,b=b0 ⟨,′≺′⟩) returns the vector w whose kth component is:

  wk= |
  ⎧ ⎨ ⎩ |
  b0, | vk ≺ b,
  vk, | otherwise

  when vk is real and

  wk= |
  ⎧ ⎪ ⎪ ⎨ ⎪ ⎪ ⎩ |
  b0 |
  vk
  |vk|
  | ,
  | |vk| ≺ b,
  vk, | otherwise

  when vk is complex; for k=0,1,…,size(v)−1
- threshold(v,b=b0 ⟨,≺⟩,abs=true) returns the vector w whose kth component is:

  wk= |
  ⎧ ⎪ ⎨ ⎪ ⎩ |
  b0, | |vk| ≺ b and vk>0,
  −b0, | |vk| ≺ b and vk<0
  vk, | otherwise

- threshold(v,[l=l0,u=u0] ⟨,≺⟩) returns the vector w whose kth component is:

  wk= |
  ⎧ ⎪ ⎨ ⎪ ⎩ |
  u0, | u ≺ vk,
  l0, | vk ≺ l
  vk, | otherwise

  when vk is real and

  wk= |
  ⎧ ⎪ ⎪ ⎪ ⎪ ⎨ ⎪ ⎪ ⎪ ⎪ ⎩ |
  u0 |
  vk
  |vk|
  | ,
  | u ≺ |vk|,
  l0 |
  vk
  |vk|
  | ,
  | |vk| ≺ l,
  vk, | otherwise

  when vk is complex; for k=0,1,…,size(v)−1.

#### Examples

- Input:
  threshold([2,3,1,2,5,4,3,7],3)
  Output:

  [3,3,3,3,5,4,3,7]

- Input:
  threshold([2,3,1,2,5,4,3,7],3=a,’>=’)
  Output:

  ⎡ ⎣ | 2,a,1,2,a,a,a,a | ⎤ ⎦

- Input:
  threshold([-2,-3,1,2,5,-4,3,-1],3=0,abs=true)
  Output:

  [0,−3,0,0,5,−4,3,0]

- Input:
  threshold([-2,-3,1,2,5,-4,3,-1],3=0,’<=’,abs=true)
  Output:

  ⎡ ⎣ | 0,0,0,0,5,−4,0,0 | ⎤ ⎦

- Input:
  threshold([-120,-11,-3,0,7,27,111,234],[-100,100])
  Output:

  ⎡ ⎣ | −100,−11,−3,0,7,27,100,100 | ⎤ ⎦

- Input:
  threshold([-120,-11,-3,0,7,27,111,234],[-100=-inf,100=inf])
  Output:

  ⎡ ⎣ | −∞ ,−11,−3,0,7,27,+∞ ,+∞ | ⎤ ⎦

- In this example, a square-like wave is created from a single sine wave by clipping sample values.
  Input:

  data:=threshold(3*sin(2*pi*440*soundsec(2)),[-1.0,1.0]):;
  s:=createwav(data):;
  playsnd(s)

  Output:
  1
  Input:
  plotwav(s,range=[1000,2000])
  Output: