function [ SIG ] = createsig( dat, M, ts, sf, cf, fs )
%CREATESIG create signal from data
%   dat = data matrix a row is seen as a sequence of data to be transmitted
%         on one channel
%   M = bits per symbol
%   ts = symbol duration (1/ts = bandwidth separation for signal)
%   sf = safety factor (1/(sf*ts) = actual bandwidth separation
%   cf = center frequency

%dat = gf2dec(dat, M, primpoly(M))
[numchan, dlen] = size(dat);

%First figure out the bandwidth for each signal

bwsig = sf/(ts);

chanbw = 2^M*bwsig;
totbw = numchan*chanbw;

str = sprintf('Total required bandwidth = %f', totbw);
disp(str)


%Find channel center frequencies

%want ch1 + ch2 + ch3 + ch4 + ... + chn/n = cf
chanspace = totbw/numchan;

startbw = cf - totbw/2;

i = 1:numchan;
cbws = (i-1/2) * chanspace+startbw;
%for i = 0:numchan-1
%    cbws(i+1) = startbw + (i+1/2) * chanspace;
%end

%Find symbol freqs and create signal for each channel
numfreqs = 2^M;

symsam = 0:1/fs:ts-1/fs;
symlen = length(symsam);
sigmat = zeros(numchan, symlen*dlen);
for i = 1:numchan
    %cfreq = zeros(1, numfreqs)
    stfreq = cbws(i) - numfreqs*bwsig/2;
    j = 1:numfreqs;
    cfreq = stfreq + (j-0.5)*bwsig;
    disp(cfreq)
    for k = 1:dlen
%        disp(cfreq(dat(i,k)+1))
        sigmat(i,(k-1)*symlen+1:k*symlen) = cos(2*pi*cfreq(dat(i, k)+1)*symsam);
    end
end

%Final signal is sum of each channel.
SIG = sum(sigmat);
