% implementierung Segmentierte Faltung
clc
clear all

%==========================================================================
% fürs erste mal ohne zerteilte Impulsantwort
% =========================================================================


%===================
% Eingangssignale
%===================
irT = [0.6450844579486333+0.8674324073675452i 0.09001532053149186+0.7807845028338266i 0.3054332470124851+0.332945976890495i 0.7864208755778657+0.6439162514064736i 0.9645555281776343+0.6988874032912417i 0.6622139217264938+0.43551314479783765i 0.014686482006609936+0.8874398263553877i 0.423083938383806+0.23325431472363045i 0.03788213009299202+0.3397548355121518i 0.7064565806649087+0.681972028431751i 0.5537719757518892+0.9643880489190886i 0.34980046534245746+0.2186722931807693i 0.3871628784798825+0.9889490945588172i 0.7254554940293109+0.5777384102533122i 0.722907233651835+0.37149076678250226i 0.7801971618713254+0.4169239526059595i 0.06486315447922308+0.6166385607792104i 0.707789514863035+0.8588436571990147i 0.9964377406185364+0.8111881890206369i 0.5040694875329287+0.9780218407993322i 0.8521193193955178+0.44487565387349803i 0.006643564787461753+0.7801597312291756i 0.1525680493902294+0.11425073758957838i 0.40506740501390404+0.018134839388118995i 0.9302484639635749+0.5210611943398703i 0.6224175837149694+0.2683465997579958i 0.8782517932503786+0.2712818911858047i 0.41545411375007635+0.05137461764057938i 0.5918698944707026+0.8568994984878276i 0.5181113646717982+0.42251692371379945i 0.04064637192210363+0.859789596780173i 0.66184366318897+0.3806275579734987i 0.8080449035829823+0.8555004535846746i 0.8463916399715286+0.6477742744518165i 0.5665389721079036+0.7144830560485168i 0.8058238212436862+0.8575884136888453i 0.717840528083322+0.9992849128332237i 0.21724734167032278+0.9567812387752876i 0.12115008123044646+0.9384506545647993i 0.8333705772173617+0.6721518141888487i ]

irL = [ 0.6450844579486333 0.09001532053149186 0.3054332470124851 0.7864208755778657 0.9645555281776343 0.6622139217264938 0.014686482006609936 0.423083938383806 0.03788213009299202 0.7064565806649087 0.5537719757518892 0.34980046534245746 0.3871628784798825 0.7254554940293109 0.722907233651835 0.7801971618713254 0.06486315447922308 0.707789514863035 0.9964377406185364 0.5040694875329287 0.8521193193955178 0.006643564787461753 0.1525680493902294 0.40506740501390404 0.9302484639635749 0.6224175837149694 0.8782517932503786 0.41545411375007635 0.5918698944707026 0.5181113646717982 0.04064637192210363 0.66184366318897 0.8080449035829823 0.8463916399715286 0.5665389721079036 0.8058238212436862 0.717840528083322 0.21724734167032278 0.12115008123044646 0.8333705772173617];

irR = [ 0.8674324073675452 0.7807845028338266 0.332945976890495 0.6439162514064736 0.6988874032912417 0.43551314479783765 0.8874398263553877 0.23325431472363045 0.3397548355121518 0.681972028431751 0.9643880489190886 0.2186722931807693 0.9889490945588172 0.5777384102533122 0.37149076678250226 0.4169239526059595 0.6166385607792104 0.8588436571990147 0.8111881890206369 0.9780218407993322 0.44487565387349803 0.7801597312291756 0.11425073758957838 0.018134839388118995 0.5210611943398703 0.2683465997579958 0.2712818911858047 0.05137461764057938 0.8568994984878276 0.42251692371379945 0.859789596780173 0.3806275579734987 0.8555004535846746 0.6477742744518165 0.7144830560485168 0.8575884136888453 0.9992849128332237 0.9567812387752876 0.9384506545647993 0.6721518141888487];




sigT = [0.35672420644834213+0.02134939597613894i 0.385139708018437+0.22451349192389713i 0.02772059705083163+0.6028756398130825i 0.7969602002667158+0.08131790055682964i 0.19019506082353643+0.03294659168711489i 0.9845530917839295+0.4313560264219606i 0.11928880347880066+0.7329455617534177i 0.26428543384957803+0.2377684973256894i 0.2690582171348592+0.32973790831464156i 0.19285295589855433+0.17969814563659758i 0.464063799539044+0.42761186593108313i 0.47748197819290283+0.5282697531440983i 0.07274864040482465+0.8494741295542949i 0.4493573667871842+0.019183164069153658i 0.15580109301453426+0.2108557050183334i 0.4767713122349385+0.08205794673493627i 0.3571102806130766+0.5037605780774971i 0.6095278971360456+0.3657689985470093i 0.6105870723909035+0.9621165152402827i 0.6924264168599926+0.35936410028338106i 0.5730372818409999+0.8565630746606839i 0.1631865539349152+0.04975622563839044i 0.061438254887309474+0.023539431024667046i 0.7589664732312011+0.980234431925323i 0.672383042589749+0.8721106711597302i 0.39566487451655696+0.7779396329951257i 0.19212192734026112+0.33169757023704327i 0.5554867329612946+0.2038020585055056i 0.3675437235490716+0.7048519440624693i 0.6207912327338446+0.045039534472160914i 0.10160410007759124+0.671445986047491i 0.7260300586098632+0.7181669281220054i 0.1611222961801113+0.754120402281065i 0.8646228928786218+0.2795689735863227i 0.0405310701740782+0.3995629974154087i 0.1839059959709861+0.6510396692979321i 0.7120794080145968+0.45616581826881375i 0.6288289801426112+0.2592936290555269i 0.6111511773275802+0.23583540366582678i 0.3538640331019203+0.961306075526642i 0.35477728469347436+0.5362075055097103i 0.612630480541334+0.08143967526299256i 0.37208250485479955+0.9692438986408558i 0.15602709382122903+0.6216680088888932i 0.8373229520979719+0.6931658075729762i 0.5222036104647031+0.22280699998352438i 0.03933317610076492+0.17690469071079518i 0.9854913767346624+0.2525174489388172i 0.5956170954536979+0.9397279931442322i 0.34645297424905674+0.4749429949635221i 0.724980185025666+0.30342383039303455i 0.19149913140955055+0.11719955251352121i 0.12951462269392633+0.07040506809467839i 0.7280603954488801+0.7895339715144915i 0.9049134336148682+0.6983203338196577i 0.8100904007996399+0.6388211153870679i 0.2975033905237029+0.9802352445145446i 0.308716056269816+0.319169886098292i 0.0570506690065834+0.4591744026362088i 0.5541398968580363+0.8487608155137097i 0.5963072126249879+0.8067909577517843i 0.17743782339063496+0.1886389288749981i 0.1614300302359266+0.17815704463936455i 0.31468555253024577+0.42703727860383645i 0.7418193774764718+0.11814516393169772i 0.8008854020863453+0.7819082487874738i 0.5035862178648016+0.9395971349656538i 0.6774546711958201+0.2446912638211245i 0.11777311560324522+0.6194534141176804i 0.06457527883649905+0.5799467497553483i 0.472616735626023+0.16624264682206102i 0.20849423910233744+0.56833839032396i 0.9085549531573152+0.1008078473707199i 0.3039027441535874+0.8345465966309445i 0.2953440757900858+0.1618715476782705i 0.5128113293108053+0.3370483522623343i 0.9825676226121492+0.5990195914172566i 0.45387733579519196+0.6132994474462314i 0.8979805730544753+0.447909425187518i 0.3199300956409934+0.1717072888224428i 0.8425482216030784+0.9959853183022553i 0.6964780313476873+0.0008837451295480303i 0.3367018806203118+0.24170949521799512i 0.1002715608072634+0.027018996159867137i 0.20924856636822287+0.5262948535190406i 0.2345340311562626+0.49181921188104205i 0.4885353387708069+0.6741514825670701i 0.9662623172537927+0.6463018489792469i 0.054883199853773657+0.9167355040363546i 0.07103798958027985+0.8598449762431178i 0.9439695546947773+0.5942157494465867i 0.8620785162431154+0.2292371409530619i 0.15275211269700262+0.13034983235083675i 0.9227974386911871+0.039077453703093346i 0.2926915011225931+0.05818849920983282i 0.2959313903667443+0.9727255850775609i 0.4594436893048969+0.9377026388429309i 0.6473308228077522+0.8021042842044926i 0.24385722154981015+0.8511548848938554i 0.11880049876728516+0.395007660806718i ]

sigL = [ 0.35672420644834213 0.385139708018437 0.02772059705083163 0.7969602002667158 0.19019506082353643 0.9845530917839295 0.11928880347880066 0.26428543384957803 0.2690582171348592 0.19285295589855433 0.464063799539044 0.47748197819290283 0.07274864040482465 0.4493573667871842 0.15580109301453426 0.4767713122349385 0.3571102806130766 0.6095278971360456 0.6105870723909035 0.6924264168599926 0.5730372818409999 0.1631865539349152 0.061438254887309474 0.7589664732312011 0.672383042589749 0.39566487451655696 0.19212192734026112 0.5554867329612946 0.3675437235490716 0.6207912327338446 0.10160410007759124 0.7260300586098632 0.1611222961801113 0.8646228928786218 0.0405310701740782 0.1839059959709861 0.7120794080145968 0.6288289801426112 0.6111511773275802 0.3538640331019203 0.35477728469347436 0.612630480541334 0.37208250485479955 0.15602709382122903 0.8373229520979719 0.5222036104647031 0.03933317610076492 0.9854913767346624 0.5956170954536979 0.34645297424905674 0.724980185025666 0.19149913140955055 0.12951462269392633 0.7280603954488801 0.9049134336148682 0.8100904007996399 0.2975033905237029 0.308716056269816 0.0570506690065834 0.5541398968580363 0.5963072126249879 0.17743782339063496 0.1614300302359266 0.31468555253024577 0.7418193774764718 0.8008854020863453 0.5035862178648016 0.6774546711958201 0.11777311560324522 0.06457527883649905 0.472616735626023 0.20849423910233744 0.9085549531573152 0.3039027441535874 0.2953440757900858 0.5128113293108053 0.9825676226121492 0.45387733579519196 0.8979805730544753 0.3199300956409934 0.8425482216030784 0.6964780313476873 0.3367018806203118 0.1002715608072634 0.20924856636822287 0.2345340311562626 0.4885353387708069 0.9662623172537927 0.054883199853773657 0.07103798958027985 0.9439695546947773 0.8620785162431154 0.15275211269700262 0.9227974386911871 0.2926915011225931 0.2959313903667443 0.4594436893048969 0.6473308228077522 0.24385722154981015 0.11880049876728516];

sigR = [ 0.02134939597613894 0.22451349192389713 0.6028756398130825 0.08131790055682964 0.03294659168711489 0.4313560264219606 0.7329455617534177 0.2377684973256894 0.32973790831464156 0.17969814563659758 0.42761186593108313 0.5282697531440983 0.8494741295542949 0.019183164069153658 0.2108557050183334 0.08205794673493627 0.5037605780774971 0.3657689985470093 0.9621165152402827 0.35936410028338106 0.8565630746606839 0.04975622563839044 0.023539431024667046 0.980234431925323 0.8721106711597302 0.7779396329951257 0.33169757023704327 0.2038020585055056 0.7048519440624693 0.045039534472160914 0.671445986047491 0.7181669281220054 0.754120402281065 0.2795689735863227 0.3995629974154087 0.6510396692979321 0.45616581826881375 0.2592936290555269 0.23583540366582678 0.961306075526642 0.5362075055097103 0.08143967526299256 0.9692438986408558 0.6216680088888932 0.6931658075729762 0.22280699998352438 0.17690469071079518 0.2525174489388172 0.9397279931442322 0.4749429949635221 0.30342383039303455 0.11719955251352121 0.07040506809467839 0.7895339715144915 0.6983203338196577 0.6388211153870679 0.9802352445145446 0.319169886098292 0.4591744026362088 0.8487608155137097 0.8067909577517843 0.1886389288749981 0.17815704463936455 0.42703727860383645 0.11814516393169772 0.7819082487874738 0.9395971349656538 0.2446912638211245 0.6194534141176804 0.5799467497553483 0.16624264682206102 0.56833839032396 0.1008078473707199 0.8345465966309445 0.1618715476782705 0.3370483522623343 0.5990195914172566 0.6132994474462314 0.447909425187518 0.1717072888224428 0.9959853183022553 0.0008837451295480303 0.24170949521799512 0.027018996159867137 0.5262948535190406 0.49181921188104205 0.6741514825670701 0.6463018489792469 0.9167355040363546 0.8598449762431178 0.5942157494465867 0.2292371409530619 0.13034983235083675 0.039077453703093346 0.05818849920983282 0.9727255850775609 0.9377026388429309 0.8021042842044926 0.8511548848938554 0.395007660806718];


n_ir = 40;
n_sig = 100;
n_segment = 40;
N = n_ir + n_segment -1

%===================
% Segmentiertes Signal
%===================

for index = n_segment*0+1 : n_segment*1
	sigT_1(index-n_segment*0) = sigT(index);
end
for index = n_segment*1+1 : n_segment*2
	sigT_2(index-n_segment*1) = sigT(index);
end
for index = n_segment*2+1 : 100
	sigT_3(index-n_segment*2) = sigT(index);
end
for index = 21 : 40 % auf die vollen 40 auffüllen
    sigT_3(index) = 0;
end

sigT_1
sigT_2
sigT_3


%===================
% Signal transformieren und trennen
%===================

irF = fft (irT, N);
sigF_1 = fftshift( fft(sigT_1, N));
sigF_2 = fftshift(fft(sigT_2, N));
sigF_3 = fftshift(fft(sigT_3, N));

% trennen von irF in zwei Signale
% x(k) = 1/2 [zr(k)+zr(n-k)] + j 1/2 [zi(k)-zi(n-k)]
% den ersten Wert direkt übernehmen (DC-Wert)
% Realteil für linkes Signal, Imaginärteil für rechtes Signal

%linksIRF(1) = real(irF(1));
%rechtsIRF(1) = imag(irF(1));

%linksSIGF_1(1) = real(sigF_1(1));
%rechtsSIGF_1(1) = imag(sigF_1(1));

%linksSIGF_2(1) = real(sigF_2(1));
%rechtsSIGF_2(1) = imag(sigF_2(1));

%linksSIGF_2(1) = real(sigF_2(1));
%rechtsSIGF_2(1) = imag(sigF_2(1));


for index = 1:N
    linksIRF(index) =  0.5 * (irF(index) + conj(irF(N+1-index)) );
    rechtsIRF(index) = 1/(2 * i) * (irF(index) - conj(irF(N+1-index)) );
    
    linksSIGF_1(index) = 0.5 * (sigF_1(index) + conj(sigF_1(N+1-index)) );
    rechtsSIGF_1(index) = 1/(2 * i) * (sigF_1(index) - conj(sigF_1(N+1-index)) );

	linksSIGF_2(index) = 0.5 * (sigF_2(index) + conj(sigF_2(N+1-index)) );
    rechtsSIGF_2(index) = 1/(2 * i) * (sigF_2(index) - conj(sigF_2(N+1-index)) );

    linksSIGF_3(index) = 0.5 * (sigF_3(index) + conj(sigF_3(N+1-index)) );
    rechtsSIGF_3(index) = 1/(2 * i) * (sigF_3(index) - conj(sigF_3(N+1-index)) );
end

%linksIRF
%rechtsIRF
%linksSIGF_1
%rechtsSIGF_1
%linksSIGF_2
%rechtsSIGF_2
%linksSIGF_3
%rechtsSIGF_3


%===================
% multiplizieren
%===================

linksMF_1 = linksIRF.* linksSIGF_1;
rechtsMF_1 = rechtsIRF.* rechtsSIGF_1;

linksMF_2 = linksIRF.* linksSIGF_2;
rechtsMF_2 = rechtsIRF.* rechtsSIGF_2;

linksMF_3 = linksIRF.* linksSIGF_3;
rechtsMF_3 = rechtsIRF.* rechtsSIGF_3;


%===================
% Signal für IFFT zusammenfügen
%===================
linksMF_1 = ifftshift(linksMF_1 );
rechtsMF_1 = ifftshift(rechtsMF_1);

linksMF_2 = ifftshift(linksMF_2 );
rechtsMF_2 = ifftshift(rechtsMF_2 );

linksMF_3 = ifftshift(linksMF_3 );
rechtsMF_3 = ifftshift(rechtsMF_3 );



%z(n) = xr(n)-yi(n) + j[xi(n) + yr(n)]
for index = 1:N
    ergMF_1(index) = real(linksMF_1(index)) - imag(rechtsMF_1(index)) + i* (imag(linksMF_1(index)) + real(rechtsMF_1(index)));
    ergMF_2(index) = real(linksMF_2(index)) - imag(rechtsMF_2(index)) + i* (imag(linksMF_2(index)) + real(rechtsMF_2(index)));
    ergMF_3(index) = real(linksMF_3(index)) - imag(rechtsMF_3(index)) + i* (imag(linksMF_3(index)) + real(rechtsMF_3(index)));
end



%===================
% IFFT
%===================

ergMT_1 = ifft(ergMF_1, N);
ergMT_2 = ifft(ergMF_2, N);
ergMT_3 = ifft(ergMF_3, N);


%===================
% Ergebnis überlagern
%===================

ergMT = ergMT_1;

for i =  42 : 79
    ergMT(i) = ergMT(i) + ergMT_2(i - 40);
end
for i = 80 : 119
    ergMT(i) = ergMT_2(i -40);
end
for i = 81 : 119
    ergMT(i) = ergMT(i) + ergMT_3(i-80);
end
for i = 120 : 159
    ergMT(i) = ergMT_3(i-80);
end



%===================
% richtiges Ergebnis
%===================

ergConv = conv(irT, sigT);
ergL = conv(irL, sigL);
ergR = conv(irR, sigR);
