control.py 3.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
# This file is part of librefdatool. librefdatool is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2013 Javier D. Garcia-Lasheras
#

import numpy as np
from numpy import pi, log10
from scipy import signal
from matplotlib import pyplot as plt
from matplotlib import mlab


class control:
    '''This class acts as control interface for filter parameters,
    such as structure realization, fixed-point approximation...
    Note (limited to FIR filters)
    '''


    def __init__(self):
        '''This method provides initial filter parameters,
        executing each set command with default values when
        a new filter object is intantiated.
        '''
        self.setBus()
        self.setScaling()
        self.setStructure()
        self.setModel()
        self.setTrace()
        self.setWorkspace()
        self.setToolchain()


    def setBus(self, busX=[1,15], busY=[1,15], busC=[1,15]):
        '''assign the different bus width:
        The value is given as a pair of natural numbers [p,q]:
        - p is the number of bits representing the integer part.
        - q is the number of bits representing the fractional part
        There are three different configurable buses:
        * busX: Input bit width
        * busY: Output bit width
        * busC: Coefficient bit width
        '''
        self.busX = busX
        self.busY = busY
        self.busC = busC


    def setScaling(self, scalingX=16, scalingC=16):
        '''assign the different FP scaling factor:
        * scalingX: Input FP scaling factor (power of two exponent)
        * scalingC: Coefficient FP scaling factor (power of two exponent)
        '''
        self.scalingX = scalingX
        self.scalingC = scalingC


    def setStructure(self, structure='firfilt'):
        '''assign filter structure or realization.
        Availability depends on python-to-HDL toolchain selection.
        TBD: only "firfilt" structure is available
        Future structures are type I/II direct forms, transposed...
        '''
        self.structure = structure


    def setTrace(self, trace=False):
        '''assign activation state for VCD file tracer.
        If true, a *.vcd file will be generated by simulation process.
        This file can be analyzed with third party tools such as GTKWave.
        '''
        self.trace = trace


    def setModel(self, model='myhdl'):
        '''assign HDL model, this is, the languaje for the toolchain. 
        When not selected the native/default value, 
        it relies on languaje conversion cogeneration & Icarus cosimulation.
        For myhdl toolchain, valid values are [myhdl, verilog, vhdl, vhdl2]
        TBD: this mechanism still has to be adapted for toolchain selection
        and including migen an other tools (maybe GHDL for cosimulation).
        '''
        self.model = model


    def setWorkspace(self, workspace='.'):
        '''assign workspace folder for filter design and analysis. 
        By default, the path is the one where the librefdatool is called.
        In this path, output files will be generated:
        * VHDL, Verilog converted files
        * VCD simulation files
        '''
        self.workspace = workspace


    def setToolchain(self, toolchain='myhdl'):
        '''Choose python-to-HDL toolchain. 
        By default, MyHDL is the toolchain in use.
        TBD: migen toolchain will be developed in the near future
        '''
        self.toolchain = toolchain