2

I'm trying to use the filtfilt command with matlab coder but I am running into trouble.

If I run the program the normal way everything runs fine, but when I try to build for c-code the following error message is displayed: "Data must have length more than 3 times filter order". I read somewhere that this may be because the filtfilt function need to know the entire signal (over time) and this may be incompatible with matlab coder. This however seems incorrect as matlab lists filtfilt as a supported function for Code generation - http://www.mathworks.se/help/toolbox/signal/ug/br7exek-1.html

My call is:

y_filt=filtfilt(b,a,y);

My variables just before the call have the following properties:

Name Size .......... Bytes ...... Class

a ...... 1x9 ........... 72 ........... double
b ...... 1x9 ........... 72 ........... double
y ...... 1499400x1 11995200 double

Does anyone have a suggestion on how to resolve this problem?

macduff
  • 4,655
  • 18
  • 29
Michael Ward
  • 213
  • 5
  • 12
  • It would seem you have proven that it is not the `filtfilt` function, can you give more of the code or more error description? – macduff Feb 25 '12 at 23:46
  • Well I have a hard time understanding what else it can be. To get (b,a) a butter is performed just before [b,a]=butter(8,0.045,high). – Michael Ward Feb 26 '12 at 17:25

2 Answers2

1

I understand your frustration. I think the problem is due to the fact that in order to use filtfilt you pre-allocate a massive amount of data. When I try a similar operation my machine, which is considered a server class, runs out of memory. I would recommend looking at your requirements and make sure that filtfilt is the only function that will meet them. I realize that the zero-phase property of the filtfilt is perfect for distribution in Matlab Coder, however the memory requirements may give you more headaches still. Let me know if you wish to discuss other alternatives. Thanks!

macduff
  • 4,655
  • 18
  • 29
  • **Thanks for your answer!** What I have tried to do instead is to use `filter()` in one direction. Then `flipud()`. Then I `filter()` again and lastly `flipud()`. I also _pad with zeros_ for twice the longest of my return from butter (a or b) as a first step and _remove what I have added to the length_ as a last step. **What are the problems with this solution as you see it?** – Michael Ward Feb 27 '12 at 20:47
  • I think that sounds fine. Frustrating that `filter` would work and not `filtfilt`, but as long as the output is as you expect. I came across this http://www.scipy.org/Cookbook/FiltFilt, I know it's for Python, but I think it will still be of some value. – macduff Feb 27 '12 at 21:34
  • **I have finally solved it** so that I can run `filtfilt()`! The problem was that I had not specified that the wave file had a length larger than a or b. This means that matlab coder does not see the length even if it is evident by running `whos`. However a new problem has arrived as I do not know how to specify constants in the code. `coder.constant` does not work. I have asked the separate questions at this link [constants-and-matlab-coder](http://stackoverflow.com/questions/9467573/constants-and-matlab-coder). **Do you know anything about this as well?** – Michael Ward Feb 29 '12 at 15:52
  • I am not very familiar with matlab-coder, more so with embedded M, but I think they work much the same. I'm thinking there's no way around your problem there. However, I would recommend calling Mathworks tech support, I know it's a pain, and alerting them to the problem. – macduff Feb 29 '12 at 16:11
  • What I see that they do internally is to call `eml_const` though I don't get if that is something that could be used with Matlab Coder as well. My workaround which is not satisfying is to work with if-statements. If I find that the input is 7, I create a `tmp = 7` which becomes a constant. Feels really weird.. – Michael Ward Feb 29 '12 at 17:53
0

you use a Buuterwroth-Filter here. This is an IIR filter. The filter order that is asked by can be much higher than the number of filter tabs. And the signal length should be three times the filter order, not the filter length!

Regards, Tchekov