butterworth filter, does not work selectively

7 views (last 30 days)
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

Accepted Answer

Daniel Shub
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
Osamu Takai
Osamu Takai on 4 Mar 2012
Hello Daniel, thank you for your feedback.
1. First of all, you solved my problem. 'which poly' gave me a very difference source, which I created a long before and stored somewhere else. I deleted this m.file, and now everything works!
2. I thought that I cannot see the contents of built-in functions, but you seem to have a quite good grasp on understanding the contents of these functions. Could you help me learn such skills?
cheers, -Osamu
Daniel Shub
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)

Sign in to comment.

More Answers (3)

Wayne King
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.
  1 Comment
Osamu Takai
Osamu Takai on 3 Mar 2012
Hi Wayne, Thank you for your comment. I tried it out, and I got:
C:\Program Files\MATLAB\R2011a Student\toolbox\matlab\polyfun\zp2ss.m
Is it an okay path? (It does so to me). - Osamu

Sign in to comment.


Wayne King
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);
  1 Comment
Osamu Takai
Osamu Takai on 4 Mar 2012
Hi Wayne. Thank you again for your help.
It appears that the problem is something else. I performed 'pass' in a brand new, blank current folder, which listed all MathWorks paths. I also ran 'butter' function, which gives me the error that I described in my original question.
But I wasn't familiar with 'path' at all and your comments helped me become aware of it and learn about it.
So big thanks, -Osamu

Sign in to comment.


Osamu Takai
Osamu Takai on 4 Mar 2012
Big thank you to Daniel, who helped me identify my problem. But what was happening seems to be what Wayne is suggesting as well. A completely different poly function that I made before was getting in the way. So big thank you to Wayne, too.
Now, the 'poly' function that I made was stored in a folder that is different from my 'current folder' where I was doing 'butter' function. I thought that MATLAB uses user-defined functions that are only in the current folder. But it seems I was wrong. For my better programming, would you guys and anyone else have any advice?
thanks, -Osamu
  3 Comments
Daniel Shub
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.
Osamu Takai
Osamu Takai on 5 Mar 2012
Thank you for all of your advice and techniques. I'll keep them in mind from now on.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!