% chci si jen overit, zda cos omegat = cos (-omegat); 
t = -10:0.1:10; 
om = 2; 
plot(t,cos(om *t+0.1) , t, cos(-om * t+0.1))

%%% demo cosinusovek posunutych o 2pi %%%
t = -10:0.01:10; 
n = -10:10;
T = 1; Fs = 1/T; 
T1 = 10; om = 2*pi/T1; omn = om / Fs;  
s = cos(om * t); sn = cos(omn * n); 
plot (t,s); hold on; stem(n,sn,'r'); hold off
print -depsc FIG/omega.eps

s = cos((om+2*pi) * t); sn = cos((omn+2*pi) * n); 
plot (t,s); hold on; stem(n,sn,'r'); hold off
print -depsc FIG/omegaplus2pi.eps

s = cos((om+4*pi) * t); sn = cos((omn+4*pi) * n); 
plot (t,s); hold on; stem(n,sn,'r'); hold off
print -depsc FIG/omegaplus4pi.eps

%s = cos((om-2*pi) * t); sn = cos((omn-2*pi) * n); 
%plot (t,s); hold on; stem(n,sn,'r'); hold off
%print -depsc FIG/omegaminus2pi.eps

%s = cos((-om) * t); sn = cos((-omn) * n); 
%plot (t,s); hold on; stem(n,sn,'r'); hold off
%print -depsc FIG/minusomega.eps

%s = cos(-(om+2*pi) * t); sn = cos(-(omn+2*pi) * n); 
%plot (t,s); hold on; stem(n,sn,'r'); hold off
%print -depsc FIG/minusomegaplus2pi.eps

%%%%%%%%%%%%%%%%% komplexni exp %%%%%%%%%%%%%%%%%%%%%%%%%
N = 40; n = 0:N;
omn = 2*pi/N; 
s1 = exp(j * omn * n); 
s2 = exp(-j * omn * n); 
subplot (221); stem3(n,s1); 
subplot (222); stem3(n,s2);
s = s1 + s2; 
subplot (223); stem(n,real(s)); 
subplot (224); stem(n,imag(s)); 
print -depsc FIG/exps_sig.eps 

s1 = 0.5*exp(j * (omn * n + 0.4*pi)); 
s2 = 0.5*exp(-j * (omn * n + 0.4*pi)); 
subplot (221); stem3(n,s1); 
subplot (222); stem3(n,s2);
s = s1 + s2; 
subplot (223); stem(n,real(s)); 
subplot (224); stem(n,imag(s)); 
print -depsc FIG/exps_sig2.eps 
%s = [1 j]; stem3([0 1],s)

%%%%%%%%%%%%%%%%%%%%%%% okno %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 9; n = -10:30; s = 2* randn(1,length(n)); 
Rn = zeros(size(n)); Rn(find(n>=0 & n<=N-1)) = 1; 
x = s .* Rn; 
subplot (311); stem(n,s); grid 
subplot (312); stem(n,Rn); grid
subplot (313); stem(n,x); grid
print -depsc FIG/oknovani.eps 

%%%%%%%%%%%%%%%% periodizace %%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 4; n = -10:30; x = randn(1,N); gj0 = find(n==0);
s = zeros(size(n)); s(gj0+(0:(N-1))) = x; 
subplot (311); stem(n,s); grid
nn = mod(n,N); 
subplot (312); stem(n,nn); grid
stil= s(gj0 +nn); 
subplot (313); stem(n,stil); grid
print -depsc FIG/periodizace.eps 

%%%%%%%%%%%%%%%% cykl posunuti  %%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 4; n = -10:30; x = [1 2 3 4]; gj0 = find(n==0);
s = zeros(size(n)); s(gj0+(0:(N-1))) = x; 
subplot (211); stem(n,s); grid
nn = mod(n-2,N); 
%subplot (312); stem(n,nn); 
stil= s(gj0 +nn); 
subplot (212); stem(n,stil); grid
print -depsc FIG/cyklpos.eps 

%%%%%%%%%%%%%%%% kruh  posunuti  %%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 4; n = -10:30; x = [1 2 3 4]; gj0 = find(n==0);
RN = zeros(size(n)); RN(gj0+(0:3)) = 1; 
s = zeros(size(n)); s(gj0+(0:(N-1))) = x; 
subplot (411); stem(n,s); grid
nn = mod(n-2,N); 
%subplot (412); stem(n,nn); grid
stil= s(gj0 +nn); 
subplot (412); stem(n,stil); grid
subplot (413); stem(n,RN); grid
subplot (414); stem(n,RN.*stil); grid
print -depsc FIG/kruhpos.eps 

%%%%%%%%%%%%%%%%%% konvol lin %%%%%%%%%%%%%%%%%%%%%%%%
N = 4; n = -10:30; gj0 = find(n==0);
x=zeros(size(n)); aux = [1 2 3 4]; x(gj0+(0:3)) = aux; 
y=zeros(size(n)); aux = [1 1 -1 -1]; y(gj0+(0:3)) = aux; 
subplot (311); stem(n,x); grid
subplot (312); stem(n,y); grid
z = zeros(size(n)); 
for ii=-5:30  % very stupid implem of convolution !
  acc = 0; 
  for k=0:N-1
    acc = acc + x(gj0+k) * y(gj0 + ii - k); 
  end
  z(gj0+ii) = acc; 
end
subplot (313); stem(n,z); grid
print -depsc FIG/linkonv.eps 

%%%%%%%%%%%%%%%%%% konvol cykl  %%%%%%%%%%%%%%%%%%%%%%%%
N = 4; n = -10:30; gj0 = find(n==0);
x=zeros(size(n)); aux = [1 2 3 4]; x(gj0+(0:3)) = aux; 
y=zeros(size(n)); aux = [1 1 -1 -1]; y(gj0+(0:3)) = aux; 
%subplot (311); stem(n,x); grid
%subplot (312); stem(n,y); grid
z = zeros(size(n)); 
for ii=n  % very stupid implem of convolution !
  acc = 0; 
  for k=0:N-1
    acc = acc + x(gj0+k) * y(gj0 + mod(ii - k,N)); 
  end
  z(gj0+ii) = acc; 
end
%subplot (313); 
stem(n,z); grid
print -depsc FIG/cyklkonv.eps 

%%%%%%%%%%%%%%%%%% konvol kruh  %%%%%%%%%%%%%%%%%%%%%%%%
% this will be easy ... 
%subplot (311); stem(n,x); grid
%subplot (312); stem(n,y); grid
%subplot (313); 
stem(n,z.*RN); grid
print -depsc FIG/kruhkonv.eps 

%%%%%%%%%%%%%%%%%%%%%%% dtft obdelnika %%%%%%%%%%%%%%%%%%%%%
n = -20:20; Fs = 8000; gj0 = find(n==0); T = 1/Fs; 
x = zeros(size(n)); x(gj0+(-4:4)) = 1; 
subplot(311); stem(n,x); grid; title('signal')
f = -3*Fs:100:3*Fs; 
fn = f / Fs; 
Om = 2*pi*f;
om = Om / Fs; 
% dtft
X = zeros(size(om)); 
for ii=1:length(om)
  ejon = exp(j*om(ii).*n); 
  X(ii) = sum(x .* ejon); 
end
subplot(312); plot (om,abs(X)); grid; axis tight; title('normalized omega');
subplot(313); plot (Om,abs(X)); grid; axis tight; title('omega');
print -depsc FIG/dtftom.eps

subplot(311); stem(n,x); grid; title('signal')
subplot(312); plot (fn,abs(X)); grid; axis tight; title('normalized f');
subplot(313); plot (f,abs(X)); grid; axis tight;  title('f'); 
print -depsc FIG/dtftf.eps
%slidify(1,1,1,6)


% check - on the omega plot, the first zero-cross is at 5500 rad/s
% should be 2pi/theta
theta = 9 * T
2*pi/ theta
% ...    5.5851e+03 ... ok

% check the size - should be D theta/ T
1 * theta / T
% ... 9 ... corresponds to the plot... ok. 

%%%%%%%%%%%%%%%%%%%%%%% cos pi/8 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n = -16:32; om = pi / 8; 
x = cos(om * n); 
stem (n,x); grid; axis tight
print -depsc FIG/cospi8.eps
e1= 0.5*exp(j*om*n); e2= 0.5*exp(-j*om*n); 
stem3(n,e1); hold on; plot3(n,real(e1),imag(e1),'r--'); hold off
print -depsc FIG/epluspi8.eps
stem3(n,e2); hold on; plot3(n,real(e2),imag(e2),'r--'); hold off
print -depsc FIG/eminuspi8.eps

%%%%%%%%%%%%%%%%%%%%%%%%%%% dfr obdelnika %%%%%%%%%%%%%%%%%%%%%
n = -24:24; x = zeros(size(n)); aux = ones(1,9); N=15;
gj0 = find(n==0); toput = -4:4;
x(gj0+ toput) = aux; 
x(gj0+N+toput) = aux; 
x(gj0-N+toput) = aux; 
subplot(311); stem(n,x);grid
X = fft(x(gj0:gj0+N-1)); 
k = -15:29; 
Xshow = [X X X]; 
subplot(312); stem(k,abs(Xshow)); grid; axis tight; ylabel('module')
subplot(313); stem(k,angle(Xshow)); grid; axis tight; ylabel('argument')
print -depsc FIG/dfr.eps

%%% postupne skladani: - 2 obrazky
myax = [-24 24 -1 1.5]; om = 2*pi/N;
all = zeros(size(n)); 
% ss
this = ones(size(n)) * 1/N*X(1+0); 
subplot(421); stem(n,this); axis(myax); grid; all=all+this; 
subplot(422); stem(n,all); axis(myax); grid;
k=1; this = 1/N*X(1+k)*exp(j*k*om*n) + 1/N*X(1+N-k)*exp(-j*k*om*n); 
subplot(423); stem(n,this); axis(myax); grid; all=all+this; 
subplot(424); stem(n,all); axis(myax); grid;
k=2; this = 1/N*X(1+k)*exp(j*k*om*n) + 1/N*X(1+N-k)*exp(-j*k*om*n); 
subplot(425); stem(n,this); axis(myax); grid; all=all+this; 
subplot(426); stem(n,all); axis(myax); grid;
k=3; this = 1/N*X(1+k)*exp(j*k*om*n) + 1/N*X(1+N-k)*exp(-j*k*om*n); 
subplot(427); stem(n,this); axis(myax); grid; all=all+this; 
subplot(428); stem(n,all); axis(myax); grid;
%
print -depsc FIG/dfrsynt1.eps
k=4; this = 1/N*X(1+k)*exp(j*k*om*n) + 1/N*X(1+N-k)*exp(-j*k*om*n); 
subplot(421); stem(n,this); axis(myax); grid; all=all+this; 
subplot(422); stem(n,all); axis(myax); grid;
k=5; this = 1/N*X(1+k)*exp(j*k*om*n) + 1/N*X(1+N-k)*exp(-j*k*om*n); 
subplot(423); stem(n,this); axis(myax); grid; all=all+this; 
subplot(424); stem(n,all); axis(myax); grid;
k=6; this = 1/N*X(1+k)*exp(j*k*om*n) + 1/N*X(1+N-k)*exp(-j*k*om*n); 
subplot(425); stem(n,this); axis(myax); grid; all=all+this; 
subplot(426); stem(n,all); axis(myax); grid;
k=7; this = 1/N*X(1+k)*exp(j*k*om*n) + 1/N*X(1+N-k)*exp(-j*k*om*n); 
subplot(427); stem(n,this); axis(myax); grid; all=all+this; 
subplot(428); stem(n,all); axis(myax); grid;
print -depsc FIG/dfrsynt2.eps

%%%%%%%%%%%%%%% dfr harmon %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n = -24:24; N=15; om = 2*pi/N; phi = 0.25*pi; gj0 = find(n==0); 
x = cos (om * n + phi); 
subplot(311); stem(n,x);grid
X = fft(x(gj0:gj0+N-1)); 
k = -15:29; 
Xshow = [X X X]; 
subplot(312); stem(k,abs(Xshow)); grid; axis tight; ylabel('module')
%necessay cleaning of the phase  !!!
phase = angle(Xshow) .* (abs(Xshow) > 0.001); 
subplot(313); stem(k,phase); grid; axis tight; ylabel('argument')
print -depsc FIG/dfrharm.eps




