# Math Help!



## newbie32 (May 1, 2014)

Long story short is that I'm a programmer not a mathlete nor DSP guy and I'm in the unusual position of not being able to explain a formula. We're working with a board that uses a MATLAB "2nd Order Transfer Filter" to which I've seen some reference. We're verifying the tests and the reviewer walks up to me with  "I don't get it." Taking a look at the MATLAB description gives me the formula:


```
Out = In * [(A + Bz-1 + Cz-2)/(D + Ez-1 + Fz-2)]
```

Where [z1] and [z2] are history states. The actual diagram breaks down to the following Python snippet:

```
class SecondOrderTransferFunc:
  def __init__(self):
    self.hist1 = 0
    self.hist2 = 0
    
    self.num1 = 0.87
    self.num2 = -1.55
    self.num3 = 0.85
    self.den1 = 1.0
    self.den2 = -1.75
    self.den3 = 0.92
    
  def exec(self,input):
    out = self.den1 * ((input * self.num1) + self.hist2)
    self.hist2 = ((input * self.num2) + self.hist1) - (self.den2 * out)
    self.hist1 = (input * self.num3) - (self.den3 * out)
    return out
```

So how does the matrix-looking division given in the description break down into the code? I've been plugging away looking at different types of transforms but honestly, I'm not making much headway (hardware manuals make me sleepy). Any pointers from the crowd would be appreciated!


----------



## chrbr (May 2, 2014)

I think you might have misunderstood the MATLAB code. I'd like to give you an extremely short idea about the digital filters first. The common idea is to sample or to describe analogue signals by sampling with a sufficiently high frequency. This can be described with a lot of mathematics. One process is the Z-Transform where the letter z in your terms come from. The letter z notes the current sample, (z-1) is the previous one and so on.

Some kinds of filters depend on some parts of the input only. The "normal" average is a quite obvious example. Other filters depend on the input but also on the history what has been evaluated so far. Both kind of filters have advantages and issues. The output of a second-order filter depends on two latest and the current  input samples and on the latest two samples of previous results. Please dig for IIR and FIR filters for more details.

Software which is close to MATLAB syntax and perfect for this kind of stuff is math/octave. I suggest to give it a try. It is by far more useful than a "normal" programming language. The code you have typed in *P*ython is  just a single line in *O*ctave with the weighting of the samples as vectors "a" and "b"  instead of single factors A, B, C and so on. There are MATLAB primers on the web which are good as a starting point. I think I gave you not all information within the few lines. But I think it should be some kind of a starting point. Fortunately the *O*ctave is available in FreeBSD  :beergrin which should make everything on-topic.


----------



## Oko (May 4, 2014)

chrbr said:
			
		

> Software which is close to MATLAB syntax and perfect for this kind of stuff is math/octave. I suggest to give it a try. It is by far more useful than a "normal" programming language. The code you have typed in *P*ython is  just a single line in *O*ctave with the weighting of the samples as vectors "a" and "b"  instead of single factors A, B, C and so on. There are MATLAB primers on the web which are good as a starting point. I think I gave you not all information within the few lines. But I think it should be some kind of a starting point. Fortunately the *O*ctave is available in FreeBSD  :beergrin which should make everything on-topic.



This post gives bad advice and is full of misinformation. Octave, Scilab and FreeMat are at best partial and not 100% compatible reimplementations of the MATLAB language interpreter. FreeMat is closest to being a 100% compatible clone of a small subset of the core MATLAB language interpreter.  However the real power of MATLAB is in its toolboxes and as such none of free reimplementations is really useful except for educational purposes.

The MATLAB interpreter and toolboxes are very, very expensive when used for real work and a decision to use MATLAB should not be made without very careful consideration. The MATLAB language is domain specific and has no equal in its simplicity for the work with matrix data structures. However that is also a major limitation of the language. My personal rule of thumb is that unless 90% of your code will consist of calling built in MATLAB functions (MATLAB is an imperative language and the object oriented part is just PR bullshit) one should not be using MATLAB. Even a single for loop in MATLAB is enough to render all MATLAB code useless.

In the long run using Python instead of MATLAB will pay handsomely except in rare circumstances. For the record at the place of my employment we use both MATLAB and Python and I am dealing with some of those rare circumstances.

As for Octave I personally have a very low opinion of the code quality which is below average already pitiful GNU code quality. I do not consider Octave a serious production level interpreter.

BSDs users do not really have a choice when it comes to MATLAB vs Python as MATLAB is only available for Linux, OS X and Windows. Worse off, MATLAB is heavily based on Java (not just the GUI but numerical libraries) which as we know doesn't run on BSDs (I mean real Oracle Java) which kills even the slightest possibility that MATLAB can run on FreeBSD for example via the Linux emulation layer. In my experience that is one of the reasons that BSDs and FreeBSD in particular are not suitable for scientific computing. For the record at the place of my employment we generally tend to use RedHat clones on computing nodes and BSDs (85% OpenBSD and 15% FreeBSD) for everything else.


----------



## fonz (May 4, 2014)

Oko said:
			
		

> BSDs and FreeBSD in particular are not suitable for scientific computing.


Scientific computing includes functional computing in Haskell. FreeBSD doesn't do that any worse than Linux.


----------



## chrbr (May 4, 2014)

Dear @Oko,

first I thank you for the pointer to *F*reeMat. I will give it a try. The new versions of *O*ctave have the *J*ava stuff as dependency which I really dislike. 


			
				Oko said:
			
		

> This post gives bad advice and is full of misinformation. Octave, Scilab and FreeMat are at best partial and not 100% compatible reimplementations of the MATLAB language interpreter. FreeMat is closest to being a 100% compatible clone of a small subset of the core MATLAB language interpreter. However the real power of MATLAB is in its toolboxes and as such none of free reimplementations is really useful except for educational purposes.


I am aware that the free reimpletations are not 100% compatible. Maybe they have been more compatible in earlier times, but nowadays *M*atlab is a huge tool which covers almost everything which is related to computing somehow. And there are lots of interfaces to tool chains of companies which work in the fields of DSPs and FPGAs for automatic code generation and so on. I guess that is even impossible to get the interface specifications to those tools. 

In my opinion the reimplementations are fine for educational purposes. And I think that this is the intention of @newbie32. I use *O*ctave for some computing which involves inverting matrices with complex elements. *O*ctave has a huge toolbox collection as well. In the past I worked with a few of them.


			
				Oko said:
			
		

> The MATLAB interpreter and toolboxes are very, very expensive when used for real work and a decision to use MATLAB should not be made without very careful consideration.


I agree.


----------



## Oko (May 5, 2014)

fonz said:
			
		

> Scientific computing includes functional computing in Haskell. FreeBSD doesn't do that any worse than Linux.


Sure enough if you are 100% certain you will be only using GCC, Python, Perl, Haskell and never need a proprietary compiler like the Portland Compiler set or MATLAB or GPU you can use FreeBSD for scientific computing. However in real life even in a small LAB like the one I work for it is hard to predict what you will need to solve the next problem and in the long run RedHat is probably a/the better choice. It boils down to the lack of vendor support for FreeBSD. Nothing I mean nothing would made me happier than waking up one day and being able to be 100% Linux free just like I am 100% Windows free :beer


----------

