clear
Fs = 7000;
f = 50;
L = 500;
t = 0:1/Fs:(L-1)/Fs;
A = 2;
Anoise = 1e-2;


sig = A*sin(2*pi*t*f);
Psig=sum(sig.^2);
noise=Anoise*randn(size(sig));
Pnoise=sum(noise.^2);
sig=sig+noise;
Psn=sum(sig.^2);
n=length(sig);
%sig(n-1)+sig(n+1)=2*cos(w)*sig(n);
s1=sig(1:n-2)+sig(3:n);
s2=sig(2:n-1);
% Regression s1 auf s2
M= [s2.' ones(length(s1),1)];
coff=inv(M'*M)*M'*s1.';
wn=acos(coff(1)/2);

M=[cos((0:n-1)*wn).' sin((0:n-1)*wn).'];
coff=inv(M'*M)*M'*sig.';

cc=coff(2)+j*coff(1);
amp=abs(cc);
ph=angle(cc);

sigr=amp*sin(wn*(0:(L-1))+ph);
noiser=sig-sigr;
Pnoiser=sum(noiser.^2);
Psigr=sum(sigr.^2);
disp(sprintf('SNR vorgegeben: %.2fdB SNR bestimmt  %.2fdB', ...
    10*log10(Psig/Pnoise),10*log10(Psigr/Pnoiser)));
n=length(sig);
plot([sig ; sigr].')