DSPRelated.com
Forums

Filter Analysis with DFT ?

Started by Robert A. April 19, 2006
Hi guys,

I'm new at this and still learning. I want to examine (graph) the amplitude 
vs. frequency response of standard IIR filters (one-pole, two-pole, etc.) Is 
it correct to pass a delta impulse through the filter, do a DFT, then look 
at the results ?

I've done it like this and the graphs look right but I'm not really sure:

(pseudo code)

// create delta impulse

impulse={1,0,0,0,0,0,0,0,...};

// create the result

for(i=0;i<num_samples;i++){
 data[i]=the_filter(impulse[i],filter_state);
}

// DFT

fourier(data,...);

...then I graph the real array returned from the DFT. For a low-pass and 
high-pass I get the knee-shape in the [0,1] interval so it looks right but 
I'm not really sure.

Also, I'm using the FFT from Numerical Recipes in C and I don't understand 
the data[n+1] and data[n+2] return values, it says "...contain the real and 
imaginary parts of the one aliased point that contains the most positive and 
the most negative frequency." Should I just ignore that and not graph it ? 
I'm using the complex version for now (setting the imaginary part to 0.0) 
until I fully understand the code.

And, maybe I should scale the y-axis of my graph to get a better 
understanding of what's happening ?

I'm using and learning DSP and filters for software music synthesizers I 
program as a hobby. I can just use existing filter code but I like to know 
what's going on in there and I also hope to create my own unique filters one 
day.

Thanks.




Robert A. skrev:
> Hi guys, > > I'm new at this and still learning. I want to examine (graph) the amplitude > vs. frequency response of standard IIR filters (one-pole, two-pole, etc.) Is > it correct to pass a delta impulse through the filter, do a DFT, then look > at the results ? > > I've done it like this and the graphs look right but I'm not really sure: > > (pseudo code) > > // create delta impulse > > impulse={1,0,0,0,0,0,0,0,...}; > > // create the result > > for(i=0;i<num_samples;i++){ > data[i]=the_filter(impulse[i],filter_state); > } > > // DFT > > fourier(data,...); > > ...then I graph the real array returned from the DFT. For a low-pass and > high-pass I get the knee-shape in the [0,1] interval so it looks right but > I'm not really sure.
You *can* do things like that, but there is another way. It turns out that it is possible to express an arbitrary rational filter function (a filter with poles and zeros) as a cascade of one all-pole and one FIR filter. What you end up with is H(w) = B(w)/A(w) = FFT{bv}/FFT{av} where av is the vetor of feedback coefficients and bv the feed-forward coeffisinets in the filter.
> Also, I'm using the FFT from Numerical Recipes in C and I don't understand > the data[n+1] and data[n+2] return values, it says "...contain the real and > imaginary parts of the one aliased point that contains the most positive and > the most negative frequency." Should I just ignore that and not graph it ?
While that book is brilliant for learning new stuff (somehow, the authors manage to get lots of material expressed in only a few pages), I never used code from that book. I *think* they meant that an array of complex numbers is organized somethink like this: Array = [(real,imag),(real,imag),...,(real,imag))] that the real and imaginary parts of number n occupy places 2n and 2n+1 in memory. Maybe somebody who used the code from the book might confirm that.
> I'm using the complex version for now (setting the imaginary part to 0.0) > until I fully understand the code.
The only place where you can set the imaginatry parts to 0 is in the input buffer if you feed real-valued data to the routine. Everywhere else, you need to stick with the complex numbers.
> And, maybe I should scale the y-axis of my graph to get a better > understanding of what's happening ?
Some people use the scaling 20*log10(abs(X(w)))
> I'm using and learning DSP and filters for software music synthesizers I > program as a hobby. I can just use existing filter code but I like to know > what's going on in there and I also hope to create my own unique filters one > day.
Lots of people have started out with that, lately. Get yourself a copy of Lyons: "Understanding Digital Signal Processing", 2nd ed., Prentice-Hall, 2004. It will take you through the basics, as it is written with the complete beginner in mind. You will also get far better help here with your own copy available. It is easier to elaborate on what that book says, than describing something completely from scratch. Rune
Robert A. wrote:

> Hi guys, > > I'm new at this and still learning. I want to examine (graph) the amplitude > vs. frequency response of standard IIR filters (one-pole, two-pole, etc.) Is > it correct to pass a delta impulse through the filter, do a DFT, then look > at the results ? > > I've done it like this and the graphs look right but I'm not really sure: > > (pseudo code) > > // create delta impulse > > impulse={1,0,0,0,0,0,0,0,...}; > > // create the result > > for(i=0;i<num_samples;i++){ > data[i]=the_filter(impulse[i],filter_state); > } > > // DFT > > fourier(data,...); > > ...then I graph the real array returned from the DFT. For a low-pass and > high-pass I get the knee-shape in the [0,1] interval so it looks right but > I'm not really sure. > > Also, I'm using the FFT from Numerical Recipes in C and I don't understand > the data[n+1] and data[n+2] return values, it says "...contain the real and > imaginary parts of the one aliased point that contains the most positive and > the most negative frequency." Should I just ignore that and not graph it ? > I'm using the complex version for now (setting the imaginary part to 0.0) > until I fully understand the code. > > And, maybe I should scale the y-axis of my graph to get a better > understanding of what's happening ? > > I'm using and learning DSP and filters for software music synthesizers I > program as a hobby. I can just use existing filter code but I like to know > what's going on in there and I also hope to create my own unique filters one > day. > > Thanks. > > > >
Get a copy of SciLab, from www.scilab.org. One of the folks I recommended it to said "it's about as user-friendly as a chain-saw" -- I thought he was being very tolerant. But it's way powerful. In your case, you can give it the z-domain transfer function of a filter and it'll generate a frequency plot, without having to go through the FFT business (Rick Lyon's book will cover doing this in chapter 6 or so). -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/
Hi Rune,

I just ordered the Lyons book, I should have it in a few days.

I won't ask anymore questions until after I get it, I'm getting excited.

Thanks for the help.

R.

PS - I wish I lived in Norway


"Rune Allnor" <allnor@tele.ntnu.no> wrote in message 
news:1145419656.285562.319670@i39g2000cwa.googlegroups.com...
> > Robert A. skrev: >> Hi guys, >> >> I'm new at this and still learning. I want to examine (graph) the >> amplitude >> vs. frequency response of standard IIR filters (one-pole, two-pole, etc.) >> Is >> it correct to pass a delta impulse through the filter, do a DFT, then >> look >> at the results ? >> >> I've done it like this and the graphs look right but I'm not really sure: >> >> (pseudo code) >> >> // create delta impulse >> >> impulse={1,0,0,0,0,0,0,0,...}; >> >> // create the result >> >> for(i=0;i<num_samples;i++){ >> data[i]=the_filter(impulse[i],filter_state); >> } >> >> // DFT >> >> fourier(data,...); >> >> ...then I graph the real array returned from the DFT. For a low-pass and >> high-pass I get the knee-shape in the [0,1] interval so it looks right >> but >> I'm not really sure. > > You *can* do things like that, but there is another way. It turns out > that > it is possible to express an arbitrary rational filter function (a > filter with > poles and zeros) as a cascade of one all-pole and one FIR filter. > > What you end up with is > > H(w) = B(w)/A(w) = FFT{bv}/FFT{av} > > where av is the vetor of feedback coefficients and bv the feed-forward > coeffisinets in the filter. > >> Also, I'm using the FFT from Numerical Recipes in C and I don't >> understand >> the data[n+1] and data[n+2] return values, it says "...contain the real >> and >> imaginary parts of the one aliased point that contains the most positive >> and >> the most negative frequency." Should I just ignore that and not graph it >> ? > > While that book is brilliant for learning new stuff (somehow, the > authors > manage to get lots of material expressed in only a few pages), I never > used code from that book. > > I *think* they meant that an array of complex numbers is organized > somethink like this: > > Array = [(real,imag),(real,imag),...,(real,imag))] > > that the real and imaginary parts of number n occupy places 2n and > 2n+1 in memory. > > Maybe somebody who used the code from the book might confirm that. > >> I'm using the complex version for now (setting the imaginary part to 0.0) >> until I fully understand the code. > > The only place where you can set the imaginatry parts to 0 is in > the input buffer if you feed real-valued data to the routine. > Everywhere else, you need to stick with the complex numbers. > >> And, maybe I should scale the y-axis of my graph to get a better >> understanding of what's happening ? > > Some people use the scaling > > 20*log10(abs(X(w))) > >> I'm using and learning DSP and filters for software music synthesizers I >> program as a hobby. I can just use existing filter code but I like to >> know >> what's going on in there and I also hope to create my own unique filters >> one >> day. > > Lots of people have started out with that, lately. Get yourself a copy > of > > Lyons: "Understanding Digital Signal Processing", 2nd ed., > Prentice-Hall, 2004. > > It will take you through the basics, as it is written with the complete > beginner in mind. You will also get far better help here with your own > copy available. It is easier to elaborate on what that book says, > than describing something completely from scratch. > > Rune >
Robert A. wrote:

> Hi Rune, >... > PS - I wish I lived in Norway
MO better than no ;} [at least our sun goes down at night ;] Sorry Rune, I couldn't resist. Our woodpeckers can differentiate between trees&chimneys - hi Jerry (can anyone tell I've been up 18+ hours ;/
Richard Owlett wrote:
> Robert A. wrote: > >> Hi Rune, >> ... >> PS - I wish I lived in Norway > > > MO better than no ;} > > [at least our sun goes down at night ;] > Sorry Rune, I couldn't resist. > Our woodpeckers can differentiate between trees&chimneys - hi Jerry > (can anyone tell I've been up 18+ hours ;/
My woodpeckers use metal chimneys and rain gutters as signaling drums. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Robert A. skrev:
> Hi Rune, > > I just ordered the Lyons book, I should have it in a few days. > > I won't ask anymore questions until after I get it, I'm getting excited.
Eh... I hope I wasn't to harsh with you. By all means, ask questions with or without that book.
> Thanks for the help. > > R. > > PS - I wish I lived in Norway
Living in this country requires something of an aquired taste. Right now it's 'normal': Little snow left on the ground, temperatures in the +5C -- +15C range, the sun is up during day and sets at night... Actually, the sun is one of the more cumbersome aspects of living here, but maybe not for the reasons you might think. In the north, there is the midningt sun during summer, which means that the sun doesn't set but slides just above the horizon at night. In winter, the sun is gone, which means that it doesn't rise high enough to get above the horizon at day. But around November and again in February, it *just* climbs above the horizon for a few hours around noon. The main axis of this country is oriented in the north--south direction. So most of the main roads go north-south. The sun hovering just above the horizon, in winter in south in daytime, in summer in the north at night, makes it very awkward to drive a car. One just doesn't see much, driving straight into the sun. Apart from that, lots of wildlife around. A couple of weeks ago, two mooses came strolling through the garden some 4 m from the house. It was a cow with her yearlng calf. There was a little snow left at the time, so I went to see the tracks they left. The tracks were fascinating. The female had put her hind feet exactly wher her front feet had been. The calf had tried to walk exactly in her tracks. Very fascinating. Mooses around the house is an "unusual but not rare" event. It can be a bit scary, though, as a female moose protecting her calf ranks among the top 3 on the list of more unpleasant wildlife experiences one can have around here. Rune
Robert A. schrieb:

> Hi guys, > > I'm new at this and still learning. I want to examine (graph) the amplitude > vs. frequency response of standard IIR filters (one-pole, two-pole, etc.) Is > it correct to pass a delta impulse through the filter, do a DFT, then look > at the results ? > > I've done it like this and the graphs look right but I'm not really sure: > > (pseudo code) > > // create delta impulse > > impulse={1,0,0,0,0,0,0,0,...}; > > // create the result > > for(i=0;i<num_samples;i++){ > data[i]=the_filter(impulse[i],filter_state); > } > > // DFT > > fourier(data,...); > > ...then I graph the real array returned from the DFT. For a low-pass and > high-pass I get the knee-shape in the [0,1] interval so it looks right but > I'm not really sure.
There are two issues here: 1. You want to plot the magnitude and not the real part of the DFT of the impulse response. 2. A good value for num_samples (the length of the impulse response) depends on the filter. If you insist on using the DFT to plot the magnitude response of the filter, use Rune's method of taking the DFT of the zero-padded coefficients. Regards, Andor