Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Can anyone help me understand this code for halfband filter?

Status
Not open for further replies.

faye_hongdou

Member level 1
Joined
Nov 21, 2003
Messages
34
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Activity points
280
Code:
     module halfband(clk, din, dout);
          input  clk;
          input  [11:0]  din;
          output  [12:0] dout;
          
          reg  [12:0] dout;
          reg  [11:0] d_m4, d_m3, d_m2, d_m1, d_0, d_1, d_2, d_3, d_4, d_5;
          reg  half;

          wire  [12:0] r5, r3, r1;
          wire  [11:0] r0;
          wire  [22:0] rr5, rr3, rr1, rr0;
          wire  [22:0] result;

          always @ (posedge clk)
            half  <=  ! half;

          always @ (posedge clk)
             if (half)
                dout <= result [22: 10] ;

          always @ (posedge clk)
             begin
                  d_m4 <= din;
	          d_m3 <= d_m4;
	          d_m2 <= d_m3;
	          d_m1 <=  d_m2;
	          d_0 <=  d_m1;
	          d_1 <=  d_0;
	          d_2 <=  d_1;
	          d_3 <=  d_2;
	          d_4 <=  d_3;
	          d_5 <=  d_4;
            end

          assign r5 = {d_5[11], d_5} + {din[11], din};
          assign r3 = {d_3[11], d_3} + {d_m3[11], d_m3};
          assign r1 = {d_1[11], d_1} + {d_m1[11], d_m1};
          assign r0 = {d_0};        

         // expand the data to 24 bit data.
         assign  rr5 = r5[12] ? {11'b1, r5} : {11'b0, r5};
         assign  rr3 = r3[12] ? {11'b1, r3} : {11'b0, r3};
         assign  rr1 = r1[12] ? {11'b1, r1} : {11'b0, r1};
         assign  rr0 = r0[11] ? {12'b1, r0} : {12'b0, r0};

          wire   [22:0]   result1 = r5 - (r3<<4);
          wire   [22:0]   result2 = result1 + (r1 <<1);
          wire   [22:0]   result3 = result2 + (result2 << 2);

          wire   [22:0]   result4 = r1 + (rr1<< 6);
          wire   [22:0]   result5 = (r1 << 9 ) + (r0 << 10);
          wire   [22:0]   result6 = result4 + result5;
          
          assign result = result3 + result6;

     endmodule

I know that for a halfband filter, every alternative coefficent is zero. And understand the assignment "d_m4 <= din" etc. My problem is that why r5, r3 and r1 should be 13 bits, while r is 12bits. And how the result, result1, ..., result6 is calculated.
The only thing I know is that it is a halfband filter, how can I calculate the coefficients for this filter?


[/img]/root/Desktop/6.jpg
 

r0 is produced directly from d0 which is 12 bits while the other (r1..r5) are produced by adding signals, so they are extended by one bit to avoid overflow.
The calculation of the results are:
result1 = r5 - 16*r3
result2 = result1 + 2*r1
result3 = result2 + 4*result2
result4 = r1 + 64*rr1
result5 = 512*r1 + 1024*r0

Basically when there is shift to the right (<<) it means multiplication. If <<1 it means multiplication by 2, <<2 by 4, <<3by 8 and so on. In general <<n is multiplication by 2 to the power of n.
When shift to the left is division.
I hope now the code is clearer for you
 

Re: Can anyone help me understand this code for halfband fil

Thanks.
I understand that <<n means to multiply by 2^n.

The code is to realize a halfband decimation filter. I only know that the coefficients of the filter are alternatively zero, and symmetry. And there is not any other detailed discription.
My puzzle is how can I get the coefficients? In another words, how can I know what I should write is "result1 = r5 - 16*r3", but not "result1 = r5 + 4*r3", or anyother equations? What is the guide behide this .
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top