1;

clear;

samples=200;
bStart=100;          #Einfügepunkt für den Barker-Code
bCode=[+1,+1,+1,+1,+1,-1,-1,+1,+1,-1,+1,-1,+1]; #Barker-Code
sFactor=.4;           #Abschwächung für das Signal

function [retval] = korrelation (pattern, signal)
  for i = 0:length(signal)
    retval(end+1) = corr(shift(pattern, i-(floor(length(pattern)/2))-1), signal);
  endfor
endfunction

function [encoded] = manchester_encode(source)
  encoded = [];
  for i=1:length(source)
    encoded(2*i - 1) = (source(i) == 1);
    encoded(2*i) = (source(i) == 0);
  end
endfunction

#generiere "noise"
x=rand(1,samples).-0.5;

#setze den bCode-code in den noise ein
sCode = zeros(1, samples);
sCode(bStart:bStart+(length(bCode)-1)) = bCode;
signal = x .+ sCode * sFactor;

subplot(2,2,1);
plot (signal,"b",[bStart,bStart+length(bCode)-1,bStart+length(bCode)-1,bStart,bStart],[1,1,-1,-1,1],"r");title("Korrelationsergebnis");
title("Signalverlauf Barker");
axis([0 samples -1.5 1.5]);
grid on;

subplot(2,2,3);
plot(korrelation(sCode, signal));
title("Korrelation Barker");
axis([0 samples]);
grid on;

#setze den Manchester-Codierten bCode-code in den noise ein
sCode = zeros(1, samples);
mCode = manchester_encode(bCode);
sCode(bStart:bStart+(length(mCode)-1)) = mCode;
signal = x .+ sCode * sFactor;

subplot(2,2,2);
plot (signal,"b",[bStart,bStart+length(mCode)-1,bStart+length(mCode)-1,bStart,bStart],[1,1,-1,-1,1],"r");title("Korrelationsergebnis");
title("Signalverlauf Manchester(Barker)");
axis([0 samples -1.5 1.5]);
grid on;

length(signal)
subplot(2,2,4);
plot(korrelation(sCode, signal));
title("Korrelation Manchester(Barker)");
axis([0 samples]);
grid on;
print ("png","Barker.png");