Sunday, November 25, 2012

Jo soc CalalaƱ :D

Today is an historic day of elections in Catalonia. It is not the famous independence referendum, but I feel it is the time to speak up. For each side. Independentist (secessionist) have taken advantage of the crisis to get a lot of supporters. To me, it is a classical example of opportunism while proposing the wrong solution to the problem, while real solutions are urgently needed.

My problem is not with the feelings of anybody. Everybody can feel from wherever and even change those feelings during life. I start having a problem when people thinks that it is better for Catalonia, from the economic point of view. That I don't buy (surely not short/medium term), and specially when the new government would be made of the same people that drove Catalonia to this situation in the first place. Yes, the rest of Spain wasted their money, but did you guys checked what we did with ours in Catalonia???

Still, this could simply be an economical discussion, which is fine. My real problem is that the secessionist voice involves, besides bending reality, a speech of superiority and look down on other people. This smells to stuff that I simply don't like.

Below couple of post I placed in La Vanguardia, a nationalist newspaper, probably the biggest newspaper in Catalonia:


Basically, I think the different governments in Spain and its regions have proven to suck at it, so, it may be an exaggeration, but Germans may do a better job at it... Or at least strong auditing from outside.


Well, maybe not imaginary, but you understand me ;)

Peace!

PS.: By the way, if you want to know what is all this about, probably one of the most accurate articles on the topic would be this one from The economist:
http://www.economist.com/news/briefing/21567085-stabilising-spains-finances-without-tearing-its-social-fabric-apart-being-made-harder

Sunday, November 18, 2012

Filter design: From Matlab to C code

Note 1:  For this, I am using not only Matlab but the Signal Processing Toolbox. Got to study the other tools that Matlab has to deal with filters...
Note 2: I should write something like this but with some free tool out there... Anyhow, for the moment just use Matlab, as I have it available in my company...

Quick reading, it looks like there are couple of methods:
  1. A=ellip(specs) --> H=dfilt.df1(A) where A are the numerator and denominator coeff. This is kind of going analog to then move to digital.
  2. D=fdesign.bandpass(specs) --> H=design(D, ellip)
H is the object describing the digital filter (not exactly the coefficients, see more below). We will look in detail and extend the method 2 following Getting started with discrete time filters (dfilt) or fdesign objects.
1/ First got to get the object describing the specification of the filter we want. So, we use fdesign. Just type  "fdesign.bandpass" for a very simple syntax, or go to fdesign for more info.
In our case, we want to create a bandpass filter around 2.5KHz. So, we will do:

>> D=fdesign.bandpass(2000,2200,2800,3000,40,1,40,44100)
And we get:
D =
               Response: 'Bandpass'                     
          Specification: 'Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2'
            Description: {7x1 cell}                     
    NormalizedFrequency: false                          
                     Fs: 44100                          
                 Fstop1: 2000                           
                 Fpass1: 2200                           
                 Fpass2: 2800                           
                 Fstop2: 3000                           
                 Astop1: 40                             
                  Apass: 1                              
                 Astop2: 40

2/ To get the real design, we use:
>> h=design(D,'ellip')
And get:
h =
         FilterStructure: 'Direct-Form II, Second-Order Sections'
              Arithmetic: 'double'                              
               sosMatrix: [4x6 double]                          
             ScaleValues: [5x1 double]                          
     OptimizeScaleValues: true                                  
        PersistentMemory: false
For help, type designmethods(h) to get a list of filters.

3/ At this moment, we may actually like a different structure. Notice it used the default one (direct form II)


but we may want the more standard one. I am going to use direct form I as it is easier for me to identify what output number below matches what coefficient (notice that I am learning while doing this), so, it'll be easier to program:

Note: notice the error on the - sign in the added of b(nb) path.

So, we do:
>> h1  = convert(h,'df1')

h1 =

     FilterStructure: 'Direct-Form I'
          Arithmetic: 'double'      
           Numerator: [1x9 double]  
         Denominator: [1x9 double]  
    PersistentMemory: false

To see what we get, we can do:
>> info(h1)
Discrete-Time IIR Filter (real)   
-------------------------------   
Filter Structure    : Direct-Form I
Numerator Length    : 9           
Denominator Length  : 9           
Stable              : Yes         
Linear Phase        : No

4/ At this point, if we want to see what we got, it is as simple as using the Filter Visualization Tool:
hfvt=fvtool(h1);


5/ To get the coefficients, use:
num = get(h1,'Numerator');
den = get(h1,'Denominator');

In our case:
num =     0.0099   -0.0729    0.2418   -0.4681    0.5789   -0.4681  0.2418   -0.0729    0.0099
den =     1.0000   -7.4222   24.5776  -47.3746   58.1077  -46.4331  23.6104   -6.9884    0.9229

Where numerator and denominator are (when a0=1, like in our case):


In this kind of structure

Notice that a0 in Matlab is den(1) as the index starts at 1. So, we create in Matlab a piece of code that will do this (equivalent to running y=filter(h1,rx)):

rx2=[zeros(length(num)-1,1);rx];
y=zeros(length(rx)+length(num)-1,1);
for R=length(num):length(rx2)
    data=0;
    for N=1:length(num)
        data=data+rx2(R-N+1)*num(N);
    end
    for D=1:(length(den)-1)
        data=data-y(R-D)*den(D+1);
    end
    y(R)=data/den(1);
end

Translating this into C is straightforward. Notice that the initialization is important. I.e., getting both, input and output past values equal to zero. We can do that adding those in front of our sequences/arrays, or, just make zero the first set of values on those arrays. Of course, this is just for test and assuming you don't care about that data being lost... Either way, you get the point :)
 
The plot on the left is the result using "filter" while the one on the right is the one from the "C" code.


Other useful information:

For an automatic way to do it: Forum describing how to do it
From Simulink to C
From Matlab to HDL
Real Time Workshop Embedded Coder

Matlab filter design
Designing Low Pass FIR filters
FDATool

To use your filter (DUH!): y=filter(num,den,x);
To see the impulse response of your filter use [h,t] = impz(num,den) and plot(t,h)
To check if it is stable: isstable(h) - Note: I didn't get it to work where I can check it with filter running in a given precision

Nice write up on filter theory: http://www.mikroe.com/chapters/view/73/chapter-3-iir-filters/
And another one...
And one more!

Saturday, November 10, 2012

On abortion

Note: I wrote this before the election. I finally never went to vote...

====


So, I am ready to vote. I feel I would vote Obama because some of the things that Romney said I can't just agree to (like military spending can't be reduced, his healthcare policies,...). There are likely more things that Romney said that I disagree to, than that Obama said. Yeah, maybe I should be looking at what I agree, not to what I disagree :). But in one particular topic that I can't just ignore, abortion, both actually agree that morally is wrong. So, the difference is in what they do about it (one would put laws against while the other wouldn't).
http://2012.candidate-comparison.org/?compare=Romney&vs=Obama&on=Abortion

Somehow then, I started researching the topic, read posts, arguments, etc.. pro and against abortion. Not a single topic on pro-choice made the cut (we could review them here but that's probably for a different post), and end-up on the page of Ron Paul (who I wouldn't necessary agree on other topics) but who I think expressed my arguments with the exact words I would use:
"In the 1960s when abortion was still illegal, I witnessed, as an OB/GYN resident, the abortion of a fetus that weighed approximately 2 pounds. It was placed in a bucket, crying and struggling to breathe, and the medical personnel pretended not to notice. Soon the crying stopped. This harrowing event forced me to think more seriously about this important issue. That same day in the OB suite, an early delivery occurred and the infant boy was only slightly larger than the one that was just aborted. But in this room everybody did everything conceivable to save this child's life. My conclusion that day was that we were overstepping the bounds of morality by picking and choosing who should live and who should die. There was no consistent moral basis to the value of life under these circumstances. Some people believe that being pro-choice is being on the side of freedom. I've never understood how killing a human being, albeit a small one in a special place, is portrayed as a precious right."

See http://www.issues2000.org/tx/Ron_Paul_Abortion.htm

I believe that one day we will see the 40M abortions performed in US since Roe V. Wade in 1970 (and of course, all others in the rest of the world), like we see today slavery or many other horrible things humans defended, allowed, promote, or simply choose to ignore. This is not a post against those women that performed an abortion, but against a society that didn't do everything they can (including helping and respecting those moms) to protect that baby. I can't vote somebody who wouldn't put a stop to it, so, I can only vote blank :(. And just to be less negative, my deepest respect and kudos to those single moms out there.

PS.: Just so we are clear, I would agree with the argument that prohibition is not the "magic" solution to it, and that many other policies that governments also implement to reduce the number of abortions are more effective, but we can't just say "it's ok".