How can I write the Matlab “filter”-function myself?

后端 未结 5 1806
攒了一身酷
攒了一身酷 2021-02-06 19:23

I would like to use a Butterworth filter on a 1D-Signal. In Matlab the script would look like this:

 f=100;
 f_cutoff = 20; 
 fnorm =f_cutoff/(f/2);
 [b,a] = but         


        
5条回答
  •  感情败类
    2021-02-06 19:52

    I found my mistake. Here's the working code (as a function):

    function filtered = myFilter(b, a, raw)
    
    filtered = zeros(size(raw));
    for c = 1:3
        for n = 9:size(raw,1)
    
            filtered(n,c) = b(1)* raw(n,c)   + b(2)* raw(n-1,c) + b(3)* raw(n-2,c) ...
                          + b(4)* raw(n-3,c) + b(5)* raw(n-4,c) + b(6)* raw(n-5,c) ...
                          + b(7)* raw(n-6,c) + b(8)* raw(n-7,c) + b(9)* raw(n-8,c) ...
                          - a(1)*filtered(n,c)   - a(2)*filtered(n-1,c) - a(3)*filtered(n-2,c) ...
                          - a(4)*filtered(n-3,c) - a(5)*filtered(n-4,c) - a(6)*filtered(n-5,c) ...
                          - a(7)*filtered(n-6,c) - a(8)*filtered(n-7,c) - a(9)*filtered(n-8,c);
        end
    end
    

    Now the filter works nearly fine, but at the first 40 values i've got divergent results. I'll have to figure that out...

提交回复
热议问题