%% Konvoluce 1D
% Tomas Krejci, 11.4.2015

clc
close all
clear all

h = [1 2 3 4 5 6 7]; % konvolucni jadro
g = [6 7 8 9 5 6 7 4 5 6];

%% vytvoreni macice pro posun
r = length(g) - length(h);
if r > 0
    g2 = zeros(length(h),length(g)*2-1-r);
else
    g2 = zeros(length(h),length(g)*2-1);
end

[n1 m] = size(h); % n1-pocet radku; m-pocet sploupcu
[p n] = size(g2); % p-pocet radku; n-pocet sploupcu

if m ~= p
    disp('chyba, vadny dormer matice'); return;
end

%% naplneni rozsirene matice
jj = 1;
for ii=1:length(h)   
    g2(jj,ii:ii+length(g)-1) = g;
    jj = jj+1;    
end

out = zeros(1,n); % vysledny vektor

%% nasobeni matic
for nn = 1:n
    x = 0;
    for mm = 1:m
        x = h(1,mm)*g2(mm,nn) + x;       
    end
    out(nn) = x;
end

if out ~= conv(h,g)
    disp('chyba, nesouhlasi s conv'); return;
else
    out
end





