function s=sinchips(chips, chipDuration, t, start)
% creates a vector of +1 or -1 samples of the given chips at the
% prescribes times

sampleIndexes = floor((t-start)./chipDuration)+1;
s = .33*zeros(size(t));
validIndices = find((sampleIndexes>0) .* (sampleIndexes<=length(chips)));
%s(validIndices) = chips(sampleIndexes(validIndices));

% now smooth
for i=1:length(t)
    tadj = t(i)-chipDuration/2;
    sampleIndex = floor((tadj-start)./chipDuration)+1;
    if sampleIndex<0 || sampleIndex>length(chips)+1; s(i)=0; continue; end
    offset = (tadj - (start + (sampleIndex-1)*chipDuration))/chipDuration;
    %disp([i t(i) sampleIndex offset])
    %    disp([ i t(i) tadj sampleIndex offset ]);
    if sampleIndex==0
        if offset>0.5
          offset = -(1-offset);
          s(i) = chips(1) * (0.5+0.5*sin(offset*pi));
        else
          s(i) = 0;
        end
    end
    if sampleIndex==length(chips)+1
        if offset>0.5
          s(i) = 0;
        else
          s(i) = chips(length(chips)) * (0.5-0.5*sin(offset*pi));
        end
    end
    if sampleIndex==1
        if offset>0.5
            offset = -(1-offset);
            if chips(sampleIndex)< chips(sampleIndex+1); s(i)= sin(offset*pi);    end
            if chips(sampleIndex)> chips(sampleIndex+1); s(i)=-sin(offset*pi);    end
            if chips(sampleIndex)==chips(sampleIndex+1); s(i)=chips(sampleIndex); end
        else
            s(i) = chips(sampleIndex) * (0.5+0.5*sin(offset*pi));
        end
    end
    if sampleIndex==length(chips)
        if offset>0.5
            offset = -(1-offset);
            s(i) = chips(sampleIndex) * (0.5-0.5*sin(offset*pi));
        else
            if chips(sampleIndex)< chips(sampleIndex-1); s(i)=-sin(offset*pi);    end
            if chips(sampleIndex)> chips(sampleIndex-1); s(i)= sin(offset*pi);    end
            if chips(sampleIndex)==chips(sampleIndex-1); s(i)=chips(sampleIndex); end
        end
    end
    if sampleIndex>1 && sampleIndex<=length(chips)-1
        if (offset>0.5);
            offset = -(1-offset);
            if chips(sampleIndex)< chips(sampleIndex+1); s(i)= sin(offset*pi);    end
            if chips(sampleIndex)> chips(sampleIndex+1); s(i)=-sin(offset*pi);    end
            if chips(sampleIndex)==chips(sampleIndex+1); s(i)=chips(sampleIndex); end
        else
            if chips(sampleIndex)< chips(sampleIndex-1); s(i)=-sin(offset*pi);    end
            if chips(sampleIndex)> chips(sampleIndex-1); s(i)= sin(offset*pi);    end
            if chips(sampleIndex)==chips(sampleIndex-1); s(i)=chips(sampleIndex); end
        end
        %if (sampleIndex==2);    disp([i t(i) sampleIndex offset s(i)]);end
        %disp([i t(i) sampleIndex offset direction s(i)])
    end
end

%plot(s)
%ylim([-1.25 1.25]);
%title(chips')
end


