I want to implement a neural network in the Sum of Products form and thus have chosen the VHDL coding approach instead of the MATLAB VHDL Coder. Can someone help me with using decimal values in VHDL and subsequently, FPGA?
Decimal? Of course, digital computer hardware works best when it works in binary form and then just inputs/outputs in a form that humans understand like decimal. I don't understand why you need decimal. There is a thing called "binary-coded decimal" (BCD) which may help you. See wikipedia for that.
personally, to overcome fixed point arithmetic complexity, sometimes I choose a convenient accuracy for my data (number of bits, for example 10bits for 10-3 precision level) and then all signals within my design are multiplied by this number of bits (shift operation).
I agree with Nadir, years ago I was involved in a project related to neural networks, and I used fixed point representation, unless you use a custom floating point representation, based on IEEE 754. Ronak, for the network of your choice, you would have to evaluate the expected maximum range of signals (maybe all unipolar, or maybe bipolar) and then assign the number of bits for representing the analog numbers (integer and decimal part), and also the sign. Not sure if your algorithm uses only additions/subtractions or also multiplication. If multiplications are used, take into account the FPGA you are using, since DSPs are limited in number.
This is very important aspect of FPGA-based designs. Especially, where mathematical operations are involved extensively (e.g. Communication, Automotive, and DSP applications). I am involved in several industrial projects that has special precision and accuracy requirements such that 1 is still different than 1.1 or 1.1122, so number of fractional digits should be specified carefully.
Anyway writing behavioral coding in Verilog or VHDL is quite straightforward. But you have to be careful of changing the behaviour and the synthesizability of your design so always you need to check the RTL netlist and then the simulation results.
To represent the numbers you can easily say:
(number of bits ' base the value in the specified base)
Example:
parameter My_Value= 2'b10; // 2 bit binary number
parameter My_Value= 16; // at least a 32-bit decimal number
parameter My_Value= 8'hAf; // 8-bit hexadecimal
parameter My_Value= -16'd47; // negative decimal number
When it comes to fractional number, in which you need a certain accuracy to be achieved such as calculating the sine of an angle: You have to follow the fixed point or floating point format in HDL. I prefer the fixed point which is you have to define a signed word with for instance 16 bits. and then define the number of fractional digits out of those 16 bits. Let's say 15 bits are fractional (i.e. in binary 0.101010100010101).
Example:
to represent fractional decimal to fixed point 15fractional bit format in verilog (Q15): you multiply the fractional number by 2^15 (15 here depends on the fractional digits you specify earlier) Represent 0.789 in verilog: (2^15)*0.789=25853.952 rounding this number to 25854 input signed [15:0] My_Value; and then in the testbench you give it My_Value= 25854; similarly with -0.789 but you add bit 1 to the MSB
to convert the Q15 back to the original fractional decimal you divide the 25854 over 2^15, 25854/( 2^15)=0.78900146484
Usually NNs require normalizing inputs in range -1..1 or 0..1. Thus, Q-arithmetic is the common choice for such representation. But be aware that every operation in Q requires normalization: Q15*Q15 will give you Q30 number and you need to normalize it by right shift and rounding. Protip: Xilinx DSP48E (and I believe Altera too) are very handy for Q-arithmetic, in 7th family they even have embedded bit shifter.
You can convert your inputs and all values to binary and implement your system with binary values. You can use (dec2bin(x)) in MATLAB to convert values to binary. I implemented a large neural network on an FPGA using binary numbers.
I agree with Safa, In order to convert a floating‐point value "a" to the corresponding fixed‐point value, use the following steps:
1- Calculate b=a*(2^F), where F is the fractional length of the variable. Note that b is represented in decimal.
2- Round the value of b to the nearest integer value. For example :
round (4.53)=5, round (-2.91)=-3
3- Convert b from decimal to binary representation then select F bits from position 0 to F‐1 as the fractional part of the fixed‐point variable.
Example 1: Convert a floating point value (9.51432) to corresponding fixed point value and then find the corresponding binary value and finally show the conversion of a binary value to corresponding real value.
(W,F)=(12,7) where W is the total of the variable and F is the fractional length of the variable
1- 9.51432 *(2^7)= 1217.8329
2- round (1217.8329)= 1218
3- x= dec2bin (1218)= 010011000010
4- x= 01001.1000010
Note: To convert a binary value to corresponding real value: