the Filter function generated output is not as same as what the reference says

18 Ansichten (letzte 30 Tage)
I am using Filter function to get moving average of a data vector.
Here is the the Matlab code:
fileID = fopen('dj_test.txt');
C = textscan(fileID,'%d %d %d');
fclose(fileID);
%STEP 1: moving average:
a = 1;
b = [1/5 1/5 1/5 1/5];
x = filter(b,a,double(C{1}));
y = filter(b,a,double(C{2}));
z = filter(b,a,double(C{3}));
And here is the my date file dj_test.txt:
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
2 2 2
2 2 2
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
2 2 2
2 2 2
The result [x y z] is as:
0.2000 0.2000 0.2000
0.4000 0.4000 0.4000
0.6000 0.6000 0.6000
0.8000 0.8000 0.8000
0.8000 0.8000 0.8000
1.0000 1.0000 1.0000
1.2000 1.2000 1.2000
1.4000 1.4000 1.4000
1.6000 1.6000 1.6000
1.6000 1.6000 1.6000
1.4000 1.4000 1.4000
1.2000 1.2000 1.2000
1.0000 1.0000 1.0000
0.8000 0.8000 0.8000
0.8000 0.8000 0.8000
1.0000 1.0000 1.0000
1.2000 1.2000 1.2000
1.4000 1.4000 1.4000
1.6000 1.6000 1.6000
1.6000 1.6000 1.6000
According to the function reference ( http://www.mathworks.com/help/matlab/ref/filter.html ): The result looks strange to me. For example, the last value should be (2+2+2+2+2)/5=2, I wonder why it is 1.6. Is it because what I use (Filter) is different from what the reference says? If that's the case, how can I set the correct context for using a function that I need. I know there are functions with the same name but do different calculations.

Akzeptierte Antwort

Guillaume
Guillaume am 29 Jun. 2015
filter is working as it should. You've only got 4 elements in you b, yet you divide by 5. 2*4/5 == 1.6. if you want a moving average filter you need either:
b = [1 1 1 1] / 4;
or
b = [1 1 1 1 1] / 5;

Weitere Antworten (1)

Dajiang
Dajiang am 29 Jun. 2015
yes, you're right. sorry for my carelessness.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by