function [y] = MSK(winid)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input: winid - Windowing choice - 0 no window
% 1 Hamming
% 2 Kaiser with param pi
% 3 Chebychev side-lobes at -60dB
%
% output: y - normalized compressed output
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% MSK signal generation initialization %%%%%%%%%%%%
% assume information bit
% 13 sub-pulse
a=[1,1,1,1,1,-1,-1,1,1,-1,1,-1,1];
% subpulse duration of one information bit
Tb=2.5e-9;
% pulse duration (13 sub-pulse)
taup = length(a)*Tb;
% sampling number per code symbol, in totoal 10 symbols in this examples
Q=250;
% number of transmitted samples 13 sub-pulse
n=length(a)*Q;
% The central frquency should be fc=n/(4*Tb), n=1,2,3,...
% In this example n=20, fc=2e6
fc=5/4/Tb;
% sampling time per code symbol
ts=Tb/Q;
%--- msk_02 ---
%%%%%%%%%%%%%%%%%%%%%% Initialization of other ranging parameters %%%%%%%%%%%%
% receive window size - meters
rrec = 5;
% number of point scatters within the received window
nscat = 1;
% vectors of scatters' relative range
scat_range = [1.5];
% vector of scatterer' RCS
scat_rcs = [2];
eps=1.0e-16;
% speed of light
c=3.e8;
% check if the scatterers are within the recieve window
index=find(scat_range > rrec);
if (index~=0)
'Error. Scatterers fall outside Receive window'
return
end
%%%%%%%%% input, output and replica signal vectors parameters %%%%%%%%%
x(nscat, 1:n)=0.;
y(1:n)=0.;
replica(1:n)=0.;
%%%%%%%%%%%%%%%%%%%%%%%%% Various windowing set %%%%%%%%%%%%%%%%
if (winid == 0.)
win(1:n)=1.;
end
if (winid == 1.)
win=hamming(n)';
end
if (winid == 2.)
win=kaiser(n,pi)';
end
if (winid == 3.)
win=chebwin(n,60)';
end
%%%%%%%%%%%%%% replica of transmission signal vector %%%%%%%%%%%%%%%%%%%%%%%
%--- msk_02 ---
%% calculate sampling interval
% t=linspace(0, taup, n);
p=pi/2/Q;
pha(1)=0;
% assume Q=3, data=[1 -1], then data_diff=[1 1 1 -1 -1 -1]
for k=1:length(a)
data_diff((k-1)*Q+1:k*Q)=a(k);
end
t1=ts:ts:length(data_diff)*ts;
% length(data_diff)=Q*length(a)
for j=2:1:length(data_diff)
pha(j)=pha(j-1)+p*data_diff(j);
msk_signal(j)=cos(2*pi*fc*t1(j)+pha(j));
end
% transmit n msk subpulse
msk_signal1=2*pi*fc*t1+pha;
replica=exp(i*msk_signal1);
figure(1);
subplot(2,1,1)
plot(t1,real(replica));
ylabel('Real part of replica')
xlabel('time in seconds')
% set(gca, 'XLim', [-3e-6 3e-6]);
grid
subplot(2,1,2)
sampling_interval=taup/n;
freqlimit=0.5/sampling_interval;
freq=linspace(-freqlimit, freqlimit, n);
plot(freq, fftshift(abs(fft(replica))));
ylabel('Spectrum of replica')
xlabel('Freq in Hz')
set(gca, 'XLim', [-5e9 5e9]);
grid
%--- msk_02 ---
%%%% --- TEST ---- autocorrelation of transmitted signal
y1=replica.*win;
figure(2)
[out,tau]=xcorr(replica, y1);
plot(tau,out)
grid on
% %%%%%%%%%%%%%%%%%%%% received signal vector %%%%%%%%%%%%%%%%%%%%%%%
% %--- msk_02 ---
% for z=1:1:nscat
% range=scat_range(z);;
% delay=2*range/c;
% delt=ceil(n*(delay/taup));
% delayed_sig(1:n) = 2*pi*fc*t1(1+delt:n+delt)+pha(1+delt:n+delt);
% x(z,:)=scat_rcs(z).*exp(i*delayed_sig);
% y=x(z,:)+y;
% end
% %--- msk_02 ---
%
% %%%%%%%%%%%%%%%%%%%% match filter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% y=y.*win;
% figure(3)
% plot(t, real(y), 'k')
% xlabel('Relative delay - scecond')
% ylabel('Uncompressed echo')
% % set(gca, 'XLim', [-3e-6 3e-6]);
% grid
%
% %--- match filter output ---
% % [out2,tau2]=xcorr(replica, y);
% % plot(tau2, 10*log(abs(out2)))
% out=xcorr(replica, y);
% out=out./n;
% s=taup*c/2;
% Npoints = ceil(rrec*n/s);
% dist = linspace(0, rrec, Npoints);
% % delr=c/2/b;
% delr=c*Tb/2
% figure(4)
% plot(dist, 10*log(abs(out(n:n+Npoints-1))), 'k')
% xlabel('Target relative position in meters')
% ylabel('Echo amplitude in dB')
% grid