Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[sos,g] with a Matlab IIR Filter

287 views
Skip to first unread message

Zach

unread,
Nov 30, 2011, 9:18:08 AM11/30/11
to
Ello all,

I'm running into a bit of a problem with getting a filter function to work properlly. A snippet of the function is as follows:

function [ coeffType,z,p,k,sos,g] = iirTesting( type, TargetFrequency, order, Fs )
N=order;
%LOW PASS IIR%%%%%%%%%%%%%%%%%%%%%
if strcmp(type,'lpiir')%low pass iir
coeffType = 'iir';
Fc = TargetFrequency; % Cutoff Frequency
h = fdesign.lowpass('N,F3dB', N, Fc, Fs);
TheFilter = design(h, 'butter');
[z p k]=butter(order,Fc/4000,'low');
[sos,g]=zp2sos(z,p,k);
end
sos(:,1)=round(sos(:,1));
sos(:,2)=round(sos(:,2));
sos(:,3)=round(sos(:,3));
sos(:,4)=round(sos(:,4));

end

Basically I'm making a function that performs some of the basic operations of the fdatool. Because of the project I am working on and the requirement for continuous operation while the order and target frequency change I cannot stop to re-do the FDAtool each operation. So I extract the functions used by the FDATool and for the most part have had success (Mostly with the FIR portion of my project)

Now when I do the IIR portion, I require the [sos,g] format for the filter coefficients. What I have managed to get so far is an almost complete match for the sos, but for the g portion I only get a single number as a result where as the FDAtool provides me with an array of numbers. I'll show you what I mean:

From the above function (using a target frequency of 500, an order of 10 and an Fs of 8000):
sos:
1 2 1 1 -1.34 0.45
1 2 1 1 -1.38 0.49
1 2 1 1 -1.45 0.57
1 2 1 1 -1.57 0.70
1 2 1 1 -1.74 0.89

g: 2.73443806791476e-08

From the FDATool using the same settings:
sos:
1 2 1 1 -1.74 0.89
1 2 1 1 -1.57 0.70
1 2 1 1 -1.45 0.57
1 2 1 1 -1.38 0.49
1 2 1 1 -1.34 0.45

g:
0.0359104585250480
0.0324266085022054
0.0299545822080925
0.0283825411844721
0.0276204703221808
1


Now as I typed this up I realized that my two sos values are identical I just need to flipud the one from my function. However you will see that the g value from my function is nothing close to the g value from the FDAtool. All of the research I have done on the function and subfunctions has led me nowhere so now I need some more direct help: Can anybody tell me how to get the array of g values?

Steven_Lord

unread,
Nov 30, 2011, 9:23:23 AM11/30/11
to


"Zach " <litz...@pct.edu> wrote in message
news:jb5dv0$hig$1...@newscl01ah.mathworks.com...
> Ello all,
>
> I'm running into a bit of a problem with getting a filter function to work
> properlly. A snippet of the function is as follows:

*snip*

> Basically I'm making a function that performs some of the basic operations
> of the fdatool. Because of the project I am working on and the
> requirement for continuous operation while the order and target frequency
> change I cannot stop to re-do the FDAtool each operation. So I extract
> the functions used by the FDATool and for the most part have had success
> (Mostly with the FIR portion of my project)

If you're doing the same filtering operation multiple times, are you using
the code generation functionality in FDATOOL to help you generate your code?

http://www.mathworks.com/help/toolbox/signal/gs/f0-41455.html

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Rune Allnor

unread,
Nov 30, 2011, 9:28:14 AM11/30/11
to
On 30 Nov, 15:18, "Zach " <litza...@pct.edu> wrote:

> Basically I'm making a function that performs some of the basic operations of the fdatool.  Because of the project I am working on and the requirement for continuous operation while the order and target frequency change I cannot stop to re-do the FDAtool each operation.

What kind of system is this? Filter design is
a one-off, which is done once before the system
becomes operational. The coefficients of an IIR
filter don't change throughout the lifetime of
the system.

If your system indeed requires the filter coefficients
to change dynamically, the IIR filter is the wrong tool
to use. In that case you need an adaptive filter.

Which is a totally different beast.

Rune

Zach

unread,
Nov 30, 2011, 9:32:10 AM11/30/11
to
"Steven_Lord" <sl...@mathworks.com> wrote in message <jb5e8r$in1$1...@newscl01ah.mathworks.com>...
> If you're doing the same filtering operation multiple times, are you using
> the code generation functionality in FDATOOL to help you generate your code?
>
> http://www.mathworks.com/help/toolbox/signal/gs/f0-41455.html
>
> --
> Steve Lord
> sl...@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

It isn't going to be as simple as the same operation multiple times. The input values (Fs, Type, order, TargetFrequency) will be changed as the program runs. The snippet is only for the Low Pass IIR but the code will be handling Highpass, Lowpass, Bandpass and Bandstop FIR and IIR filters.

If needed I can post up my entire code for the project but I only wanted to ask for advice on that snippet of code since that is the only real issue I am having.

As a note I thought of after posting my original message, I have also tried the
[A B C D]=butter(order,Fc/4000,'low');
[sos,g]=ss2sos(A, B, C, D,'down');
format before the zpk format but the results were the same.

Zach

unread,
Nov 30, 2011, 9:34:08 AM11/30/11
to
Rune Allnor <all...@tele.ntnu.no> wrote in message <bd26e8c4-d49f-49a9...@g7g2000vbd.googlegroups.com>...
This is a comparative system where I am using Matlab to generate a simple waveform of a randomly selected frequency range and sending the coefficients out to an HC12 microcontroller. The HC12 uses the waveform data and the coefficients to filter the data while Matlab does the same thing. I then graph the output of the filters over the given range of frequencies and compare the HC12 and Matlab results to see which one is the better filtering tool

Rune Allnor

unread,
Nov 30, 2011, 9:45:11 AM11/30/11
to
On 30 Nov, 15:34, "Zach " <litza...@pct.edu> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <bd26e8c4-d49f-49a9-8916-02416d8fb...@g7g2000vbd.googlegroups.com>...
Why?

Since the same coefficients are used in both places,
any differences will be down to factors like internal
numerical formats and filter structures.

Matlab will likely score high on numerics, with its
double-precision floating point format versus either
a single precision FP system, or even a fixed-point
integer number format.

A specialist filtering tool will likely take some
of that disadvantage back by utilizing realization
structures that have better numerical properties than
the naive straight-forward stuff matlab uses.

So you are investing a lot of effort in getting data
that will tell you nothing about the questions you have,
and where the answers to those questions are readily
available in the standard literature and have been so
for several decades.

Rune

Zach

unread,
Nov 30, 2011, 10:01:08 AM11/30/11
to
Who am I to reason why a college professor would assign such a project? In any case it is due next week and aside from the IIR portion, it is ready for presentation. I would be at a loss to try and remake it at this point.

Rune Allnor

unread,
Nov 30, 2011, 10:09:13 AM11/30/11
to
On 30 Nov, 16:01, "Zach " <litza...@pct.edu> wrote:
> Who am I to reason why a college professor would assign such a project?  In any case it is due next week and aside from the IIR portion, it is ready for presentation.   I would be at a loss to try and remake it at this point.

In that case I'd have a word with the dean or the headmaster
about tuition fees.

Rune

Rune Allnor

unread,
Nov 30, 2011, 10:47:01 AM11/30/11
to
Ouch! Grammar matters, so I'll rephrase:

If I were you, I would have had a word with the
dean or headmaster about tuition fees.

Rune

Zach

unread,
Nov 30, 2011, 10:58:08 AM11/30/11
to
I will take that advice to heart but it still does not help me extract the proper g array which I require.

Rune Allnor

unread,
Nov 30, 2011, 11:30:41 AM11/30/11
to
On 30 Nov, 16:58, "Zach " <litza...@pct.edu> wrote:
> I will take that advice to heart but it still does not help me extract the proper g array which I require.

Once upon a time I implemented a flter design tool for
IIR filters, much like FDATool.

When representing the filter on biquad (i.e. SOS) form,
you need to supply one overall gain factor. IF FDAtool
works in similar ways as the thing I implemented,
you only need that one g factor. One might get
several g factors as intermediate results, depending
on exactly how the computations are done, but one
only needs one such factor.

If I am right, you will find the relation between
the vector gv from your method 1 and the scalar g
from your method 2 to be

g == prod(gv)

If the factor g is very large or very small, you
could distribute it across the N biquads as

gn = g^(1/N).

You want a gain factor close to 1, for numerical
reasons.

Rune

Zach

unread,
Nov 30, 2011, 1:40:09 PM11/30/11
to
Rune Allnor <all...@tele.ntnu.no> wrote in message <0a073bc4-8e92-49fd...@p2g2000vbj.googlegroups.com>...
After testing and slamming my head against the wall this method works fantastically. Thank you very much Rune.
0 new messages