### 21.3.4 Performing thresholding operations on an array

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

- threshold takes two mandatory arguments and two optional arguments:
  - v, an array (vector or matrix) 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 array w of the same dimensions as v 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

threshold([2,3,1,2,5,4,3,7],3)

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

threshold([2,3,1,2,5,4,3,7],3=a,'>=')

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

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

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

threshold([-2,-3,1,2,5,-4,3,-1],3=0,'<=',abs=true)

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

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

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

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

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

In the following example, a square-like signal is created from a sine wave by clipping sample values.

data:=threshold(3*sin(2*pi*440*soundsec(2)),[-1.0,1.0]):; snd:=createwav(data):; plotwav(snd,range=[1000,2000])