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

% clean up
close all;
clear;

% Initializations
%--------------------------------------------------------------------------
sampRate  = 44100;
freq      = 1000;
zeroSec   = 0.5;
toneSec   = 1;
outFile   = 'sweep.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
bird = chirp( chirpVec, 50, toneSec, 21000, 'logarithmic', 270 );
sine = sin( 2 * pi * ( freq / sampRate ) * sineVec );

% mix sweep and sine tones
tone = bird + 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