%--------------------------------------------------------------------------
% 2 second 1000Hz zero-padded band-limited square tone
% .5 sec silence + 1 sec tone + .5 sec silence
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------
% clean up
close all;
clear;
% Initializations
%--------------------------------------------------------------------------
sampRate = 44100;
freq = 1000;
zeroSec = 0.5;
toneSec = 1;
harmonics = 10;
harmNum = 1;
outFile = 'square.wav';
toneSamps = toneSec * sampRate;
zeroSamps = zeroSec * sampRate;
tone = zeros( 1, toneSamps );
% Synthesis
%--------------------------------------------------------------------------
% generate a vector to synthesize the waveform
toneVec = [ 0 : 1 : toneSamps - 1 ];
% generate band-limited square wave
while harmNum <= ( harmonics * 2 )
% synthesize harmonic
harmonic = sin( 2 * pi * ( ( freq * harmNum ) / sampRate ) * toneVec ) / harmNum;
% mix harmonic into tone vector
tone = tone( 1 : toneSamps ) + harmonic( 1 : toneSamps );
% increment harmonic number
harmNum = harmNum + 2;
end
% 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: %.0f\nWaveform: %s', freq, 'square' );
% write file to disk
audiowrite( outFile, output, sampRate, 'BitsPerSample', 32, 'Artist', 'NormCoeff', 'Title', num2str( 1 ), 'Comment', commentString );
% EOF
% 2 second 1000Hz zero-padded band-limited square tone
% .5 sec silence + 1 sec tone + .5 sec silence
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------
% clean up
close all;
clear;
% Initializations
%--------------------------------------------------------------------------
sampRate = 44100;
freq = 1000;
zeroSec = 0.5;
toneSec = 1;
harmonics = 10;
harmNum = 1;
outFile = 'square.wav';
toneSamps = toneSec * sampRate;
zeroSamps = zeroSec * sampRate;
tone = zeros( 1, toneSamps );
% Synthesis
%--------------------------------------------------------------------------
% generate a vector to synthesize the waveform
toneVec = [ 0 : 1 : toneSamps - 1 ];
% generate band-limited square wave
while harmNum <= ( harmonics * 2 )
% synthesize harmonic
harmonic = sin( 2 * pi * ( ( freq * harmNum ) / sampRate ) * toneVec ) / harmNum;
% mix harmonic into tone vector
tone = tone( 1 : toneSamps ) + harmonic( 1 : toneSamps );
% increment harmonic number
harmNum = harmNum + 2;
end
% 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: %.0f\nWaveform: %s', freq, 'square' );
% write file to disk
audiowrite( outFile, output, sampRate, 'BitsPerSample', 32, 'Artist', 'NormCoeff', 'Title', num2str( 1 ), 'Comment', commentString );
% EOF