Filtre

From RoboWiki
Revision as of 15:29, 18 June 2008 by Balogh (talk | contribs) (Plávajúci priemer)
Jump to: navigation, search


Moving Average Filter

Plávajúci priemer

 % 
 % Verzia 1.0 na prvy pokus 5. 6. 2008 R.B.
 %
 
 function y = movingaverage (x, M)

% MOVINGAVERAGE Vypocita plavajuci priemer dlzky M na vektore x 
%       FLIPUD(X) returns X with columns preserved and rows flipped
%       in the up/down direction.  For example,
%
%  x je vstupny vektor, ktory filtrujeme
%  M je rad filtra, kolko clenov priemerujeme
 
 y=[zeros(1,M-1) x];     % predlzime vektor o M-1 nul na zaciatku
                         % aby sa aj prva hodnota zaratala do priemeru
 for i=1 : length(x)
  y(i) = sum( y( i : (i+M-1) ) );       % spocitavame M-tice
 endfor
  
 y=y/M;                                 % napokon to vydelime
 
 y=y(1:length(x));                      % a vektor zasa skratime o ten 
                                        % pridavok, lenze na konci
 
 endfunction

Download: movingaverage.m

Príklad použitia

% Priklad na pouzivanie MA filtra:

SignalSinus=sin(linspace(1,6*pi,300))
 

% Najprv sa pozrime, ako poskodi MA filter povodny harmonicky
% signal pri roznych velkostiach okienka:

plot(SignalSinus);  hold on;
plot(movingaverage(SignalSinus,5));
plot(movingaverage(SignalSinus,25));
plot(movingaverage(SignalSinus,100));


% Teraz uz naozaj ideme filtrovat. Vidno, ze ak jed MA filter
% normalny cisty signal mierne tlmi, tuto sa to neprejavi, pretoze
% sum niektore hodnoty rozhodi aj smerom nahor, aj nadol. Takze
% vyfiltrovany sinus je niekedy aj vacsi ako povodny... 

clg;

noise    = 0 + 0.1 * randn(size(SignalSinus)); % \mu = 0, \sigma = 0.1

plot(SignalSinus+noise); hold on;
plot(movingaverage(SignalSinus+noise,10),"r");
plot(SignalSinus,"g");



% Teraz ideme filtrovat realne data z teplomera:

clg;
data; % nacita vektor temperature s teplotami:


t1 = movingaverage(temperature(:,2)',10);  % skus rozlicne M = 10, 30, 100
plot(t1);
hold on;
plot(temperature(:,2),"g");


MAexample.m data.m


MA-TimeChar.jpg


MA-FreqChar.jpg