% Casove frekvencni spektrum


% Ukazkove signaly
n=8; N=2^n;
%t=(fix(-N/2):fix(N/2-1));
t=(0:N-1)'; % Diskretni promenna (sloupcovy vektor)
w1=2*pi/N*8; w2=2*pi/N*40; w3=2*pi/N*80;
f1=sin(w1*t)+sin(w2*t)+sin(w3*t); % Diskretni funkce
f2=sin(w1*t);
f2(fix(N/4):fix(3*N/4))=f2(fix(N/4):fix(3*N/4))+0.3*sin(w3*t(fix(N/4):fix(3*N/4)));
f2(fix(N/2):N-1)=f2(fix(N/2):N-1)+0.3*sin(w2*t(fix(N/2):N-1));

% Vytvoreni oken
M=64; % Pocet bodu, na nichz bude okno realizovano
th=(1:M)'; % Diskretni promenna okenni funkce
% Obdelnikove okno
h1=ones(size(th)); h1=h1/sum(h1);
% Trojuhelnikove okno
h2=[1:ceil(M/2),floor(M/2):-1:1]'; h2=h2/sum(h2);
% Hanningovo okno
h3=[1-cos(2*pi*th/(M+1))]; h3=h3/sum(h3);
% Hammingovo okno
h4=[1-0.84*cos(2*pi*th/(M+1))]; h4=h4/sum(h4);


% Nastaveni zbyvajicich parametru diskretni okenni Fourierovy transformace
  % Posun okna o wstep bodu
stepw=1;%fix(M/4);
  % Pocet oken
nw=fix( (N-(M-stepw))/stepw );


% Prima transformace
F1=fft(f1);
F2=fft(f2);
% Vypocet ampl. spektra
F1_am=abs(F1)/(N/2);
F2_am=abs(F2)/(N/2);

% Matice Fourierovych koeficientu
c1=zeros(nw,M); c2=zeros(nw,M); i=0;
for m=1:nw
  c1(m,:)=( fft(f1(i+1:i+M).*h3) )';
  c2(m,:)=( fft(f2(i+1:i+M).*h3) )';
  i=i+stepw;
end;  

cabs1=abs(c1(:,1:M/2))';
cabs2=abs(c2(:,1:M/2))';
[t_,w_]=meshgrid((0:nw-1)/(nw-1)*N,(1:M/2)*N/M);
[tg,wg]=meshgrid(0:N-1,1:N/2);
cabsg1=interp2(t_,w_,cabs1,tg,wg);
cabsg2=interp2(t_,w_,cabs2,tg,wg);

% Graficke zobrazeni
figure(1);
  subplot(2,2,1);
    hold on
    axis([min(t) max(t) min(f1) max(f1)]);
    title('Signál f_1','FontName','@MS Mincho','FontSize',14);
    plot(t,f1,'k-');
  subplot(2,2,2);
    hold on
    axis([min(t) max(t) min(f2) max(f2)]);
    title('Signál f_2','FontName','@MS Mincho','FontSize',14);
    plot(t,f2,'k-');
  subplot(2,2,3);
    hold on
    axis([0 N/2-1 min(F1_am(1:N/2)) max(F1_am(1:N/2))]);
    title('Amplitudové spektrum f_1','FontName','@MS Mincho','FontSize',14);
    bar(0:N/2-1,F1_am(1:N/2));
  subplot(2,2,4);
    hold on
    axis([0 N/2-1 min(F2_am(1:N/2)) max(F2_am(1:N/2))]);
    title('Amplitudové spektrum f_2','FontName','@MS Mincho','FontSize',14);
    bar(0:N/2-1,F2_am(1:N/2));
  pause;
  clf;
  subplot(1,2,1);
    hold on; grid on;
    view(3); rotate3d; box; grid on;
    axis([0 N 0 N/2 0 max(max(cabsg1))]);
    surf( tg,wg,cabsg1, 'EdgeColor','interp' );
    title('Èasovì frekvenèní spektrum signálu f_1','FontName','@MS Mincho','FontSize',12)
    xlabel('t_m'); ylabel('\omega_n'); zlabel('c_{mn}');
    %colorbar;
  subplot(1,2,2);
    hold on
    view(3); rotate3d; box; grid on;
    axis([0 N 0 N/2 0 max(max(cabsg2))]);
    surf( tg,wg,cabsg2, 'EdgeColor','interp' );
    title('Èasovì frekvenèní spektrum signálu f_2','FontName','@MS Mincho','FontSize',12)
    xlabel('t_m'); ylabel('\omega_n'); zlabel('c_{mn}');
    %colorbar;
  pause;
close(1);
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    