%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Frequency Hopping Spread Spectrum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
close all
clear all
n_bits = 80;
% Generation of bit pattern
s=round(rand(1,n_bits))   % Generating n bits
tp=641;
%
signal=[];  
carrier=[];
t=[0:100];   
fc=.01;
for k=1:n_bits
    if s(1,k)==0;
        sig=-ones(1,101);    % 1001 minus ones for bit 0
    else
        sig=ones(1,101);     % 1001 ones for bit 1
    end
    c=cos(2*pi*fc*t);   
    carrier=[carrier c];
    signal=[signal sig];
end

%
%MFSK of the bit pattern
% Preparation of 4 tons (frequencies)
t0=[0:2*pi/320:2*pi*4];
t1=[0:2*pi/160:2*pi*8];
t2=[0:2*pi*3/320:2*pi*12];
t3=[0:2*pi/80:2*pi*16];
ton0=sin(t0);
ton1=sin(t1);
ton2=sin(t2);
ton3=sin(t3);
%

%MSK modulation of the bit pattern
%bits are gouped 2 by 2 and modulated in a 4 -MSK signal
s_fsk=[];

msk=zeros(1,tp*n_bits/2);

for i=1:n_bits/2
	i
	s_fsk(i)=s(2*i-1)*2+s(2*i)
	sw=s(2*i-1)*2+s(2*i)
switch(sw);
        case(0)
        	for j=1:tp
            	msk(1,j+tp*(i-1))=ton0(1,j);
            	end
        case(1)
            	for j=1:tp
            	msk(1,j+tp*(i-1))=ton1(1,j);
            	end
        case(2)
        	for j=1:tp
            	msk(1,j+tp*(i-1))=ton2(1,j);
            	end
        case(3)
        	for j=1:tp
            	msk(1,j+tp*(i-1))=ton3(1,j);
            	end
          end
end
%
figure (1);
subplot(2,1,1);
plot(signal,'LineWidth',2);
axis([-1 100*n_bits -1.5 1.5]);
title('Original Bit Sequence');

%
figure(2);
subplot(2,2,1);
plot(ton0, 'k');
title('Ton #1');
subplot(2,2,2);
plot(ton1, 'r');
title('Ton #2');
subplot(2,2,3);
plot(ton2,'g');
title('Ton #3');
subplot(2,2,4);
plot(ton3,'b');
title('Ton #4');
figure(3);
plot(abs(fft(ton0))/tp,'k')
hold on
plot(abs(fft(ton1))/tp,'r')
hold on
plot(abs(fft(ton2))/tp,'g')
hold on
plot(abs(fft(ton3))/tp,'b')
axis([0 tp/2 0 1]);
title('Four tons spectra');
%
figure(4);
subplot(2,1,2);
plot(msk);
axis([-1 tp*10 -1.5 1.5]);
title('MSK signal (partial)');
subplot(2,1,1);
plot(signal,'LineWidth',2);
axis([-1 25*n_bits -1.5 1.5]);
title('Original Bit Sequence (partial)');
%subplot(2,1,2);
%plot(s_fsk);
%


% Preparation of 6 new carrier frequencies

spread_signal=[];
pn=[1, 6, 3, 1, 1,2,4, 5,4 6,3,2, 4,6, 5, 2, 3, 1, 5, 4]

for n=1:n_bits/4
    c=pn(n)
    switch(c)
        case(1)
           	 for j=1:tp
            	c1(1,j)=cos(2*pi*120*j/tp);
            	end
            	spread_signal=[spread_signal c1 c1];
        case(2)
           	 for j=1:tp
            	c2(1,j)=cos(2*pi*140*j/tp);
            	end
            	spread_signal=[spread_signal c2 c2];
        case(3)
           	 for j=1:tp
            	c3(1,j)=cos(2*pi*160*j/tp);
            	end
            	spread_signal=[spread_signal c3 c3];
        case(4)
           	 for j=1:tp
            	c4(1,j)=cos(2*pi*180*j/tp);
            	end
            	spread_signal=[spread_signal c4 c4];
        case(5)        
           	 for j=1:tp
            	c5(1,j)=cos(2*pi*200*j/tp);
            	end
            	spread_signal=[spread_signal c5 c5];
        case(6)        
           	 for j=1:tp
            	c6(1,j)=cos(2*pi*220*j/tp);
            	end
            	spread_signal=[spread_signal c6 c6];         	
    end
end

%for n=1:5
    %c=pn(n)
    %switch(c)
        %case(1)
            %spread_signal=[spread_signal c1];
        %case(2)
            %spread_signal=[spread_signal c2];
        %case(3)
            %spread_signal=[spread_signal c3];
        %case(4)
            %spread_signal=[spread_signal c4];
        %case(5)        
            %spread_signal=[spread_signal c5];
        %case(6)
            %spread_signal=[spread_signal c6];
    %end
%end
figure(6)
subplot(3,2,1)
plot(c1)
title('Carrier Frequencies');
subplot(3,2,3)
plot(c3)
subplot(3,2,5)
plot(c6)
subplot(3,2,2)
plot(abs(fft(c1,tp))/tp);
axis([0 tp/2 0 1]);
title('FFT of Carriers');
subplot(3,2,4)
plot(abs(fft(c3,tp))/tp);
axis([0 tp/2 0 1]);
subplot(3,2,6)
plot(abs(fft(c6,tp))/tp);
axis([0 tp/2 0 1]);


figure(7)
subplot(2,1,1)
plot(spread_signal);
axis([-1 6410 -1.5 1.5]);
title('Spread Signal with 6 frequencies');

% Spreading FSK Signal into wider band with total of 12 frequencies
%spread_signal=[spread_signal zeros (1,100)
freq_hopped_sig=msk.*spread_signal;
subplot(2,1,2)
plot(freq_hopped_sig);
axis([-1 6410 -1.5 1.5]);
title('Frequency Hopped Spread Spectrum Signal');

% Expressing the FFTs
figure(8)
subplot(2,1,1)
plot(freq_hopped_sig);
%axis([-100 3100 -1.5 1.5]);
title('Frequency Hopped Spread Spectrum signal and its FFT');
subplot(2,1,2);
plot(abs(fft(freq_hopped_sig))/(tp*n_bits/2));
axis([0 tp*n_bits/4]);


