%--------------------------------------------------------------------------
% 4 second zero-padded 1kHz sine + logarithmic sine sweep tone
% 1 sec silence + 2 sec 20Hz to 20kHz tone + 1 sec silence
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------

% clean up
close all;
clear;

% Initializations
%--------------------------------------------------------------------------
sampRate  = 44100;
freq      = 1000;
zeroSec   = 1;
toneSec   = 2;
outFile   = 'sweep_ideal.wav';
toneSamps = toneSec * sampRate;
zeroSamps = zeroSec * sampRate;

% Synthesis
%--------------------------------------------------------------------------

% generate the synthesis vectors
chirpVec = [ 0 : toneSec / (toneSamps - 1) : toneSec ];
sineVec  = [ 0 : 1 : toneSamps - 1 ];

% synthesize the sweep and sine
sweep = chirp( chirpVec, 50, toneSec, 21000, 'logarithmic', 270 );
sine  = sin( 2 * pi * ( freq / sampRate ) * sineVec );

% mix sweep and sine tones
tone = sweep + sine;

% generate silence
zero = zeros( 1, zeroSamps );

% concatenate waveform and silence into output vector
output = [ zero( 1, 1 : zeroSamps ), tone( 1, 1 : toneSamps ), zero( 1, 1 : zeroSamps ) ];

% normalize between -1 and 1
output = output( 1 : end ) / max ( abs ( output ) );

% Output
%--------------------------------------------------------------------------

% create comment string
commentString = sprintf( 'Frequency: %s\nWaveform: %s', '50Hz-21kHz', 'sine + log sweep' );

% write file to disk
audiowrite( outFile, output, sampRate, 'BitsPerSample', 32, 'Artist', 'NormCoeff', 'Title', num2str( 1 ), 'Comment', commentString );

% EOF