%This demonstrates the small set of Kasami seqences. Kasami sequences are
%sequences with good periodic auto-correlation and cross-correlation
%properties.
%For more details, see Pingzhui Fan and Michael Darnell, "Sequence Design
% for communications applications," Research Studies Press, Tauton, 1996.
m=10;%order of sequence (must be even)
%Kasami sequences are generated from a maximal length sequence, a, of length
%2^m-1 where m is even. A second sequence, b, is generated by sampling a
%by a factor of 1+2^(m/2), which is a maximal length sequence of period
%2^(m/2)-1. Kasami sequences are the set of sequences of
%xor(a,circshift(b)). 2^(m/2) sequences are calculated.
%The function kasami() performs this calculation, with each row of K being
%a sequence.
K=kasami(m);
%Now we check the autocorrelation
[N_seq N]=size(K);
acorr=zeros(N_seq,N);
for i=1:N_seq
acorr(i,:)=xcorr_fft(K(i,:),K(i,:));
end
%The autocorrelation should have a peak of N at lag=0. All other values
%should be equal to one of the possible values of R_max:
R_max=-1+[0 -2^(m/2) 2^(m/2)];
figure(1)
plot(0:(N-1),acorr','.')
hold on
h=plot([0 N-1],[R_max;R_max],'k');
hold off
xlabel('Lag')
ylabel('Auto Correlation')
legend(h(1),'R_{max}')
%check cross-correlation for one sequence
k_check=2;%index of sequence to check
xcorr=zeros(N_seq,N);
for i=1:N_seq
xcorr(i,:)=xcorr_fft(K(i,:),K(k_check,:));
end
xcorr(k_check,:)=[];%clear autocorr
%Again, all values should be one of R_max
figure(2)
plot(0:(N-1),xcorr','.')
hold on
h=plot([0 N-1],[R_max;R_max],'k');
hold off
xlabel('Lag')
ylabel('Cross Correlation')
legend(h(1),'R_{max}')
%Now, a practical example. Can we pick out the delay of one sequence, when
%the other sequences are added to it (as noise).
k_target=2;%index of target sequence
delays=round(N*rand(1,N_seq));%delay to apply to each sequence
signal=zeros(1,N);
for i=1:N_seq
signal=signal+circshift(K(i,:),[0 delays(i)]);
end
sigma_noise=1;%add some Gaussian noise (this can be larger with larger m)
signal=signal+sigma_noise*randn(size(signal));
%the peak of the cross-correlation between the noisy signal and the target
%sequence should reveal the delay
xc_delay=xcorr_fft(signal,K(k_target,:));
figure(3)
plot(0:(N-1),xc_delay','.')
hold on
[xc_max idx_max]=max(xc_delay);
h2=plot(idx_max-1,xc_max,'ro');
h1=plot(delays(k_target)*[1 1],xc_max*[-1 1],'k');
hold off
xlabel('Lag')
ylabel('Cross Correlation')
legend([h1(1) h2],'Target Lag','Measured Lag','Location','Best')
%notice that although the noise (interfering signals plus Gaussian) is much
%larger than the signal, the larger number of samples allows us to pick out
%the transmitted signal.
%
%Copyright (c) 2009, Travis Wiens
%All rights reserved.
%
%Redistribution and use in source and binary forms, with or without
%modification, are permitted provided that the following conditions are
%met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
%THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
%AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
%IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
%LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
%CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
%SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
%CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
%ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%POSSIBILITY OF SUCH DAMAGE.
%
% If you would like to request that this software be licensed under a less
% restrictive license (i.e. for commercial closed-source use) please
% contact Travis at travis.mlfx@nutaksas.com