function soundWave = modulateData2(data, pulses, Tp, startPulse, startDistance, padding)

%MODULATEDATA2 Creates a playable sound wave based on data and symbols that
%             should be used to represent that data. The audio can then be
%             played, recorded and demodulated by the demodulateSound function.
%
%  PARAMETERS:
%   data:
%       Vector of data to send. The data should be a sequence of integers
%       from 0 and higher (0 or 1 for bionary data).
%   pulses: 
%       Cell array of pulses to represent the data. The length of the array
%       needs to be at least as long as the amount of different data values.
%       The data values are mapped to the symbols and put on the final sound
%       wave.  Data = 0 is represented by pulses{1}, and so on.
%   Tp:
%       Amount of samples between two data symbols. If this is shorter than the
%       symbol length, the samples will overlap. 
%   startPulses (optional): 
%       The audio 'symbol' to represent the start of the message. Note: If this
%       is used, startPadding is also required
%   startPadding (optional):
%       Amount of samples between the start symbol and first data symbol
%   padding (optional):
%       The amount of zeros to add at the start and end of the sound wave
%   
%  RETURNS:
%   soundWave:
%       Final sound wave that can be played on the speaker
%   
%  See also:
%   DEMODULATESOUND
%   SOUNDMODULATION

%Default parameters
DEFAULT_PADDING = 16000;
switch nargin
    case 3 %No start symbol, no startDistance, no padding
        startPulse = [];
        startDistance = 0;
        padding = DEFAULT_PADDING;
    case 4 %No startDistance, no padding
        startDistance = 2*Tp;
        padding = DEFAULT_PADDING;
    case 5 %no padding specified
        padding = DEFAULT_PADDING;
    case 6 %All parameters specified, keep going
    otherwise
        error('Failed to modulate data, wrong amount of parameters');
end


figure(1);clf;

% Start with empty signal
soundWave = zeros(1,padding + startDistance + length(data) * Tp + padding);

% A period of length Tp that starts with an impulse
imp = [1 zeros(1,Tp-1)];

% Add pulses for each type of symbol in the data
for symb=1:length(pulses),
  imps=[zeros(1,padding) zeros(1,startDistance) kron(data==(symb-1),imp) zeros(1,padding)];
  soundWave=soundWave+conv(imps,pulses{symb},'same');
  subplot(length(pulses),1,symb);
  plot(pulses{symb},'LineWidth',2);
  title(['puls ' num2str(symb-1)]);
end

% Add also startPulse if specified
if length(startPulse>0),
  imps=[zeros(1,padding) 1 zeros(1,startDistance-1) zeros(1,length(data)*Tp) zeros(1,padding)];
  soundWave=soundWave+conv(imps,startPulse,'same');
  figure(2);clf;
  plot(startPulse,'LineWidth',2);
  title('Startpuls');
end

