%%  Test1
% a = [1 1 0 0 1 1 0 0 1 1 ];
% fs = 44100;
% fd = [0 400 420 1000 1100 5000 5100 12000 12100 fs/2];
% L = 1000;
% N = 65;
%%  Test2
% a = [1 1 0 0 ];
% fs = 12000;
% fd = [0 5000 5100 fs/2];
% L = 1000;
% N = 65;
%%  function
function [ g_win ] = myfir( a, fd, Fs, L, N, win )

if nargin < 6
    win = 'rectangular';
end

close all
delta_f = Fs/(L-1);
frek = 0:delta_f:Fs;
leng = length(frek);
halfleng = leng/2;

if( leng < N )
    disp('N túl nagy');
    return; 
end

fd_size = length(fd);
for i=1:fd_size
 if( Fs/2 < fd(i) )
    disp('Fs kicsi');
    disp(Fs);
    return; 
 end
end



d = a(1) * ones(1,L);
k = length(a);
for i=2:k
    
    if a(i-1)~=a(i)
        x0=round(fd(i-1)/delta_f);
        x1=round(fd(i)/delta_f);
        
        p = polyfit([x0 x1],[a(i-1) a(i)],1);
        
        x=x0:1:x1;
        d( x0 : x1) = polyval(p,x);
        d( x1 : end) = a(i);

    else
        x=round(fd(i)/delta_f);
        d( x : end) = a(i);
    end
end
d((L/2)+1:end)=fliplr( d(1:(L/2)) );


% d terv plot
figure;
plot(frek(1:halfleng),d(1:halfleng),'k','LineWidth',1);
%title('H_d - terv')
ylabel('Magnitude','FontSize',13)
xlabel('Frequency (Hz)','FontSize',13)
grid on

hd = real(ifft(d));
leng_hd = length(hd);


figure;
plot(frek,abs(hd),'k','LineWidth',1);
title('h_d')
ylabel('Magnitude','FontSize',13)
xlabel('Frequency (Hz)','FontSize',13)
grid on

h = zeros(1,L);
g = [ hd(round(leng_hd-N/2+1):end), hd(round(1:N/2)) ];
g_size = length(g);
h((L/2)-(g_size/2):(L/2)+(g_size/2)-1) = g;

figure;
plot(abs(g),'k','LineWidth',1);
%title('g - csonkolás')
ylabel('Magnitude','FontSize',13)
xlabel('N','FontSize',13)
grid on

H0 = real(fft(h));

% H plot
figure;
plot(frek(1:halfleng),20*log10(abs(H0(1:halfleng))),'k','LineWidth',1);
%title('H - csonkolás után')
ylabel('Magnitude (dB)','FontSize',13)
xlabel('Frequency (Hz)','FontSize',13)
grid on

switch win
    case 'blackman'
        w = blackman(g_size)';
        g_win = h((L/2)-(g_size/2):(L/2)+(g_size/2)-1).*w;
        h((L/2)-(g_size/2):(L/2)+(g_size/2)-1) = g_win;
        %h_hamming = h;
    case 'hamming'
        w = hamming(g_size)';
        g_win = h((L/2)-(g_size/2):(L/2)+(g_size/2)-1).*w;
        h((L/2)-(g_size/2):(L/2)+(g_size/2)-1) = g_win;
        %h_hamming = h;
    case 'bartlett'
        w = bartlett(g_size)';
        a_win = h((L/2)-(a_size/2):(L/2)+(a_size/2)-1).*w;
        h((L/2)-(a_size/2):(L/2)+(a_size/2)-1) = a_win;
        %h_bartlett = h;
    case 'kaiser'
        w = kaiser(g_size)';
        g_win = h((L/2)-(g_size/2):(L/2)+(g_size/2)-1).*w;
        h((L/2)-(g_size/2):(L/2)+(g_size/2)-1) = g_win;
        %h_kaiser = h;
    otherwise
        g_win = g;
end
H = real(fft(h));

f=frek(1:halfleng);
figure;
%plot(f,20*log10(abs(H0(1:halfleng)), f,20*log10(abs(H(1:halfleng))),'LineWidth',1);
plot(frek(1:halfleng),20*log10(abs(H(1:halfleng))),'k','LineWidth',1);
%title(['H - csonkolás és ',win,' ablak után'])
title([win,' ablak után'])
ylabel('Magnitude (dB)','FontSize',13)
xlabel('Frequency (Hz)','FontSize',13)
grid on

end
