% Oppgave 5
f0=2e6;%center frequency 
Tp=0.35e-6;%pulse length rms 
fs=50e6;%sampling frequency 
T=2e-6;%time interval [-T,T] 
t=-T:1/fs:T;%time vector 
s=exp(-1/2*(t/Tp).^2).*cos(2*pi*f0*t);%Gaussian signal envelope
figure(1);subplot(3,1,1); plot(t,s);
S=fftshift(abs(fft(s)));
f=linspace(-fs/2,fs/2,length(S));
subplot(3,1,2);plot(f,S);grid;

dec=2;%decimation factor
sd=s(1:dec:end);%decimated signal
sz=zeros(1,length(s)); sz(1:dec:end)=s(1:dec:end);%sz zero padded between decimated samples
subplot(3,1,1);plot(t,s,t,sz);
Sz=fftshift(abs(fft(sz)));
subplot(3,1,2);plot(f,S,f,Sz);grid;
tdec=-T:dec/fs:T;
sinterp=interp1(tdec,sd,t,'nearest',0);
err=sinterp-s;
subplot(3,1,3);plot(t,sinterp,t,err);
sumerr=sum(err.^2)
relerr=sumerr/sum(s.^2)
