Guest User

MyHDL FIR Test Failure

a guest
Jun 28th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.12 KB | None | 0 0
  1. #
  2. # Based heavily on code from https://bitbucket.org/cfelton/examples/src/tip/siir/siir.py
  3. # which is Copyright (c) 2011 Christopher Felton
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. #
  18.  
  19.  
  20. from myhdl import *
  21.  
  22. import numpy as np
  23. from scipy.signal import firwin
  24.  
  25.  
  26. def adc_straight(
  27.     clk,
  28.     x,
  29.     y,
  30.     B=None,        # Numerator coefficients, in fixed-point specified
  31.     A=None,        # Denominator coefficients, in fixed-point specified
  32.     W=(24,0)       # Fixed-point description, tuple,
  33.                    #  W[0] = word length (wl)
  34.                    #  W[1] = integer word length (iwl)
  35.     ):
  36.        
  37.     @always(clk.posedge)
  38.     def adc_test():
  39.         y.next = x
  40.        
  41.     return instances()
  42.  
  43. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. # RTL description of the FIR filter
  45. def sfir_hdl(
  46.     # ~~ Ports ~~
  47.     clk,           # Synchronous clock
  48.     x,             # Input word, fixed-point format described by "W"
  49.     y,             # Output word, fixed-point format described by "W"
  50.  
  51.     # ~~ Parameters ~~
  52.     B=None,        # Numerator coefficients, in fixed-point specified
  53.     W=(24,0)       # Fixed-point description, tuple,
  54.                    #  W[0] = word length (wl)
  55.                    #  W[1] = integer word length (iwl)
  56.                    #  fraction word length (fwl) = wl-iwl-1
  57.     ):
  58.  
  59.    
  60.     assert isinstance(B, tuple), "Tuple of numerator coefficients length 3"
  61.     assert len(B) == 5, "Tuple of numerator coefficients length 5"
  62.  
  63.     assert isinstance(W, tuple), "Fixed-Point format (W) should be a 2-element tuple"
  64.     assert len(W) == 2, "Fixed-Point format (W) should be a 2-element tuple"
  65.  
  66.     # Make sure all coefficients are int, the class wrapper handles all float to
  67.     # fixed-point conversion.
  68.  
  69.     rB = [isinstance(B[ii], int) for ii in range(len(B))]
  70.  
  71.     assert False not in rB, "All B coefficients must be type int (fixed-point)"
  72.  
  73.     # double width max and min
  74.     _max = 2**(2*W[0])
  75.     _min = -1*_max
  76.  
  77.     Q  = W[0]-1
  78.     Qd = 2*W[0]
  79.  
  80.     # Delay elements, list of signals (double length for all)
  81.     ffd = [Signal(intbv(0, min=_min, max=_max)) for ii in range(len(B))]
  82.     yacc = Signal(intbv(0, min=_min, max=_max))
  83.    
  84.     b0 = B[0]
  85.     b1 = B[1]
  86.     b2 = B[2]
  87.     b3 = B[3]
  88.     b4 = B[4]
  89.  
  90.  
  91.     @always(clk.posedge)
  92.     def rtl_fir():
  93.         #for i in range(0, len(B-1)):
  94.         #    ffd[i+1].next = ffd[i]
  95.         #    ffd[i].next = x
  96.         ffd[4].next = ffd[3]
  97.         ffd[3].next = ffd[2]
  98.         ffd[2].next = ffd[1]
  99.         ffd[1].next = ffd[0]
  100.         ffd[0].next = x
  101.  
  102.         # Double precision accumulator
  103.         y.next = yacc[Qd:Q].signed()
  104.  
  105.     @always_comb
  106.     def rtl_acc():        
  107.         # Double precision accumulator
  108.         yacc.next = b0*ffd[0] + b1*ffd[1] + b2*ffd[2] + b3*ffd[3] + b4*ffd[4]
  109.  
  110.     return instances()
  111.  
  112. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
  113. class SIIR():
  114.     """
  115.    This is a simple example that illustrates how to create
  116.    an object for designing an simple (fixed structure) IIR
  117.    filter and the RTL implementation.  
  118.    """
  119.     def __init__(self,
  120.                  Fc=9200,     # cutoff frequency
  121.                  Fs=96000,    # sample rate
  122.                  b = None,    # or pass b,a coefficients
  123.                  a = None,    # or pass b,a coefficients
  124.                  sos = None,  # WIP sos of section matrix
  125.                  W=(24,0)     # Fixed-point to use
  126.                  ):
  127.         """
  128.        In general this object can be used to generate the HDL
  129.        for an IIR filter by passing the cutoff frequency
  130.        and stop frequency (absolute):  It can also generate
  131.        the HDL based on a set of coefficients or a sum of
  132.        sections matrix.  This object can only be used for 2nd
  133.        order type=I IIR filters or cascaded 2nd orders.
  134.  
  135.        -------
  136.        """
  137.         # The W format, intended to be (total bits, integer bits,
  138.         # fraction bits) is not fully support.
  139.         # Determine the max and min for the word-widths specified
  140.         self.W = W
  141.         self.max = int(2**(W[0]-1))
  142.         self.min = int(-1*self.max)
  143.        
  144.         # Filter Design
  145.         N  = 5
  146.         Wn = 0
  147.         self.isSos = False
  148.         if b is not None and a is not None:
  149.             self.b = b
  150.             self.nSection = 1
  151.         else:            
  152.             # Define the cutoff as a fraction of the nyquist
  153.             Wn = float(Fc)/(Fs/2.0)
  154.             self.b = firwin(N, Wn)
  155.             self.nSection = 1
  156.        
  157.         # fixed-point Coefficients for the IIR filter
  158.         self.fxb = np.round(self.b * self.max)/self.max
  159.        
  160.         # Create the integer version
  161.         self.fxb = tuple(map(int, self.fxb*self.max))
  162.  
  163.         print "FIR w,b", Wn, self.b
  164.         print "FIR fixed-point b", self.fxb
  165.  
  166.     def Convert(self, W=None):
  167.         """Convert the HDL description to Verilog
  168.        """        
  169.         clk = Signal(False)
  170.         #ts  = Signal(False)  
  171.         x   = Signal(intbv(0,min=-2**(self.W[0]-1), max=2**(self.W[0]-1)))
  172.         y   = Signal(intbv(0,min=-2**(self.W[0]-1), max=2**(self.W[0]-1)))
  173.    
  174.         print("Convert IIR to Verilog")
  175.         toVerilog(sfir_hdl, clk, x, y, B=self.fxb, W=self.W)
  176.  
  177. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
  178. if __name__ == '__main__':
  179.     # Instantiate the filter and define the Signal
  180.     W = (10,0)
  181.     flt = SIIR(W=W)
  182.  
  183.     flt.Convert()
Advertisement
Add Comment
Please, Sign In to add comment