Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
filter

Subject: filter

From: Fernando

Date: 5 Aug, 2011 19:26:10

Message: 1 of 6

hi
i want to do a algorithm in language c that realize the filter function of matlab.

Subject: filter

From: Reese

Date: 5 Aug, 2011 20:29:27

Message: 2 of 6

"Fernando" wrote in message <j1hg4i$9mk$1@newscl01ah.mathworks.com>...
> hi
> i want to do a algorithm in language c that realize the filter function of matlab.

Sounds like you want mex files.

http://www.mathworks.com/support/tech-notes/1600/1605.html

Subject: filter

From: Jan Simon

Date: 5 Aug, 2011 21:08:10

Message: 3 of 6

Hi Fernando

> i want to do a algorithm in language c that realize the filter function of matlab.

I've implemented a direct form II transposed filter as described in "doc filter" as C-Mex:
  http://www.mathworks.com/matlabcentral/fileexchange/32261-filterm
The results equal Matlab's FILTER exactly (when compiled with MSVC), but it is 60% faster than the Matlab 2009a version. I was very surprised, because I'm using a naive method without SSE or assembler tricks. For small parameter vectors (up to 8 elements) I've unrolled the loops and win further 10% speed.
Since my FilterM uses a single thread only, the multi-threaded FILTER of Matlab 2011a may handle arrays faster - I cannot test this currently.

For SINGLE input, the intermediate values are accumulated as DOUBLEs for a higher accuracy. This is remarkably more stable without loosing too much speed.

I cannot imagine what TMW inserted in the algorithm to implement it slower, but I'm very curious.

Kind regards, Jan

Subject: filter

From: Jan Simon

Date: 5 Aug, 2011 22:17:31

Message: 4 of 6

Dear Fernando,

And if you want to implement it by your own, here us the function as Matlab code:
  function [Y, z] = myFilter(b, a, X, z)
  % Author: Jan Simon, Heidelberg, (C) 2011
  n = length(a);
  z(n) = 0;
  Y = zeros(size(X));
  for m = 1:length(Y)
     Y(m) = b(1) * X(m) + z(1);
     for i = 2:n
        z(i - 1) = b(i) * X(m) + z(i) - a(i) * Y(m);
     end
  end
  z = z(1:n - 1);

If you unroll the loop for a specific filter length and use temporary variables for X(m) and Y(m), the performance is not too bad compared to MATLAB's filter.

Kind regards, Jan

Subject: filter

From: Fernando

Date: 9 Aug, 2011 18:33:11

Message: 5 of 6

"Jan Simon" wrote in message <j1hq5q$c4b$1@newscl01ah.mathworks.com>...
> Dear Fernando,
>
> And if you want to implement it by your own, here us the function as Matlab code:
> function [Y, z] = myFilter(b, a, X, z)
> % Author: Jan Simon, Heidelberg, (C) 2011
> n = length(a);
> z(n) = 0;
> Y = zeros(size(X));
> for m = 1:length(Y)
> Y(m) = b(1) * X(m) + z(1);
> for i = 2:n
> z(i - 1) = b(i) * X(m) + z(i) - a(i) * Y(m);
> end
> end
> z = z(1:n - 1);
>
> If you unroll the loop for a specific filter length and use temporary variables for X(m) and Y(m), the performance is not too bad compared to MATLAB's filter.
>
> Kind regards, Jan

Jan simon i would like to do a filter without initial conditions, zi, and the final conditions, zf, but i cannot use the filter function i need use a algorithm that does the same that the function because after this i will do this code in language c

thaks for the answer

Subject: filter

From: Jan Simon

Date: 11 Aug, 2011 21:17:28

Message: 6 of 6

Dear Fernando,

> Jan simon i would like to do a filter without initial conditions, zi, and the final conditions, zf, but i cannot use the filter function i need use a algorithm that does the same that the function because after this i will do this code in language c

I assume this is a question.
"No initial conditions" means that z is set to zeros(1, n) inside the function.
"No final conditions" means, that you simply ignore the 2nd output.
The conversion of the shown algorithm to C is not complicated. And the posted link contains a full-featured C-version already.

Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us