### 5.25.6 Perform thresholding operations on an array : threshold

threshold changes the data in an array which does not meet some kind of minimality criterion. It takes the following parameters :

- vector v of real or complex numbers
- bound specification bnd
- comparison operator (optional)
- abs[=true,false] (optional)

Bound specification may be either a single real number b (or an equation b=value) or a list of two real numbers l,u (or equations l=lvalue, u=uvalue). In the latter case a vector w is returned, as defined by :

wk=

⎧
⎪
⎨
⎪
⎩

uvalue (defaults to u ), | vk>u,
lvalue (defaults to l ), | vk<l,
vk, | otherwise

for k=0,1,…,n−1 where n= size( v ) when the element vk is a real number. If vk is complex, then |vk| is compared with u resp. l and the value uvalue resp. lvalue is multiplied by vk/|vk| .

In the first case where bnd is a number or an equation, the return vector w is defined by :

wk=

⎧
⎨
⎩

value (defaults to b ), | vk<b,
vk, | otherwise

if vk∈ℝ (if vk is complex, then |vk| is compared with b and the value is multiplied by vk/|vk| ), for k=0,1,…,n−1 . If comparison operator is specified (one of >, <= or >=, must be quoted), it is used instead of < (which is the default) in the above formula. If the fourth argument is specified, the data in v must be real and the following formula is used for wk , k=0,1,…,n−1 :

wk=

⎧
⎪
⎨
⎪
⎩

value, | vk≥ 0 and |vk|<b ,
−value, | vk<0 and |vk|<b ,
vk, | otherwise.

As before, value defaults to b and the comparison operator used to test |vk| against b (by default <) is specified by the third argument.

For example, 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 :

[-infinity,-11,-3,0,7,27,+infinity,+infinity]

In the following 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 :