%function [ z ] = fir(NewSample)

% Filter type: Low Pass
% Filter model: Butterworth
% Filter order: 2
% Sampling Frequency: 200 Hz
% Cut Frequency: 5 Hz

clear all;
clc;

%TESTSignal:
frequenz=50; % abzutastendes Signal in Hz 
perioden=4;
tend=1/frequenz*perioden;

tstep=1/(frequenz*20000);

fa=200; % Abtastfrequenz mit den Signal abgetastet wird [Hz]
ta=1/fa;
tdelay=0.002;

t=0:tstep:tend;
sig=sin(2*pi*50*t);

f00=size(sig);
array=zeros(1,f00(1,2));
array2=zeros(1,f00(1,2));

n=1;
k=0;
for i=1:1:(tend/ta)
    
    k=k+round(ta/tstep);
    if k < ((tend/tstep))
        Sample(1,n)=sig(1,k);
        array(1,k)=Sample(1,n);
        %Sample(1,n)=sig(1,k+(tdelay/tstep));
        %array(1,k+(tdelay/tstep))=Sample(1,n);
    end
    
    n=n+1;
    
end

size_val=size(Sample);


global x;
global y;
global fir_val;


 
FIRCoef=[  0.07852348396611394400,
        0.09197229217974078900,
        0.10690307134057316000,
        0.12336376893393712000,
        0.13198182850849385000,
        0.12336376893393712000,
        0.10690307134057316000,
        0.09197229217974078900,
        0.07852348396611394400,
        0.06649293865077621200];

foo=size(FIRCoef)
NTap=foo(1,1)
x=zeros(1,NTap);
    
for i=1:1:size_val(1,2)

    y(1,1)=0;
    
    for n=NTap:-1:2
        x(1,n)=x(1,n-1);
    end

    x(1,1)=Sample(1,i);
    for n=1:1:NTap
        foo=FIRCoef(n,1)*x(1,n);
        y(1,1)=y(1,1)+foo;
    end
    
    fir_val(1,i)=y(1,1);
    
end

n=1;
k=0;

for i=1:1:(tend/ta)
    
    k=k+round(ta/tstep);
    if k < ((tend/tstep))
        array2(1,k)=fir_val(1,n);
       %array2(1,k+(tdelay/tstep))=fir_val(1,n);
    end
    n=n+1;
    
end

figure(1);
plot(t,sig,t,array,t,array2,'-');
grid on;

figure(2);
plot(t,array2);
grid on;

%end

