%==========================================================================
% Author: Bernhard Schauer
% bernhard.schauer@gmx.net
%==========================================================================


% implementierung Segmentierte Faltung
clc
clear all

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


%===================
% Eingangssignale
%===================
irT = [0.9678012807769817+0.39615578311169164i 0.030029826416134053+0.3113383407364506i 0.5542324238627486+0.13157917880331693i 0.47690434040269547+0.23581578665634473i 0.3305291664182063+0.5616904701419418i 0.5206861734923335+0.12604811570891483i 0.5286503164181257+0.9519618330152416i 0.22973265269199217+0.9126046821195585i 0.8249398319451722+0.03962735713165144i 0.40015249412341214+0.6376971227852601i 0.12343984379830608+0.0008209181643814567i 0.9159760064933895+0.7464620674981332i 0.7620603013648669+0.3298359105970782i 0.9138498479987505+0.43582781544356686i 0.17959918322378998+0.8855422458822668i 0.4215981216448138+0.857503972540963i 0.8208655640679101+0.4820660768081386i 0.4573749294082867+0.79682903389752i 0.20266573680993438+0.17915845862333168i 0.7997657351821611+0.43065541059870893i 0.615170068070564+0.862249393499041i 0.7690783494835474+0.9051475397044974i 0.8911860914988063+0.7930036806328162i 0.3961966463985527+0.7683133365130097i 0.04012160595070424+0.5582655441838041i 0.29775293901248756+0.7422850609045047i 0.31392875433740175+0.2485995839319809i 0.425008407739335+0.9268106781326938i 0.449078285208074+0.7572053248067779i 0.6087128613473379+0.28811223558390975i 0.41162083353235346+0.8058034087797146i 0.48334857212395543+0.2062130398298434i 0.9616500384636582+0.6761616518346576i 0.9415454611381928+0.9290307560762426i 0.7489897798319043+0.6542494801727126i 0.743087977255257+0.012979024462756672i 0.45368610283597877+0.8785794846625599i 0.2828163925423738+0.21751490052833888i 0.69289170884256+0.6251154676484612i 0.807858402190417+0.8650157796092416i ]

irL = [ 0.9678012807769817 0.030029826416134053 0.5542324238627486 0.47690434040269547 0.3305291664182063 0.5206861734923335 0.5286503164181257 0.22973265269199217 0.8249398319451722 0.40015249412341214 0.12343984379830608 0.9159760064933895 0.7620603013648669 0.9138498479987505 0.17959918322378998 0.4215981216448138 0.8208655640679101 0.4573749294082867 0.20266573680993438 0.7997657351821611 0.615170068070564 0.7690783494835474 0.8911860914988063 0.3961966463985527 0.04012160595070424 0.29775293901248756 0.31392875433740175 0.425008407739335 0.449078285208074 0.6087128613473379 0.41162083353235346 0.48334857212395543 0.9616500384636582 0.9415454611381928 0.7489897798319043 0.743087977255257 0.45368610283597877 0.2828163925423738 0.69289170884256 0.807858402190417]

irR = [ 0.39615578311169164 0.3113383407364506 0.13157917880331693 0.23581578665634473 0.5616904701419418 0.12604811570891483 0.9519618330152416 0.9126046821195585 0.03962735713165144 0.6376971227852601 0.0008209181643814567 0.7464620674981332 0.3298359105970782 0.43582781544356686 0.8855422458822668 0.857503972540963 0.4820660768081386 0.79682903389752 0.17915845862333168 0.43065541059870893 0.862249393499041 0.9051475397044974 0.7930036806328162 0.7683133365130097 0.5582655441838041 0.7422850609045047 0.2485995839319809 0.9268106781326938 0.7572053248067779 0.28811223558390975 0.8058034087797146 0.2062130398298434 0.6761616518346576 0.9290307560762426 0.6542494801727126 0.012979024462756672 0.8785794846625599 0.21751490052833888 0.6251154676484612 0.8650157796092416]




sigT = [0.029989335313282406+0.05819613497846221i 0.5670098110812746+0.3775636273842251i 0.4423072772701626+0.13024456487475566i 0.47371541363239933+0.5586606027585801i 0.030539054229741902+0.6226031861358589i 0.8689845825766529+0.20059471008766339i 0.5859865070329315+0.2856549629498606i 0.8817083407131737+0.7671201889338516i 0.8710032215105767+0.6584612642109371i 0.7012279878642703+0.22631185658775776i 0.11352383571001712+0.42646765233600514i 0.8006378604514469+0.589898616203276i 0.26832151423318595+0.11039874223309698i 0.7791877649581387+0.35307631330028i 0.05564460100859803+0.4702839646032281i 0.028283140106735605+0.8227453470784618i 0.6521405695557736+0.2556259694424061i 0.8679644069475275+0.27231415887943966i 0.17948403654739176+0.8360442092812285i 0.14937920487704393+0.9676544977204119i 0.8128822586565655+0.8062959953041021i 0.5736441922215422+0.3714903329673388i 0.4146405007296712+0.8753983029061769i 0.1386262163916553+0.2524466753029134i 0.08722677292399783+0.22164308956109224i 0.19255484295168623+0.0001810299589299902i 0.9357835019135945+0.02362003613278596i 0.5470117104259082+0.1794644640908103i 0.6317913407880728+0.061930348429528825i 0.9817246386045048+0.24377525708055803i 0.0775984875221013+0.12709588344917555i 0.259727009902892+0.37291309155078556i 0.5857861423664248+0.46623779997711845i 0.46382650043511175+0.599337209171648i 0.5078491614658587+0.6544199939142523i 0.0639591372804117+0.7443772134911187i 0.17342833717284045+0.08612181450150413i 0.629888149405182+0.19816044573213254i 0.3343386493209982+0.748126110992943i 0.51483958394919+0.2460453271963038i 0.04689580931467774+0.057177583125667675i 0.15304954800240378+0.4695452469536986i 0.07155096853912224+0.32408818656887506i 0.8378308360575628+0.7311729229163474i 0.42060161248215433+0.8405354778782709i 0.9766491232857456+0.39033042487009306i 0.6427539219656457+0.6756205936720348i 0.8558324390717283+0.691315592103989i 0.7975705626937571+0.016323019222981072i 0.8899196761597291+0.7147105714637929i 0.8433445813713073+0.28930926341674046i 0.1792872695188087+0.8898635380357901i 0.33869899835364625+0.361123107842506i 0.9545755561584474+0.8288704892508566i 0.9412186953397539+0.8296051498763627i 0.14756971426539778+0.16140843153885165i 0.17567923754169168+0.7914602838942869i 0.8955720886989076+0.12496752541094425i 0.33994546388751545+0.4992110131140014i 0.25993676105498054+0.8964742139212992i 0.2183697148391136+0.00534605052318593i 0.8193186478199225+0.038993458126773906i 0.48052841066224317+0.23229299115077695i 0.08432341008425426+0.9811427972788758i 0.2573922158516556+0.9039145107580533i 0.8726266221364498+0.1470469169703209i 0.4075342644821087+0.427548152256621i 0.8822634773969631+0.21110607801126768i 0.41273743760241954+0.125179466336223i 0.7056734807610895+0.9645477699331929i 0.6753705438093321+0.17815913326677624i 0.43821713282004926+0.44967548939198354i 0.22916009179061814+0.2719446373647523i 0.85226419437322+0.34292466010416256i 0.5204933461388045+0.20566747999198332i 0.8315865933980233+0.049280263156254445i 0.9691447418354695+0.6658976223712311i 0.39067119583573895+0.6083068042163243i 0.47532678840341347+0.9547962664343427i 0.13970081586145322+0.30598575261064365i 0.9042237918671653+0.7099194290163333i 0.4983344261125343+0.8367516905522748i 0.8496746541533007+0.5562541974726966i 0.3100508698557981+0.9665630691852211i 0.6554286919242679+0.19064054519822193i 0.7637022103023967+0.9105393637466881i 0.7554065355262674+0.6145515109591971i 0.6900569174228726+0.05764110926050969i 0.8836557700688864+0.10327476242279043i 0.3684946660046903+0.25006516494268405i 0.6001986755892703+0.027859503911297212i 0.9526482941966513+0.556472528663862i 0.8807277482284076+0.8307620862483014i 0.50367072410862+0.3658615775884594i 0.2261115531990623+0.2969447810517911i 0.40593772956441065+0.6728445405017986i 0.8619325089373227+0.7592520872394384i 0.6323799650325133+0.6252808908683855i 0.1187849151508209+0.030209372378307564i 0.9252711043973673+0.7616661309758697i 0.010819923918767183+0.6619588111724924i 0.4631806112752743+0.8783698302439429i 0.36303190288204823+0.6018681830933319i 0.9959339698663267+0.13576274021192558i 0.5770383638333801+0.43341831135405584i 0.7468047228442363+0.8374670251830901i 0.27061329887554986+0.17029475121280613i 0.6047960098796645+0.2984097717823583i 0.577992095015868+0.006180599797781072i 0.2597523462035034+0.3550552160620758i 0.855973189851489+0.5135667985361256i 0.969174722813217+0.3651359257971354i 0.9051004747217268+0.5319667150431255i 0.5879951492169455+0.994398943658422i 0.5857172134533539+0.38924278126357836i 0.34119966153159564+0.6943281141239619i 0.885440724399212+0.1593222786775479i 0.5740714044886476+0.5374990346232206i 0.0776149462881578+0.37540051616211456i 0.37595839561179434+0.9961685279302951i ]

sigL = [ 0.029989335313282406 0.5670098110812746 0.4423072772701626 0.47371541363239933 0.030539054229741902 0.8689845825766529 0.5859865070329315 0.8817083407131737 0.8710032215105767 0.7012279878642703 0.11352383571001712 0.8006378604514469 0.26832151423318595 0.7791877649581387 0.05564460100859803 0.028283140106735605 0.6521405695557736 0.8679644069475275 0.17948403654739176 0.14937920487704393 0.8128822586565655 0.5736441922215422 0.4146405007296712 0.1386262163916553 0.08722677292399783 0.19255484295168623 0.9357835019135945 0.5470117104259082 0.6317913407880728 0.9817246386045048 0.0775984875221013 0.259727009902892 0.5857861423664248 0.46382650043511175 0.5078491614658587 0.0639591372804117 0.17342833717284045 0.629888149405182 0.3343386493209982 0.51483958394919 0.04689580931467774 0.15304954800240378 0.07155096853912224 0.8378308360575628 0.42060161248215433 0.9766491232857456 0.6427539219656457 0.8558324390717283 0.7975705626937571 0.8899196761597291 0.8433445813713073 0.1792872695188087 0.33869899835364625 0.9545755561584474 0.9412186953397539 0.14756971426539778 0.17567923754169168 0.8955720886989076 0.33994546388751545 0.25993676105498054 0.2183697148391136 0.8193186478199225 0.48052841066224317 0.08432341008425426 0.2573922158516556 0.8726266221364498 0.4075342644821087 0.8822634773969631 0.41273743760241954 0.7056734807610895 0.6753705438093321 0.43821713282004926 0.22916009179061814 0.85226419437322 0.5204933461388045 0.8315865933980233 0.9691447418354695 0.39067119583573895 0.47532678840341347 0.13970081586145322 0.9042237918671653 0.4983344261125343 0.8496746541533007 0.3100508698557981 0.6554286919242679 0.7637022103023967 0.7554065355262674 0.6900569174228726 0.8836557700688864 0.3684946660046903 0.6001986755892703 0.9526482941966513 0.8807277482284076 0.50367072410862 0.2261115531990623 0.40593772956441065 0.8619325089373227 0.6323799650325133 0.1187849151508209 0.9252711043973673 0.010819923918767183 0.4631806112752743 0.36303190288204823 0.9959339698663267 0.5770383638333801 0.7468047228442363 0.27061329887554986 0.6047960098796645 0.577992095015868 0.2597523462035034 0.855973189851489 0.969174722813217 0.9051004747217268 0.5879951492169455 0.5857172134533539 0.34119966153159564 0.885440724399212 0.5740714044886476 0.0776149462881578 0.37595839561179434]

sigR = [ 0.05819613497846221 0.3775636273842251 0.13024456487475566 0.5586606027585801 0.6226031861358589 0.20059471008766339 0.2856549629498606 0.7671201889338516 0.6584612642109371 0.22631185658775776 0.42646765233600514 0.589898616203276 0.11039874223309698 0.35307631330028 0.4702839646032281 0.8227453470784618 0.2556259694424061 0.27231415887943966 0.8360442092812285 0.9676544977204119 0.8062959953041021 0.3714903329673388 0.8753983029061769 0.2524466753029134 0.22164308956109224 0.0001810299589299902 0.02362003613278596 0.1794644640908103 0.061930348429528825 0.24377525708055803 0.12709588344917555 0.37291309155078556 0.46623779997711845 0.599337209171648 0.6544199939142523 0.7443772134911187 0.08612181450150413 0.19816044573213254 0.748126110992943 0.2460453271963038 0.057177583125667675 0.4695452469536986 0.32408818656887506 0.7311729229163474 0.8405354778782709 0.39033042487009306 0.6756205936720348 0.691315592103989 0.016323019222981072 0.7147105714637929 0.28930926341674046 0.8898635380357901 0.361123107842506 0.8288704892508566 0.8296051498763627 0.16140843153885165 0.7914602838942869 0.12496752541094425 0.4992110131140014 0.8964742139212992 0.00534605052318593 0.038993458126773906 0.23229299115077695 0.9811427972788758 0.9039145107580533 0.1470469169703209 0.427548152256621 0.21110607801126768 0.125179466336223 0.9645477699331929 0.17815913326677624 0.44967548939198354 0.2719446373647523 0.34292466010416256 0.20566747999198332 0.049280263156254445 0.6658976223712311 0.6083068042163243 0.9547962664343427 0.30598575261064365 0.7099194290163333 0.8367516905522748 0.5562541974726966 0.9665630691852211 0.19064054519822193 0.9105393637466881 0.6145515109591971 0.05764110926050969 0.10327476242279043 0.25006516494268405 0.027859503911297212 0.556472528663862 0.8307620862483014 0.3658615775884594 0.2969447810517911 0.6728445405017986 0.7592520872394384 0.6252808908683855 0.030209372378307564 0.7616661309758697 0.6619588111724924 0.8783698302439429 0.6018681830933319 0.13576274021192558 0.43341831135405584 0.8374670251830901 0.17029475121280613 0.2984097717823583 0.006180599797781072 0.3550552160620758 0.5135667985361256 0.3651359257971354 0.5319667150431255 0.994398943658422 0.38924278126357836 0.6943281141239619 0.1593222786775479 0.5374990346232206 0.37540051616211456 0.9961685279302951]

n_ir = length(irT);
n_sig = length(sigT);
n_segment = 40;
N = n_ir + n_segment -1

%debug
N_long = n_ir + n_sig -1
%debug


%===================
% 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 : n_segment*3
	sigT_3(index-n_segment*2) = sigT(index);
end

sigT_1;
sigT_2;
sigT_3;


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

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

testMult = irF.*sigF_1

%debug
irF_long = fftshift(fft(irT, N_long));
sigF = fftshift(fft(sigT, N_long));
%debug

% 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

%debug
for index = 1:N_long
    linksIRF_long(index) =  0.5 * (irF_long(index) + conj(irF_long(N_long +1-index)) );
    rechtsIRF_long(index) = 1/(2 * i) * (irF_long(index) - conj(irF_long(N_long +1-index)) );
    
    linksSIGF(index) = 0.5 * (sigF(index) + conj(sigF(N_long +1-index)) );
	rechtsSIGF(index) = 1/(2 * i) * (sigF(index) - conj(sigF(N_long +1-index)) );
end
%debug

%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;

%debug
linksMF = linksIRF_long.* linksSIGF;
rechtsMF = rechtsIRF_long.*rechtsSIGF;
%debug

%===================
% 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 );

%debug
linksMF = ifftshift(linksMF);
rechtsMF = ifftshift(rechtsMF);
%debug


%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

%debug
for index = 1: N_long
    ergMF(index) = real(linksMF(index)) - imag(rechtsMF(index)) + i* (imag(linksMF(index)) + real(rechtsMF(index)));
end
%debug


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

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

%debug
ergMT_debug = ifft(ergMF, N_long);
%debug

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

for index = 1: n_segment % für den ersten teil
    ergMT(index + n_segment*0) = ergMT_1(index);
end
for index = n_segment +1 : N % überlappen mit nächstem teil
    buffer(index - n_segment) = ergMT_1(index); 
end

for index = 1: length(buffer) % überlappung
    ergMT(index + n_segment*1) = ergMT_2(index) + buffer(index);
end
for index = length(buffer) +1 : n_segment % nach überlappung
    ergMT(index + n_segment*1) = ergMT_2(index);
end
for index = n_segment +1 : N % überlappen mit nächstem teil
    buffer(index - n_segment) = ergMT_2(index); 
end

for index = 1: length(buffer) % überlappung
    ergMT(index + n_segment*2) = ergMT_3(index) + buffer(index);
end
for index = length(buffer) +1 : n_segment % nach überlappung
    ergMT(index + n_segment*2) = ergMT_3(index);
end
for index = n_segment +1 : N % überlappen mit nächstem teil
    buffer(index - n_segment) = ergMT_3(index); 
end

l = length(ergMT);
% am ende des signals -> keine überlappung mehr
for index = 1 : length(buffer)
    ergMT(index + l) = buffer(index);
end




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

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