# Compare vectors with threshold

1. ## Compare vectors with threshold

hi all

i'v 2 std vectors (16 bit wide) and i need to compare them within threshold (4 bit wide).

Code:
```enable	 <= '1' when unsigned(DATA) <= ((DATA2)+("0000"&unsigned(THRESHOLD))) and
unsigned(DATA) >= ((DATA2)-("0000"&unsigned(THRESHOLD))) else
'0' ;```
i know there is a problem on the edges of the vector, for example:

2>= 0-7

any suggestion would be aprochiated

Best
Gil

•

2. ## Re: Compare vectors with threshold

I dont really understand the problem. Please could you explain what the issue is. Your code sets enable to '1' when DATA is between the THRESHOLD values on DATA2.

NOTE. Why is everything declared as std_logic_vector, and not unsigned? doing all these type conversions makes it harder to read and may end up giving you an RSI.

•

3. ## Re: Compare vectors with threshold

I dont really understand the problem. Please could you explain what the issue is. Your code sets enable to '1' when DATA is between the THRESHOLD values on DATA2.
the problem is with negative numbers, for example:
DATA = 4,
DATA2 = 2
THRESHOLD = 5

•

4. ## Re: Compare vectors with threshold

Your problem is one of wrap around. The 2nd compare will wrap around to a very high unsigned number.
Why not used the signed type instead to avoid this problem?

5. ## Re: Compare vectors with threshold

Do you mean somthing like this:

Code:
`enable <= '1' when unsigned(abs(signed(DATA)-signed(DATA2)) < ("0000"&unsigned(THRESHOLD)) else '0'  ;`

6. ## Re: Compare vectors with threshold

Thats pretty hiddeous. WHats wrong with this:

```Code VHDL - [expand]1
2
3
enable   <= '1' when signed('0' & DATA) <= (signed('0' & DATA2)+(signed('0' & THRESHOLD))) and
signed('0' & DATA) >= (signed('0' & DATA2)-(signed('0' & THRESHOLD))) else
'0' ;```

Would be even better if DATA, DATA2 and THRESHOLD are signed data types in the first place. There is absolutly no need for them to be std_logic_vectors. Ports can be unsigned/signed.

1 members found this post helpful.

•

7. ## Re: Compare vectors with threshold

Hi

i'v changed it so it can do the warp around:
Code:
```enable_riz	 		<= '1' when resize(signed(COMPA),COMPA'length +2)   <= (resize(signed(COMPB),COMPB'length +2)  +(signed('0' & th))) and
resize(signed(COMPA),COMPA'length +2)   >= (resize(signed(COMPB),COMPB'length +2)  -(signed('0' & th)))
else '0' ;```
Code:
```enable   	<= '1' when signed('0' & COMPA) <= (signed('0' & COMPB)+(signed('0' & th))) and
signed('0' & COMPA) >= (signed('0' & COMPB)-(signed('0' & th)))
else '0' ;```
and the results are:

8. ## Re: Compare vectors with threshold

What is your question? You have only posted a snippet and a waveform with different signal names to what is in your code.
Please ask a question and post the whole code if you have an issue.

--[[ ]]--