
function trend_sig = trend_ECG(signal,fvz,kind)
%funkcia na symuláciu trendu v EKG

SNR=5;

sig_f=signal-mean(signal);
%  figure
%  x_frek_sig=linspace(0,fvz,length(signal));
%  PSD_spek_sig=(abs(fft(sig_f)).^2)/length(signal);
%  plot(x_frek_sig,PSD_spek_sig);
%  axis([0 60 0 max(PSD_spek_sig)])
% title('Spektrum signalu')

% figure
% [PSD_W,F_W] = pwelch(sig_f,260,65,(1024*2)-1,fvz,'onesided'); %uses a hamming window 
% plot(F_W,PSD_W);
%  axis([0 60 0 max(PSD_W)])
% title('Spektrum signalu')

trend_sig=zeros(1,length(signal));
trend=zeros(1,length(signal));
switch kind
    case 1 %lineárny trend (line trend)
        liner=1:1:length(signal);
        trend=1+0.5*liner;
        trend_f=trend-mean(trend);
%         figure
%         x_frek_trend=linspace(0,fvz,length(trend));
%         PSD_spek_trend=(abs(fft(trend_f)).^2)/length(trend);
%         plot(x_frek_trend,PSD_spek_trend);
%         axis([0 1 0 max(PSD_spek_trend)])
%         title('Spektrum trendu')
    case 2 %Gaussian trend (Gauss trend)
        liner=1:1:length(signal);
        trend=600.*exp(-0.000001.*(liner-length(signal)/2).^2);
        trend_f=trend-mean(trend);
%         figure
%         x_frek_trend=linspace(0,fvz,length(trend));
%         PSD_spek_trend=(abs(fft(trend_f)).^2)/length(trend);
%         plot(x_frek_trend,PSD_spek_trend);
%         axis([0 1 0 max(PSD_spek_trend)])
%         title('Spektrum trendu')
        
    case 3 %vrcholový trend (Cusp Trend)
        liner=1:1:length(signal);
        trend=300.*sqrt(abs(liner-length(signal)/2));
        trend_f=trend-mean(trend);
        trend=trend-max(trend); %posunutie vrcholoveho trendu smerom dole
%         figure
%         x_frek_trend=linspace(0,fvz,length(trend));
%         PSD_spek_trend=(abs(fft(trend_f)).^2)/length(trend);
%         plot(x_frek_trend,PSD_spek_trend);
%         axis([0 1 0 max(PSD_spek_trend)])
%         title('Spektrum trendu')
    case 4 %lomový trend (Break trend)
        L=length(signal);
        A=500;
        k=0.5;
        for i=1:1:length(signal)
            if((0<=i)&&(i<=(L/2-A/k)))
                trend(1,i)=A;
            elseif((L/2-A/k)<=i)&&(i<=(L/2+A/k))
                trend(1,i)=k*(L/2-i);
            else
                trend(1,i)=-A;
            end
        end
        trend_f=trend-mean(trend);
%         figure
%         x_frek_trend=linspace(0,fvz,length(trend));
%         PSD_spek_trend=(abs(fft(trend_f)).^2)/length(trend);
%         plot(x_frek_trend,PSD_spek_trend);
%         axis([0 1 0 max(PSD_spek_trend)])
%         title('Spektrum trendu')
    case 5
        mt=500;
        f1=0.25;
        n=1:length(signal);
        trend = mt*sin(2*pi*(f1/fvz)*n);
        trend_f=trend-mean(trend);
%         figure
%         x_frek_trend=linspace(0,fvz,length(trend));
%         PSD_spek_trend=(abs(fft(trend_f)).^2)/length(trend);
%         plot(x_frek_trend,PSD_spek_trend);
%         axis([0 1 0 max(PSD_spek_trend)])
%         title('Spektrum trendu')

end
SNRvs=10*log10(sum(signal.^2)/sum(trend.^2))

A=sqrt(sum(signal.^2)/(10^(SNR/10)*sum(trend.^2)));

trend=A*trend;

trend_sig=signal+trend;

SNRXXX=10*log10(sum(signal.^2)/sum(trend.^2))
SNRXXX=10*log10(sum(signal.^2)/sum(trend.^2))
% figure;
% plot(trend_sig)
% trend_f=trend-mean(trend);
%  figure
%  x_frek_trend_sig=linspace(0,fvz,length(trend_f));
%  PSD_spek_trend_sig=(abs(fft(trend_f)).^2)/length(trend_f);
%  stem(x_frek_trend_sig,PSD_spek_trend_sig);
%  axis([0 60 0 max(PSD_spek_trend_sig)])
% title('Spektrum trendu')

% figure
% [PSD_W_ST,F_W_ST] = pwelch(trend_sig_f,260,65,(1024*2)-1,fvz,'onesided'); %uses a hamming window 
% plot(F_W_ST,PSD_W_ST);
%  axis([0 60 0 max(PSD_W)])
% title('Spektrum signalu s trendem')
end