Difference between revisions of "Signaly"
From RoboWiki
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
== Spektrá == | == Spektrá == | ||
− | + | <source lang="octave"> | |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % | % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % | ||
% % | % % | ||
Line 39: | Line 39: | ||
plot(freq(1:N1),Pha(1:N1)); % Fazova frekvencna charakteristika | plot(freq(1:N1),Pha(1:N1)); % Fazova frekvencna charakteristika | ||
− | + | </source> | |
[[Image:spektrum.m]] | [[Image:spektrum.m]] | ||
Line 49: | Line 49: | ||
% Verzia 1.0 na prvy pokus 5. 6. 2008 R.B. | % Verzia 1.0 na prvy pokus 5. 6. 2008 R.B. | ||
% | % | ||
− | + | ||
function y = movingaverage (x, M) | function y = movingaverage (x, M) | ||
− | + | ||
% x je vstupny vektor, ktory filtrujeme | % x je vstupny vektor, ktory filtrujeme | ||
% M je rad filtra, kolko clenov priemerujeme | % M je rad filtra, kolko clenov priemerujeme | ||
− | + | ||
y=[zeros(1,M-1) x]; % predlzime vektor o M-1 nul na zaciatku | y=[zeros(1,M-1) x]; % predlzime vektor o M-1 nul na zaciatku | ||
% aby sa aj prva hodnota zaratala do priemeru | % aby sa aj prva hodnota zaratala do priemeru | ||
Line 60: | Line 60: | ||
y(i) = sum( y( i : (i+M-1) ) ); % spocitavame M-tice | y(i) = sum( y( i : (i+M-1) ) ); % spocitavame M-tice | ||
endfor | endfor | ||
+ | |||
+ | y=y/M; % napokon to vydelime | ||
− | |||
− | |||
y=y(1:length(x)); % a vektor zasa skratime o ten | y=y(1:length(x)); % a vektor zasa skratime o ten | ||
% pridavok, lenze na konci | % pridavok, lenze na konci | ||
− | + | ||
endfunction | endfunction | ||
+ | |||
− | [[ | + | [[Media:movingaverage.m|movingaverage.m]] |
− | |||
− | Príklad použitia | + | === Príklad použitia === |
% Priklad na pouzivanie MA filtra: | % Priklad na pouzivanie MA filtra: | ||
− | + | ||
SignalSinus=sin(linspace(1,6*pi,300)) | SignalSinus=sin(linspace(1,6*pi,300)) | ||
− | + | ||
− | + | ||
% Najprv sa pozrime, ako poskodi MA filter povodny harmonicky | % Najprv sa pozrime, ako poskodi MA filter povodny harmonicky | ||
% signal pri roznych velkostiach okienka: | % signal pri roznych velkostiach okienka: | ||
− | + | ||
plot(SignalSinus); hold on; | plot(SignalSinus); hold on; | ||
plot(movingaverage(SignalSinus,5)); | plot(movingaverage(SignalSinus,5)); | ||
plot(movingaverage(SignalSinus,25)); | plot(movingaverage(SignalSinus,25)); | ||
plot(movingaverage(SignalSinus,100)); | plot(movingaverage(SignalSinus,100)); | ||
− | + | ||
− | + | ||
% Teraz uz naozaj ideme filtrovat. Vidno, ze ak jed MA filter | % Teraz uz naozaj ideme filtrovat. Vidno, ze ak jed MA filter | ||
% normalny cisty signal mierne tlmi, tuto sa to neprejavi, pretoze | % normalny cisty signal mierne tlmi, tuto sa to neprejavi, pretoze | ||
% sum niektore hodnoty rozhodi aj smerom nahor, aj nadol. Takze | % sum niektore hodnoty rozhodi aj smerom nahor, aj nadol. Takze | ||
% vyfiltrovany sinus je niekedy aj vacsi ako povodny... | % vyfiltrovany sinus je niekedy aj vacsi ako povodny... | ||
− | + | ||
clg; | clg; | ||
− | + | ||
noise = 0 + 0.1 * randn(size(SignalSinus)); % \mu = 0, \sigma = 0.1 | noise = 0 + 0.1 * randn(size(SignalSinus)); % \mu = 0, \sigma = 0.1 | ||
− | + | ||
plot(SignalSinus+noise); hold on; | plot(SignalSinus+noise); hold on; | ||
plot(movingaverage(SignalSinus+noise,10),"r"); | plot(movingaverage(SignalSinus+noise,10),"r"); | ||
plot(SignalSinus,"g"); | plot(SignalSinus,"g"); | ||
− | + | ||
− | + | ||
− | + | ||
% Teraz ideme filtrovat realne data z teplomera: | % Teraz ideme filtrovat realne data z teplomera: | ||
− | + | ||
clg; | clg; | ||
data; % nacita vektor temperature s teplotami: | data; % nacita vektor temperature s teplotami: | ||
− | + | ||
− | + | ||
t1 = movingaverage(temperature(:,2)',10); % skus rozlicne M = 10, 30, 100 | t1 = movingaverage(temperature(:,2)',10); % skus rozlicne M = 10, 30, 100 | ||
plot(t1); | plot(t1); | ||
hold on; | hold on; | ||
plot(temperature(:,2),"g"); | plot(temperature(:,2),"g"); | ||
− | + | ||
− | + | ||
− | [[ | + | [[Media:MAexample.m|MAexample.m]] |
+ | [[Media:data.m|data.m]] |
Latest revision as of 14:26, 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
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
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");