Difference between revisions of "Signaly"

From RoboWiki
Jump to: navigation, search
Line 114: Line 114:
  
  
[[Image:MAexample.m]]
+
[[Media:MAexample.m]]
 +
[[Media:data.m]]

Revision as of 13:27, 18 June 2008

Spektrá

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %                            
%                                                             %                            
% Takto sa kresli spektrum signalu typu Sinus s f 440 Hz.     %                            
%                                                             %                            
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %                            
                                                                                           
   f_sample = 8000;               % [Hz] - pre telefony OK                                 
   duration =    1;               % [s]  - jedna sekunda                                   
       freq =  440;               % [Hz] - komorne A4 = 440 Hz                             
          A =  0.9;               % [V]  - amplituda signalu z <-1,1>                      
                                                                                           
       time = 0 : (1/f_sample) : duration;                                                 
SinusSignal = A * sin( 2*pi*freq*time + 0 );                                               
                                                                                           
   T_sample = 1/f_sample;                                                                  
          N = length(SinusSignal);                                                         
                                                                                           
 maxtime = (N-1) * T_sample;      % Doba trvania signalu                                   
                                                                                           
 time = (0:N-1) * T_sample;       % Casova osa                                             
 freq = (0:N-1) / maxtime;        % Kmitoctova osa                                         
                                                                                           
                                                                                           
% Fourierova analyza:                                                                      
                                                                                           
S = fft(SinusSignal);             % Vypocet komplexneho spektra                            
Mod = 2/N * abs(S);               % Spravne amplitudove spektrum                           
Pha = 180/pi * angle(S);          % Fazove spektrum v stupnoch                             
                                                                                           
N1 = round(N/2);                  % Kreslime len relevantu cast po f_sample/2              
                                                                                           
subplot(2,1,1);                                                                            
plot(freq(1:N1),Mod(1:N1));       % Aplitudova frekvencna charakteristika                  
                                                                                           
subplot(2,1,2);                                                                            
plot(freq(1:N1),Pha(1:N1));       % Fazova frekvencna charakteristika


File:Spektrum.m

Plávajúci priemer

% Toto je moj vlastny plavajuci priemer
% 
% Verzia 1.0 na prvy pokus 5. 6. 2008 R.B.
%
function y = movingaverage (x, M)
%  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

File: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");


Media:MAexample.m Media:data.m