butterworth filter, does not work selectively
7 views (last 30 days)
Show older comments
Hello MATLAB users,
I'm a relatively new user of MATLAB. This may be a simple question, but it seems to be beyond my current skills and knowledge. I much appreciate it if anyone can give me some thought.
I have access to two MATLABs. One at home (student version); the other at work (a different version). I'm working on the butterworth filter. Somehow, it works at work, but it doesn't at home.
Here are two ways that 'butter' doesn't work only at home.
1. It gives an error, and this happens when the order is 2, 3, 4, etc. % eg. I want to make a 2nd order lowpass butter filter at 1k Hz srate = 48000; [b,a] = butter(2, 1000/srate/2);
??? Attempted to access den(3); index out of bounds because numel(den)=2.
Error in ==> zp2ss at 134 a1 = t\[-den(2) -den(3); 1 0]*t;
Error in ==> butter at 73 [a,b,c,d] = zp2ss(z,p,k);
2. It does not give me an error but the filter does not seem to filter. eg. I want to lowpass white noise at 1k Hz deltat = duration/1000; srate = 48000; t = linspace(0, deltat, deltat*srate); noise = randn(1, length(t)); [b,a] = butter(1, 1000/srate/2); % this gives me a = b = 6.4670 new_noise = filter(b,a,noise); wavplay(new_noise, srate) % this sounds just like wavplay(noise, srate)
My further observation
1. If I make the same lowpass filter by using Filter Design & Analysis Tool box at home, I can successfully make this lowpass filter. 2. For both cases, I have no trouble using the 'butter' function at work.
Thank you very much for your thought. -Osamu
0 Comments
Accepted Answer
Daniel Shub
on 4 Mar 2012
From the first error it seems you have the correct butter and zp2ss functions on the path. The error occurs on line 134 of zp2ss. Line 134 is group inside a while loop (i < np) starting on line 129. It appears that i will be at least 1 when you get to line 129. My guess is that np is the number of poles and that with a 1st order filter you never end up in this loop.
Looking at the code of zp2ss, specifically where den on line 134 comes from, it appears that den comes from line 130 which depends on the functions "real" and "poly". The "real" function is a built-in function and you would probably know if you overloaded this. I would check
which poly
and hopefully you get something like: .../toolbox/matlab/polyfun/poly.m
2 Comments
Daniel Shub
on 4 Mar 2012
First, don't overload MATLAB functions names with either variable or functions of your own. MATLAB doesn't make this easy, but as you can see it can cause odd and hard to track down problems. The which command will let you deal see if your proposed name is in use for the toolboxes you have.
Many of the MATLAB functions are open source. For these functions you can see the content (e.g., with type or edit)
More Answers (3)
Wayne King
on 3 Mar 2012
Hi Osamu, Is it possible that you have a non-MathWorks version of zp2ss at home?
If you enter
>>which zp2ss
what path is returned?
Is it something like:
matlab\toolbox\shared\controllib\general\zp2ss.m
My suspicion is that you have another zp2ss.m which precedes the MathWorks' version on your path.
Wayne King
on 4 Mar 2012
Hi Osamu, That is the correct path for R2011a. I'm wondering if you have some other function which is not a MathWorks' function (perhaps something being called by zp2ss.m, which calls several functions).
Can you try something:
Restart MATLAB and make sure you do not have any added paths where you may have your own .m files, or .m files you have obtained from some non-MathWorks source.
You can examine these paths with:
>>path
I have no problem with this line in R2011a:
srate = 48000; [b,a] = butter(2, 1000/srate/2);
Osamu Takai
on 4 Mar 2012
3 Comments
Daniel Shub
on 4 Mar 2012
MATLAB has a complicated rule about what functions it uses. Eventually, it gets to the search path (after checking the current workspace and current directory). You can see the search path with the command path.
See Also
Categories
Find more on Filter Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!