User Tools

Site Tools


public:m_sequences_code:mtest.m
function output = mtest(mseqq,output,cb,auto)
% Function mtest(mseq)
%
% This tests a column vector to see if it is a M-sequence with the 
% following criteria:
% 
% 1. nth order counterbalancing: Each number is the nth number after each 
%     other number the same number of times.
%     The output is in the form of n 9x9 matrices in order from 1 to n.
%     If all the entries in these matrices are identical, the sequence is
%     nth order counterbalanced.
% 2. Autocorrelation: the sequence should not be correlated with itself
%     with respect to any phase shifts.
% 
% Imputs:
% 
% mseqq : a column vector whose integer values are centered around zero.
% output: if 0 (default) output is cb mx
%         if 1 then is sum(abs(autocorrs))
%         if 2 then output is figure and cbmx
%         if 3 then output is figure and sum(abs(autocorrs))
% cb    : highest order of counterbalancing to check for
% auto  : 1 to skip autocorrelation check
% 
% Wesley Kerr
% wesleytk@sas.upenn.edu
 
%% Checking for Errors and Prepping
 
if nargin<4
    auto=0;
end
if nargin<3
    cb=2;
end
if nargin<2
    output=0;
end
 
mlength=max(size(mseqq));
 
siz=size(mseqq);
if siz(1,2)>1
    error 'M-sequence is not a column vector.  Reshape.'
end
 
mlabelmax=max(mseqq);
mlabelmin=min(mseqq);
nlabels= abs(mlabelmax-mlabelmin)+1;
 
if mlabelmin>0
    error 'blank must be present'
end
 
%% nth order Counterbalancing
 
countbal= zeros(nlabels, nlabels*cb);
 
countercb=0;
for k=1:cb
    for i=1:(mlength-k)
        for j=mlabelmin:mlabelmax
            if mseqq(i+k,1)==j
                if mlabelmin<0
                    row=mseqq(i,1)+mlabelmax+1;
                    col=j+mlabelmax+1+countercb*nlabels;
                else
                    row=mseqq(i,1)+1;
                    col=j+1+countercb*nlabels;
                end
                countbal(row,col)=1+countbal(row,col);
            end
        end
    end
    countercb=countercb+1;
end
 
if output==0
    output= countbal;
elseif output==2
    output= countbal;
else
    countbal
end
 
%% Autocorrelation
if auto==1
else
    shiftedmseq=zeros(mlength,1);
    cor=zeros(mlength,1);
 
    for i=1:(mlength-1)
        for j=1:mlength
            shiftedmseq(j,1)=mseqq(mod(i+j-1,mlength)+1,1);
        end
        corre=corrcoef(shiftedmseq,mseqq);
        cor(i,1)=corre(1,2);
        if abs(corre(1,2))>0.15
            sprintf('Autocorrelated with self with correlation of %d at %d',corre(1,2),i)
        end
        clear shiftedmseq;
    end
    if output>1
        figure;
        plot(cor(:,1));
    end
    abssumcor=sum(abs(cor(:,1)));
    sumcor=sum(cor(:,1));
    if output==1
        output=abssumcor;
    elseif output==2
        output=abssumcor;
    else
        sumcor
        abssumcor
    end
end
public/m_sequences_code/mtest.m.txt · Last modified: 2011/12/13 18:12 by aguirreg