# can somebody help me understand this piece of code of an FIR

1. ## can somebody help me understand this piece of code of an FIR

Code:
```module fir_srg (clk, x, y);  //----> Interface

input        clk;
input  [3:0] x;
output [3:0] y;
reg    [3:0] y;
// Tapped delay line array of bytes
reg    [3:0] tap0, tap1, tap2, tap3;
// For bit access use single vectors in Verilog

always @(posedge clk)  //----> Behavioral Style
begin : p1
// Compute output y with the filter coefficients weight.
// The coefficients are [-1  3.75  3.75  -1].
// Multiplication and division for Altera MaxPlusII can
// be done in Verilog with sign extensions and shifts!
y <= (tap1<<1) + tap1 + {tap1[3],tap1[3:1]}
+ {tap1[3],tap1[3],tap1[3:2]} + (tap2<<1) + tap2
+ {tap2[3],tap2[3:1]}
+ {tap2[3],tap2[3],tap2[3:2]} - tap3 - tap0;

tap3 <= tap2;  // Tapped delay line: shift one
tap2 <= tap1;
tap1 <= tap0;
tap0 <= x;   // Input in register 0
end

endmodule```
this is basically a 4 tap FIR filter. it was taken from the book "Digital Signal Processing with Field Programmable Gate Arrays" by Uwe Meyer.

the difficulty that i am having in understanding this code is in the always block. i understand the non-blocking assignments tap3 <= tap2 etc but i dont understand how y is calculated.

•

2. ## Re: can somebody help me understand this piece of code of an

4 tap filter eqn for ur design is:
y = (-1 * Tap0) + (3.75 * Tap1) + (3.75 * Tap2) + (-1 * Tap3)

while Rearranging it looks like:
y = [(2 + 1 + 0.5 + 0.25) * Tap1] +
[(2 + 1 + 0.5 + 0.25) * Tap2] +
- Tap3 +
-Tap0;

Now Correlate this with ur verilog code:
y <= (tap1<<1) + tap1 + {tap1[3],tap1[3:1]} + {tap1[3],tap1[3],tap1[3:2]} +
(tap2<<1) + tap2 + {tap2[3],tap2[3:1]} + {tap2[3],tap2[3],tap2[3:2]} +
- tap3
- tap0;

tap <<1 means -- shift left, which is as good as Xly by 2.
{tap1[3],tap1[3:1]} -- means shift right with msb intact-- as good as xly by 0.5
{tap1[3],tap1[3],tap1[3:2]} --means shift right by 2 -- as good as xly by 0.25

So adding all these u get 3.75 * Tap1
Similarly for the Tap2 also

Hope this helps

1 members found this post helpful.

•

3. ## Re: can somebody help me understand this piece of code of an

Renjith you are my hero.

now it becomes all clear to me. actually i didnt see this type of bit shifting before. i knew that it was concatenating bits but i didnt know that it was actually shifting the bits.

Added after 1 hours 39 minutes:

in all the excitement i forgot to ask one thing. cant the >> operator be used for shift right? whats the difference between using the >> operator and using concatenation as in the code?

--[[ ]]--