Developing design: moving average filter. Part 5 – model of the filter.

Next step in extending test automation is to create model of the filter and run some tests on it. Perfect applications which will do that job are environments for technical and mathematical computations like Matlab or Octave, which very easily allow to express even complex algorithms.

Remembering about requirements for the filter, below you can find the filter’s model. I have to mention about fix function. I created model of the filter which originally is described in VHDL. There is not used float arithmetic. It means that my mathematical model has to be adjusted to the rules which are in VHDL, so I have to round all values to integers. Remember that functionality of two models has to be exactly the same.

function FilRes = AvgFilter(DataIn, FilLen)
   VecSize = size(DataIn);
   TmpRes = (zeros(VecSize));
   %for first samples, when nrOfSample < FileLen
   for i = 1:FilLen-1
      acc = 0;
      for j = 1:i
         acc = acc + DataIn(j);
      acc = acc/FilLen;
      TmpRes(i) = fix(acc);
   %for next samples, when nrOfSample >= FileLen
   for i = (FilLen):VecSize(1)
      acc = 0;
      for j = 0:FilLen-1
         acc = acc + DataIn(i-j);
      acc = acc/FilLen;
      TmpRes(i) = fix(acc);
   FilRes = TmpRes;

Now, I need to generate a test vector (can be random values) and a function which will write the test vector and the output vector from the filter to files. Then files will be used in VHDL simulation and for comparing results from two models. Below is shown a script which should be executed.

addpath ../../src/

DataInLen = 10;
CellMinVal = 100;
CellMaxVal = 1000;

FilLen = 4;

DataIn = randi([CellMinVal, CellMaxVal], DataInLen, 1);
WriteMat(DataIn, "DataIn.dat", '%04X');

DataOut = AvgFilter(DataIn, FilLen);

WriteMat(DataOut, "DataOut_Octave.dat", '%04X');

I think this code is self-explanatory. I set some input parameters like length of the input vector, max and min values. Then I generate an input vector, which is also written to a file, I send the vector through the model and write results to another file.

Last thing to show is WriteMat function, which writes data to the file.

function WriteMat(DataToWrite, FileName, ff)
   fid = fopen(FileName, 'w');
   fprintf(fid, [ff, '\n'], DataToWrite);

*** *** ***

All source codes used in that post you can find on – here & here.

*** *** ***

Leave a Reply

Your email address will not be published. Required fields are marked *